web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
applicationContext.xml
<context:annotation-config/>
<bean id="WcgTransactionLogin"
class="com.mposa.channel.wcg.WcgTransactionLogin">
</bean>
<bean id="WcgTransactionOther"
class="com.mposa.channel.wcg.WcgTransactionOther">
</bean>
<!-- 交易列表 -->
<bean id="TestSpring"
class="com.mposa.action.TestSpring">
<property name="tran">
<list>
<ref bean="WcgTransactionLogin"></ref>
<ref bean="WcgTransactionOther"></ref>
</list>
</property>
</bean>
在tomcat启动的时候,已经出现这样的日志了:
[13:52:37|DEBUG|(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor)]=[Autowiring by type from bean name 'TestSpring' to bean named 'WcgTransactionLogin']
[13:52:37|DEBUG|(org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor)]=[Autowiring by type from bean name 'TestSpring' to bean named 'WcgTransactionOther']
[13:52:37|DEBUG|(org.springframework.beans.factory.support.DefaultListableBeanFactory)]=[Returning cached instance of singleton bean 'WcgTransactionLogin']
[13:52:37|DEBUG|(org.springframework.beans.factory.support.DefaultListableBeanFactory)]=[Returning cached instance of singleton bean 'WcgTransactionOther']
[13:52:37|DEBUG|(org.springframework.beans.factory.support.DefaultListableBeanFactory)]=[Finished creating instance of bean 'TestSpring']
我基本认为这个已经注入成功了。
但是在实际运行的时候,这个变量却是null。
但是,我如果在TestSpring里面直接使用main函数进行调用:
public class TestSpring {
@Autowired
private List<Transaction> tran = new ArrayList<Transaction> ();
public void show(){
System.out.println("trans:"+tran.size());
}
public List<Transaction> getTran() {
return tran;
}
public void setTran(List<Transaction> tran) {
this.tran = tran;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
TestSpring test = (TestSpring) ac.getBean("TestSpring");
test.show();
}
}
直接执行,是ok的。
不知道为啥,在此标记,等有时间再查明,如果有大神了解,请告知,非常感谢。