Spring In Action 运用@Autowired 和@ Resource 主动安装BeanITeye - AG环亚娱乐集团

Spring In Action 运用@Autowired 和@ Resource 主动安装BeanITeye

2019年03月04日14时11分18秒 | 作者: 耘涛 | 标签: 主动,安装,特点 | 浏览: 1295

   从Spring 2.5 开端,主动安装功用进行了屡次改善,能够经过@Autowired或许@Resource注解一个设值办法,结构字段,特点乃至恣意办法主动安装特定的特点。

    为了要求Spring 主动安装具有@Autowired或许@Resource注解的特点,有必要在IOC容器中注册一个AutowiredAnnotationBeanPostProcessor实例。在上下文中什么实例:

bean /

 也能够在bean的配置文件中包括 context:annotation-config/ 元素。或许是

context:component-scan base-package="*" /context:component-scan

 默许情况下一切带@Autowired的特点都是有必要的,假如没有找到匹配的Bean就会抛出如下反常

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name sequenceGenerator: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private SpringInAction.IOC.PrefixGenerator SpringInAction.IOC.SequenceGeneratorImpl.prefixGenerator; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [SpringInAction.IOC.PrefixGenerator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
 at org.springframework.context.support.ClassPathXmlApplicationContext. init (ClassPathXmlApplicationContext.java:139)
 at org.springframework.context.support.ClassPathXmlApplicationContext. init (ClassPathXmlApplicationContext.java:83)
 at SpringInAction.IOC.App.main(App.java:14)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private SpringInAction.IOC.PrefixGenerator SpringInAction.IOC.SequenceGeneratorImpl.prefixGenerator; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [SpringInAction.IOC.PrefixGenerator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
 ... 13 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [SpringInAction.IOC.PrefixGenerator] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:947)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:816)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
 ... 15 more

 假如你期望某个特点是可选的能够将@Autowired的required特点设置为false 。这样当Spring 找不到匹配的bean之后,将不设置该特点。

 

@Autowired(required=false)
private PrefixGenerator prefixGenerator;

 运转成果:

ABC10000A
ABC10001A

 

      默许情况下,依照类型主动安装在IOC容器中有超越一个类型兼容性的Bean时无效,可是Spring 答应指定一个候选Bean 这个Bean的称号在@Qulifier注解中供给。

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐集团立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章