XmlWebApplicationContext实例化成功,但是调用失败

讨论了在使用Spring框架时遇到的@Autowired注解注入失败的问题,并提供了可能的解决方案,包括检查依赖项和配置文件,确保正确引用bean。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的。

不知道为啥,在此标记,等有时间再查明,如果有大神了解,请告知,非常感谢。

转载于:https://my.oschina.net/longtsiu/blog/613341

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值