注:对象交给spring管理,并且要熟悉bean的生命周期
- 对象必须交给spring管理,new 出来的对象@Value不会生效
- spring管理的对象,使用@Componment、@Configuration等注解标识,@Value才会生效
- 在spring生命周期中,InstantiationAwareBeanPostProcessor(bean的实例化回调后置处理器),在 postProcessBeforeInstantiation(实例化前) 方法的返回值必须为null,@Value才会生效。在 postProcessAfterInstantiation(实例化后)方法返回值必须为true,@Value才会生效。2个方法的返回值不为null和true时,就不会调用 CommonAnnotationBeanPostProcessor 的 postProcessProperties 方法不会调用,该方法就是对@Value和@Autowired注解的支持
本文详细介绍了在Spring框架中@Value注解的正确使用方法及生效条件,包括对象必须由Spring管理、使用@Componment等注解标识、以及在特定的bean后置处理器方法中设置正确的返回值。
259

被折叠的 条评论
为什么被折叠?



