- html - 我的下拉菜单的内容关闭得太快
- c# - 使用 Html Agility Pack 从网页中的表中获取值而不使用 "SelectNode'
- html - 内容容器下的 CSS 下拉菜单
- html - 如何停止嵌套列表重叠父列表?
如何访问由 <context:property-placeholder>
加载的属性在BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
.
我无法使用带有 @Value
注释的字段,因为它们似乎没有被初始化(它们的值为空)。
最佳答案
只有在 BeanDefinitionRegistry
的后处理之后,才能设置用 @Value
注释的字段的值,这意味着它们在初始化过程的这个阶段不可用。
然而,您可以显式扫描配置环境并从那里读取相关属性的值,然后在您的动态 bean 定义中使用它们。
要访问配置环境,您可以在使用 @Bean
注释的方法中创建您的 BeanDefinitionRegistryPostProcessor
,该方法将 ConfigurableEnvironment
作为一个参数。
请看下面的例子:
package com.sample.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;
@Configuration
public class DynamicBeanConfig {
private static final String PROPERTY_KEY = "somename";
@Bean
public BeanDefinitionRegistryPostProcessor beanPostProcessor(ConfigurableEnvironment environment) {
return new PostProcessor(environment);
}
class PostProcessor implements BeanDefinitionRegistryPostProcessor {
private String propertyValue;
/*
* Reads property value from the configuration, then stores it
*/
public PostProcessor(ConfigurableEnvironment environment) {
propertyValue = readProperty(environment);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {}
/*
* Creates the bean definition dynamically (using the configuration value), then registers it
*/
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SampleDynamicBean.class);
builder.addPropertyValue("property", propertyValue);
registry.registerBeanDefinition("sampleDynamicBean", builder.getBeanDefinition());
}
/*
* Iterates over all configuration sources, looking for the property value.
* As Spring orders the property sources by relevance, the value of the first
* encountered property with the correct name is read and returned.
*/
private String readProperty(ConfigurableEnvironment environment) {
for (PropertySource<?> source : environment.getPropertySources()) {
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> propertySource = (EnumerablePropertySource<?>) source;
for (String property : propertySource.getPropertyNames()) {
if (PROPERTY_KEY.equals(property))
{
return (String)propertySource.getProperty(PROPERTY_KEY);
}
}
}
}
throw new IllegalStateException("Unable to determine value of property " + PROPERTY_KEY);
}
}
class SampleDynamicBean {
private String property;
public void setProperty(String property)
{
this.property = property;
}
public String getMessage()
{
return "This message is produced by a dynamic bean, it includes " + property;
}
}
}
示例代码改编自这篇博文,https://scanningpages.wordpress.com/2017/07/28/spring-dynamic-beans/
关于spring - 访问 BeanDefinitionRegistryPostProcessor 中的 Spring 加载属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54690946/
在此系列文章中,我总结了Spring几乎所有的扩展接口,以及各个扩展点的使用场景。并整理出一个bean在spring中从被加载到最终初始化的所有可扩展点的顺序调用图。这样,我们也可以看到bean是如
我的用例处理动态创建 bean。我发现它们是注册 bean 定义的两个选项:ImportBeanDefinitionRegistrar和 BeanDefinitionRegistryPostProce
如何访问由 加载的属性在BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry . 我无法使用带有 @Value
如何访问 BeanDefinitionRegistryPostProcessor (BDRPP) 内部的 ApplicationContext?我有以下 BDRPP。 public class MyC
我正在尝试使用 BeanDefinitionRegistryPostProcessor 动态创建 N 个 bean .基于 this问题,我选择使用 BeanDefinitionRegistryPos
假设我有这个@Configuration类: @Configuration public class SomeConfig{ @Bean public MyBean myBean(){
我正在动态创建 spring bean(使用: https://scanningpages.wordpress.com/2017/07/28/spring-dynamic-beans/ 中描述的方法)
我是一名优秀的程序员,十分优秀!