Spring配置context:annotation-config说明

Spring的配置文件中,作用是隐式地向Spring容器注册
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor 

RequiredAnnotationBeanPostProcessor 

注册这四个BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean,传统声明方式如下:


使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean,传统声明方式如下:

同样适用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;

使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。

这样的配置会使配置文件变得冗长,Spring便提供

另外在我们使用注解时一般都会配置扫描包路径选项:

该配置项其实也包含了自动注入上述processor的功能,因此当使用后,即可将省去。

你可能感兴趣的:(Spring)