没有 xml bean 定义的 Spring 组件检测
只要配置了上下文组件扫描,就可以仅使用 @Component
注解来创建 spring bean,这是否正确?
将 spring 3.0.5 与 Java 6 结合使用。
我的测试用例是:
@ContextConfiguration(locations={"classpath:spring-bean.xml"})
public class ServerServiceUnitTest extends AbstractJUnit4SpringContextTests {
@Autowired
private ServerService serverService;
@Test
public void test_server_service() throws Exception {
serverService.doSomething();
//additional test code here
}
}
spring-bean.xml
文件包含:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
我想要成为 bean 的类是:
@Component("ServerService")
public class ServerServiceImpl implements ServerService {
private static final String SERVER_NAME = "test.nowhere.com";
//method definitions.....'
}
这是否不足以让 spring 实例化ServerService
bean 并进行自动装配?
我得到的错误是:
由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项的 [serversystem.ServerService] 类型的匹配 bean:预计至少有 1 个有资格作为此依赖项的自动装配候选者的 bean。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我确信我错过了一些简单的东西。
Is it correct that one can create spring beans using just the @Component
annotation as long as context component scanning is configured?
Using spring 3.0.5 with Java 6.
My test case is:
@ContextConfiguration(locations={"classpath:spring-bean.xml"})
public class ServerServiceUnitTest extends AbstractJUnit4SpringContextTests {
@Autowired
private ServerService serverService;
@Test
public void test_server_service() throws Exception {
serverService.doSomething();
//additional test code here
}
}
The spring-bean.xml
file contains:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
My class I want to be a bean is:
@Component("ServerService")
public class ServerServiceImpl implements ServerService {
private static final String SERVER_NAME = "test.nowhere.com";
//method definitions.....'
}
Should that not be sufficient for spring to instantiate the ServerService
bean and do the autowiring?
The error I get is:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [serversystem.ServerService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
I'm sure I'm missing something simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尚未在
spring-beans.xml
中定义
元素:包含
仅允许您使用
@Required<用于配置的 /code>、
@Autowired
和@Inject
注释。通过指定
,您可以告诉 Spring 在哪里查找@Component
注释。You have not defined in your
spring-beans.xml
the<context:component-scan>
element:The inclusion of
only allows you to use
@Required
,@Autowired
, and@Inject
annotations for configuration. By specifying the<context:component-scan>
, you are telling Spring where to look for@Component
annotations.如果您正在使用带注释的控制器和其他功能
您应该包括
您应该用来
指定存储控制器类的包。
if you are using annotated controllers and other features
you should include
you should use
to specify the package in which controller classes are stored.