这里简单介绍一下 BeanFactoryPostProcessor 的作用
A BeanFactoryPostProcessor lets you modify the actual bean definition instead of the instance as it's created.Spring IoC container will allow BeanFactoryPostProcessors to read the configuration metadata and potentially change it before the container has actually instantiated any bean. PropertyPlaceholderConfigurer is the example of BeanFactoryPostProcessors to externalize some property values into another separate file in Java Properties format
我从上面了解到的是 BeanFactoryPostProcessor 在应用程序上下文开始时修改 bean 定义(即在应用程序加载时)。 但我不知道它是否会物理地更改文件或内存中的类定义。
我在我的项目中使用PropertyPlaceholderConfigurer。为了验证这一点,应用程序启动后,我进入类路径并在反编译器中打开类文件 属性 {test.property} 是否被替换为实际值。它不是。那么spring会修改内存中的类定义吗?
最佳答案
BeanFactoryPostProcessor 不会更改物理类定义。在大多数生产应用程序中,运行应用程序的用户甚至无权“写入”文件。所有修改都会在类加载后发生。因此,当您需要一个bean实例时,Spring将加载bean定义(但不会实例化“任何”实例),应用注册的bean工厂后处理器,然后创建一个实例(然后应用bean后处理器,即与 bean 工厂后处理器不同),然后只返回 bean。
关于java - BeanFactoryPostProcessor修改实际的bean定义而不是实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668993/