如何开启基于注解的自动装配?

Sping自动装配注解浏览:418收藏:1
答案:
要使用 @Autowired,需要注册 AutowiredAnnotationBeanPostProcessor,可以有以下两种方式来实现:
1、引入配置文件中的<bean>下引入 <context:annotation-config>
<beans>
    <context:annotation-config />
</beans>

2、在bean配置文件中直接引入AutowiredAnnotationBeanPostProcessor
<beans>
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>