【问题标题】:Accessing spring loaded properties in BeanDefinitionRegistryPostProcessor访问 BeanDefinitionRegistryPostProcessor 中的弹簧加载属性
【发布时间】:2023-03-28 23:50:01
【问题描述】:

如何访问<context:property-placeholder>BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry 中加载的属性。

我无法使用带有@Value 注释的字段,因为它们似乎没有被初始化(它们的值为空)。

【问题讨论】:

  • 显示一些东西When I use @Value, it does not load the properties.

标签: spring spring-bean property-placeholder


【解决方案1】:

设置带有@Value 注释的字段的值仅发生在BeanDefinitionRegistry 的后处理之后,这意味着它们在初始化过程的这个阶段不可用。

但是,您可以显式扫描配置环境并从那里读取相关属性的值,然后在您的动态 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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2015-11-12
    • 2018-01-16
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多