Spring 注解和xml方式注入bean
在看Spring源码,测试到了一个地方有点不解。
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring-test-bean.xml"));在这个xml里面手动配置一个bean,在Spring载入的过程中,xml里面bean会被放在beanFactory中,但是其他的通过注解的注入bean的此时却不在。
有没有朋友能告诉我为什么呢。自动注解是什么时候注入的呢(ApplicationContext我也是过了也是这样)。
我的想法是再启动Spring的过程中,Spring 应该会先把配置文件中的bean注入,在去按照扫描规则找注解注入的。但是,代码就那么几行,他在哪里注入的注解bean呢?
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring-test-bean.xml"));在这个xml里面手动配置一个bean,在Spring载入的过程中,xml里面bean会被放在beanFactory中,但是其他的通过注解的注入bean的此时却不在。
public void xmlbeanFactoryResource() throws IOException {
BeanFactory factory = new XmlBeanFactory(new ClassPathResource(
"spring-test-bean.xml"));
//getBeanTest xml 注入,有值
GetBeanTest beanTest = (GetBeanTest) factory.getBean("getBeanTest");
beanTest.beanTest();
//personArtServiceimpl @service注入 factory 无值,报错
System.out.println(factory.getBean("personArtServiceimpl"));
}有没有朋友能告诉我为什么呢。自动注解是什么时候注入的呢(ApplicationContext我也是过了也是这样)。
我的想法是再启动Spring的过程中,Spring 应该会先把配置文件中的bean注入,在去按照扫描规则找注解注入的。但是,代码就那么几行,他在哪里注入的注解bean呢?
...全文
相关推荐
Spring定义bean的三种方式和自动注入
bean加入spring容器管理的方式,bean加入applicationcontext容器的方式
Spring bean 管理
Spring bean 管理大全 全面的介绍springbean各种操作原理
Spring_依赖注入_面向接口编程_不同加载方式
本项目为Java项目,使用Spring实现依赖注入,实现采用ApplicationContext,不再直接使用XmlBeanFactory方式。适合初学者了解Spring依赖注入的实现。
详解Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理
主要介绍了详解Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring依赖注入底层详解
spring依赖注入底层详解,很不错的资源,欢迎大家来下载学习。


