我正在尝试创建一些东西,它将根据可配置的属性自动创建bean(来自application.yml
等)。
因为我不能像通常那样访问属性组件BeanFactoryPostProcessor
,我有点困惑如何访问它们。
如何访问应用程序属性BeanFactoryPostProcessor
?
如果您想以类型安全的方式访问属性BeanFactoryPostProcessor
你需要将它们绑定Environment
自己使用Binder
API。这本质上就是 Boot 本身支持的功能@ConfigurationProperties
beans.
Your BeanFactoryPostProcessor
看起来像这样:
@Bean
public static BeanFactoryPostProcessor beanFactoryPostProcessor(
Environment environment) {
return new BeanFactoryPostProcessor() {
@Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
BindResult<ExampleProperties> result = Binder.get(environment)
.bind("com.example.prefix", ExampleProperties.class);
ExampleProperties properties = result.get();
// Use the properties to post-process the bean factory as needed
}
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)