Spring 可以懒惰地初始化 Bean(Factory)PostProcessor 吗?
Can Spring init Bean(Factory)PostProcessor lazily?
有人能给我解释一下吗?阅读文档后我不明白。
可以Spring init Bean(Factory)PostProcessor lazy 还是不可以?
有个这样的块把我搞糊涂了:
As with BeanPostProcessors , you typically do not want to configure BeanFactoryPostProcessors for lazy initialization. If no other bean references a Bean(Factory)PostProcessor, that post-processor will not get instantiated at all. Thus, marking it for lazy initialization will be ignored, and the Bean(Factory)PostProcessor will be instantiated eagerly even if you set the default-lazy-init attribute to true on the declaration of your element.
"Can Spring init Bean(Factory)PostProcessor lazily?"问题的正确答案是"NO"。我自己查过。我创建了 2 类:
@Lazy
@Component
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("bean factory!");
}
}
和
@Lazy
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("before init!");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
和运行spring申请。因此,在控制台中打印了:"bean factory" 和几次 "before init",尽管我在这些 类 上添加了 @Lazy 注释。
有人能给我解释一下吗?阅读文档后我不明白。
可以Spring init Bean(Factory)PostProcessor lazy 还是不可以?
有个这样的块把我搞糊涂了:
As with BeanPostProcessors , you typically do not want to configure BeanFactoryPostProcessors for lazy initialization. If no other bean references a Bean(Factory)PostProcessor, that post-processor will not get instantiated at all. Thus, marking it for lazy initialization will be ignored, and the Bean(Factory)PostProcessor will be instantiated eagerly even if you set the default-lazy-init attribute to true on the declaration of your element.
"Can Spring init Bean(Factory)PostProcessor lazily?"问题的正确答案是"NO"。我自己查过。我创建了 2 类:
@Lazy
@Component
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("bean factory!");
}
}
和
@Lazy
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("before init!");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
和运行spring申请。因此,在控制台中打印了:"bean factory" 和几次 "before init",尽管我在这些 类 上添加了 @Lazy 注释。