我定義的類加載器,並加入我的所有庫,然後我定義了一個ApplicationContext中,我試圖從外部罐子一類ApplicationContext來的getBean,這裏是我的主要代碼:可以註釋配置工作在Java應用程序項目?
ApplicationContext context = new FileSystemXmlApplicationContext(beanPath) {
@Override
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
super.initBeanDefinitionReader(reader);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.setBeanClassLoader(classLoader);
}
};
我試過的getBean這樣:
AswanService instance = (AswanService)context.getBean("aswanService", AswanService.class);
但問題是,如果我定義哪些屬性具有設定器/ getter方法目標類,實例對象可以用兩個屬性注入時,類定義是這樣的:
public class AswanServiceImpl implements AswanService {
SpringInjectFaker springInjectFaker;
ItemDO itemDO;
public AswanResponse request(AswanRequest aswanRequest) {
springInjectFaker.fake();
AswanResponse response = new AswanResponse();
response.setName("response: " + aswanRequest.getName() + "; " + itemDO.getItemId());
return response;
}
public SpringInjectFaker getSpringInjectFaker() {
return springInjectFaker;
}
public void setSpringInjectFaker(SpringInjectFaker springInjectFaker) {
this.springInjectFaker = springInjectFaker;
}
public ItemDO getItemDO() {
return itemDO;
}
public void setItemDO(ItemDO itemDO) {
this.itemDO = itemDO;
}
}
但如果它與自動裝配Autowired註釋中定義的,它不工作:
public class AswanServiceImpl implements AswanService {
@Autowired
SpringInjectFaker springInjectFaker;
@Autowired
ItemDO itemDO;
public AswanResponse request(AswanRequest aswanRequest) {
springInjectFaker.fake();
AswanResponse response = new AswanResponse();
response.setName("response: " + aswanRequest.getName() + "; " + itemDO.getItemId());
return response;
}
}
實例= context.getBean( 「aswanService」,AswanService.class),實例被注入,但其兩個屬性itemDO和springInjectFaker都是null。
我試圖添加配置,但沒有奏效。
<?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/>
<bean id="aswanService" class="com.taobao.qa.perf.tr.AswanServiceImpl" autowire="byName"></bean>
<bean id="springInjectFaker" class="com.taobao.qa.perf.tr.SpringInjectFakerImp" autowire="byName"></bean>
<bean id="itemDO" class="com.taobao.qa.perf.tr.ItemDO" autowire="byName">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="2"></constructor-arg>
</bean>
</beans>
我的項目是Java項目,我運行的主要功能的程序(公共靜態無效的主要(字串[] args)),感謝您的幫助~~~
我的項目是一個java應用程序項目而不是web應用程序。我試圖添加組件掃描配置。但它也行不通... – liefdiy