今天在使用spring注入bean实例的时候,不知道为什么注入的是null,百度了好久也找不到答案,问一个同学他给我发来了一篇博文是用的@Autowired注解,但是我并没有使用注解,而是使用的普通的xml文件方式来注入,但是注入的是null,搞得我头大。
参考博文地址:https://blog.csdn.net/qq_32623363/article/details/80666942
1. application.xml文件我是这样写的
<bean id="customerDAO" class="com.myshop.dao.CustomerDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="customerService" class="com.myshop.service.CustomerService">
<property name="customerDAO">
<ref bean="customerDAO" />
</property>
</bean>
<bean id="customerAction" class="com.myshop.action.CustomerAction">
<property name="customerService">
<ref bean="customerService" />
</property>
</bean>
2. Action类部分代码:
private CustomerService customerService;
public CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
就这样,注入的customerServie是null,不知道为什么
3. 后来将action类中代码修改为:仅仅是增加了static
@Autowired
private static CustomerService customerService;
public static CustomerService getCustomerService() {
return customerService;
}
public void setCustomerService(CustomerService customerService) {
this.customerService = customerService;
}
这样就好了,不知道为什么,有知道大神可以告诉我不