为什么@autowired在我访问bean时不起作用 [英] why @autowired is not working when I access a bean
本文介绍了为什么@autowired在我访问bean时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用像这样的BeanFactory从spring bean配置文件访问bean时:
When I access a bean from spring bean configuration file using BeanFactory like this:
public class Person {
private String id,address;
@Autowired
private Customer customer;
//setters & getters
}
和bean配置文件
<bean name="person" class="com.ram.spring.model.Person"></bean>
<bean class="com.ram.spring.model.Customer">
<property name="email" value="ram@adp.com"></property>
<property name="name" value="Ram"></property>
</bean>
这里是执行者类
public class PersonExecutor {
public static void main(String[] args) {
BeanFactory context = new XmlBeanFactory(new ClassPathResource("Spring.xml"));
Person person = (Person)context.getBean("person");
System.out.println(person.getCustomer());
}
}
当我执行此操作时,我得到了 null
.is注释不支持BeanFactory ??任何想法??
when I execute this, I got null
.is BeanFactory not supported for annotations?? any ideas??
推荐答案
正如@jens建议
你应该活动注释扫描
<context:component-scan base-package="package_path">
</context:component-scan>
<context:annotation-config />
希望有帮助
这篇关于为什么@autowired在我访问bean时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文