没有xml bean定义的Spring组件检测
只要配置了上下文组件扫描,就可以使用没有xml bean定义的Spring组件检测,spring,annotations,javabeans,Spring,Annotations,Javabeans,只要配置了上下文组件扫描,就可以使用@Component注释创建Springbean,这是否正确 在Java6中使用Spring3.0.5 我的测试用例是: @ContextConfiguration(locations={"classpath:spring-bean.xml"}) public class ServerServiceUnitTest extends AbstractJUnit4SpringContextTests { @Autowired private Se
@Component
注释创建Springbean,这是否正确
在Java6中使用Spring3.0.5
我的测试用例是:
@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
}
}
springbean.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>
spring实例化ServerService
bean并进行自动连接还不够吗
我得到的错误是:
原因:org.springframework.beans.factory.NoSuchBean定义异常:找不到依赖项类型为[serversystem.ServerService]的匹配bean:至少需要1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我确信我遗漏了一些简单的东西。您还没有在
springbeans.xml
中定义
元素:
<context:component-scan base-package="the.package.with.your.service"/>
列入
<context:annotation-config/>
仅允许您使用
@Required
、@Autowired
和@Inject
注释进行配置。通过指定
,您告诉Spring在何处查找@组件
注释。如果您使用的是带注释的控制器和其他功能
你应该包括
<mvc:annotation-driven/>
你应该使用
<context:component-scan base-package="spring3.example.controllers"/>
指定存储控制器类的包
<context:component-scan base-package="spring3.example.controllers"/>