Spring PropertySourcesPlaceholderConfigurer不对@Value使用自定义PropertySource

约翰尼斯·洛佩尔曼

我一直在尝试获取在Spring Application中运行的自定义PropertySource的非常基本的示例。

这是我的PropertySource:

public class RemotePropertySource extends PropertySource{
    public RemotePropertySource(String name, Object source) {
        super(name, source);
    }

    public RemotePropertySource(String name) {
        super(name);
    }

    public Object getProperty(String s) {
        return "foo"+s;
    }
}

它通过ApplicationContextInitializer添加到ApplicationContext中:

public class RemotePropertyApplicationContextInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
    public void initialize(GenericApplicationContext ctx) {
        RemotePropertySource remotePropertySource = new RemotePropertySource("remote");
        ctx.getEnvironment().getPropertySources().addFirst(remotePropertySource);
        System.out.println("Initializer registered PropertySource");
    }
}

现在,我创建了一个简单的单元测试,以查看PropertySource是否正确使用:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RemotePropertySourceTest.ContextConfig.class, initializers = RemotePropertyApplicationContextInitializer.class)
public class RemotePropertySourceTest {

    @Autowired
    private UnderTest underTest;

    @Autowired
    Environment env;

    @Test
    public void testContext() {
        assertEquals(env.getProperty("bar"),"foobar");
        assertEquals(underTest.getFoo(),"footest");
    }


    @Component
    protected static class UnderTest {
        private String foo;

        @Autowired
        public void setFoo(@Value("test")String value){
            foo=value;
        }

        public String getFoo(){
            return foo;
        }
    }

    @Configuration
    @ComponentScan(basePackages = {"test.property"})
    protected static class ContextConfig {
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
            PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
            return configurer;
        }

    }
}

通过环境访问该值可以得到正确的结果(“ foobar”),但是使用@ Value-Annotation失败。据我在文档中阅读的内容,Configuration中的PropertySourcesPlaceholderConfigurer应该会自动从环境中提取PropertySource,但显然不会。我有什么想念的吗?

我知道最好通过环境显式访问属性,但是现有应用程序经常使用@ Value-Annotations。

任何帮助是极大的赞赏。谢谢!

肯·贝科夫

要从属性源获取值,@Value必须使用${}语法:

@Autowired
public void setFoo(@Value("${test}")String value){
    foo=value;
}

看一下官方文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何创建依赖于Spring Bean的自定义Spring PropertySource

自定义类的Spring @Value属性

Spring Cloud配置服务器-如何添加在EnvironmentEncryptorEnvironmentRepository的findOne()方法中可见的自定义PropertySource

如何使用Spring Redis的缓存使用自定义RestTemplate?

使用YAML的Spring @PropertySource

Spring Boot - 使用自定义对象数组 JSON 序列化自定义对象

使用自定义Gson覆盖Spring-Boot的GsonAutoConfiguration

使用自定义DiskSpaceHealthIndicator(Spring Boot执行器)?

如何使用Spring实现自定义WebSocket子协议

使用Spring JPA进行自定义查询的IllegalArgument异常?

使用Spring从自定义XML配置创建对象网络

使用Spring Boot对kafka主题进行自定义分区

使用自定义基础存储库配置Spring @DataJpaTest

使用自定义用户对象时的Spring Session Redis

如何使用 Spring Boot 设置自定义登录?

使用自定义HandlerMethodArgumentResolver的Spring MVC @Valid验证

使用自定义验证器进行Spring Bean验证

使用spring-data-jpa的自定义ItemReader

使用本机查询从Spring Data返回自定义对象

Spring Boot无法使用自定义JPA请求

使用 Spring Cloud Stream (StreamBridge) 包含自定义属性

配置Spring Security以使用自定义UsernamePasswordAuthenticationFilter

使用Gradle在Spring Boot项目中的自定义属性

Spring Boot:使用自定义UserDetailsService配置AuthenticationManager

使用自定义UsernamePasswordAuthenticationFilter的Spring安全并发控制

使用Spring Web MVC自定义处理405错误

使用Spring Boot自定义Jetty的HttpConfiguration

在Spring登录表单中使用自定义表

使用Spring Crystal创建自定义调度程序