在spring中获取自动连线对象的空值
我有以下BeanXML,它被导入到ApplicationConfiguration.java中,并在测试类中自动创建了DbManager,但它总是为null。 有人能帮忙吗在spring中获取自动连线对象的空值,spring,Spring,我有以下BeanXML,它被导入到ApplicationConfiguration.java中,并在测试类中自动创建了DbManager,但它总是为null。 有人能帮忙吗 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta
<?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"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<bean id="dbManager" class="com.bandu.myfriendsbook.common.services.dbservices.dbmanager.impl.DbManagerImpl">
</bean>
<bean id="dbManagers" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="dbManager"/>
</list>
</constructor-arg>
</bean>
</beans>
现在只是在ApplicationTest.java中调用bean,但它总是为null
@Component
public class ApplicationTest {
@Autowired
private DbManagerImpl dbManager;
public Integer testQuery(){
return dbManager.testQuery();
}
}
必须将@ComponentScan与参数basePackages或basePackagesClass一起使用。 例如:
必须将@ComponentScan与参数basePackages或basePackagesClass一起使用。 例如: 将
添加到ur xml文件以在@Autowired annotation中工作 将
添加到ur xml文件以在@Autowired annotation中工作 @ComponentScan(“my.package”)?@ComponentScan(“my.package”)?
@Component
public class ApplicationTest {
@Autowired
private DbManagerImpl dbManager;
public Integer testQuery(){
return dbManager.testQuery();
}
}
@ComponentScan(basePackages = {"com.example"})