自定义spring属性源不解析@Value中的占位符

前端之家收集整理的这篇文章主要介绍了自定义spring属性源不解析@Value中的占位符前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试构建一个Spring 3.1 PropertySource,它从Zookeeper节点读取它的值.为了连接到Zookeeper,我使用的是Netflix的Curator.

为此,我构建了一个自定义属性源,它从Zookeeper读取属性的值并返回它.当我解析这样的属性时,这很好用

ZookeeperPropertySource zkPropertySource = new ZookeeperPropertySource(zkClient);
ctx.getEnvironment().getPropertySources().addLast(zkPropertySource);
ctx.getEnvironment().getProperty("foo"); // returns 'from zookeeper'

但是,当我尝试实例化一个带有@Value注释的字段的bean时,这会失败:

@Component
public class MyBean {
    @Value("${foo}") public String foo;
}

MyBean b = ctx.getBean(MyBean.class); // fails with BeanCreationException

这个问题很可能与Zookeeper没有任何关系,但是我正在注册属性源并创建bean.

任何见解都受到高度赞赏.

更新1:

我正在从这样的XML文件创建应用程序上下文:

public class Main {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ctx.registerShutdownHook();
    }
}

连接到Zookeeper的类是@Component.

@Component
public class Server {
    CuratorFramework zkClient;

    public void connectToZookeeper() {
        zkClient = ... (curator magic) ...
    }

    public void registerPropertySource() {
        ZookeeperPropertySource zkPropertySource = new ZookeeperPropertySource(zkClient);
        ctx.getEnvironment().getPropertySources().addLast(zkPropertySource);
        ctx.getEnvironment().getProperty("foo"); // returns 'from zookeeper'
    }

    @PostConstruct
    public void start() {
        connectToZookeeper();
        registerPropertySource();
        MyBean b = ctx.getBean(MyBean.class);
    }
}

更新2

当我使用无XML配置时,这似乎有效,即@Configuration,@ ComponentScan和@PropertySource与AnnotationConfigApplicationContext结合使用.为什么它不能用于ClassPathXmlApplicationContext?

@Configuration
@ComponentScan("com.goleft")
@PropertySource({"classpath:config.properties","classpath:version.properties"})
public class AppConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
最佳答案
回答您的更新2:这不适用于您的原始配置(使用@PostConstruct注册PropertySource),因为PropertySource注册很晚,此时您的目标bean已经构建并初始化.

通常,占位符的注入是通过BeanFactoryPostProcessor进行的,这是在Spring生命周期的早期(在此阶段尚未创建bean),如果在该阶段注册了PropertySource,则应解析占位符.

最好的方法是使用ApplicationContextInitializer,获取applicationContext的句柄并在那里注册propertySource:

public class CustomInitializer implements ApplicationContextInitializer
原文链接:https://www.f2er.com/spring/432131.html

猜你在找的Spring相关文章