有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Spring中重写依赖项属性值

在我的Spring项目中,我使用的是在Spring中开发的依赖项项目。此依赖项有自己的属性文件,并定义了一个指向localhost的属性。现在在我的设置中,我希望此属性指向另一个URL,但不指向localhost。我试图在我的属性文件中使用属性源的addFirst方法覆盖它,但是依赖项仍然加载原始属性值

ConfigurableEnvironment environment = applicationContext.getEnvironment();
//here i overload the props
environment.getPropertySources().addFirst(
                new ResourcePropertySource("classpath:conf/app.properties")); 
LOG.debug("dependency property: " + applicationContext.getEnvironment().
getProperty("server.hostname")); // here it prints the overloaded value in app.properties

当我打印重载属性时,我得到重载属性值,但当程序执行时,它指向localhost。这是重写依赖属性的方法吗?Spring版本是3.2


共 (1) 个答案

  1. # 1 楼答案

    关键是,在PropertySources中,最后一个是赢家

    (就像在数据库中,最后一个写的是wins)

    试着简单地使用add