注:对象交给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注解的支持