- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一处特性 test=default
在 DefaultConfig 类中,我使用 @PropertySource 注释使它们可用。
@Configuration
@PropertySource("classpath:default.properties")
public class DefaultConfig {}
然后我希望能够覆盖到 test=override
,它位于类 OverrideConfig 的不同属性文件中,所以我再次使用 @PropertySource。
@Configuration
@Import(DefaultConfig.class)
@PropertySource("classpath:override.properties")
public class OverrideConfig {}
我配置了一个测试来证明它有效。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={OverrideConfig.class})
public class TestPropertyOverride {
@Autowired
private Environment env;
@Test
public void propertyIsOverridden() {
assertEquals("override", env.getProperty("test"));
}
}
当然不是。
org.junit.ComparisonFailure: expected:<[override]> but was:<[default]>
最大化调试,我可以看到发生了什么:
StandardEnvironment:107 - Adding [class path resource [default.properties]] PropertySource with lowest search precedence
StandardEnvironment:107 - Adding [class path resource [override.properties]] PropertySource with lowest search precedence
似乎倒退了。我是否犯了一个简单的错误或误解了这一点,或者您是否希望@Import-ed 配置类中的@PropertySource 定义的属性被@Import-ing 类中的@PropertySource 中定义的属性覆盖?
最佳答案
这是 Helder Sousa 的解决方案,写为 a comment of the JIRA issue由 OP 创建:
[T]he behaviour available in spring xml (one xml importing another xml) is achievable using nested configurations:
@Configuration
@PropertySource("classpath:default.properties")
public class DefaultConfig {}@Configuration
@PropertySource("classpath:override.properties")
public class OverrideConfig {
@Configuration
@Import(DefaultConfig.class)
static class InnerConfiguration {}
}With this setup, the properties will be gather in the proper order.
关于spring - @Import 覆盖 Spring @PropertySource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15577125/
在Spring框架中@PropertySource注解是非常常用的一个注解,其主要作用是将外部化配置解析成key-value键值对"存入"Spring容器的Environment
我想使用 @PropertySource 注释设置动态属性源值。谁能告诉我如何实现这一目标?对于下面我动态传递属性文件名。 @Configuration @PropertySource("classp
为什么当我尝试从属性文件输出数据时,显示的数据是错误的? 在我的 ChatApp 项目中,我有 datasource-cfg.properties 文件: # DataSource ds.databa
我有一个@Configuration类。这个类有一个@PropertySource。 我希望每个应用程序都有一个不同的属性。 示例: @Configuration @PropertySource("f
我有一个完美的测试设置,如下所示...... @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = Annota
自 4.0 以来, Spring 为所有标记为 @Configuration 的类引入了一个新的注解 @PropertySources。它采用不同的 @PropertySource 作为参数。 @Pr
我正在使用 Spring Java 配置来创建我的 bean。但是这个 bean 对 2 个应用程序是通用的。两者都有一个属性文件 abc.properties 但具有不同的类路径位置。当我把明确的类
在我的 sprint 启动应用程序中,我有一个配置类来读取属性文件:common.properties 和 dev.properties。我在两个属性文件中都有相同的 key server.url。该
我正在 Tomcat 中将 Spring Boot 应用程序部署为 WAR 文件。我希望我的 application-local.properties 能够覆盖 application.propert
我的配置文件有问题,它位于我的 jar 文件之外的其他目录中。 我使用 @PropertySource 加载属性。 @PropertySource(ignoreResourceNotFound = t
我只需要读取MyServiceImpl.java类中的menu.properties文件这些值不是特定于环境的。 menu.properties ---------------- menu.optio
我有一个 micronaut 项目,我想为私有(private)数据(如数据库连接等)提供一个非版本化的配置文件 此信息必须通过@Property注释加载,但由于将有多个.yml(至少还有一个appl
我有一个关于以编程方式向 Spring 上下文添加属性的问题。 GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(
我正在尝试通过以下方式创建一个 spring 配置类 @Configuration @PropertySource(value={"file:${my.dir}/fileone.properties"
嗨,我是 Spring 和 Jpa 集成的初学者。当我尝试配置我的数据库连接时,详细信息处理程序 itp。我发现了 Spring 的奇怪行为。 首先,我有3个配置文件: 1) RootConfig -
如何在 spring 中刷新 @PropertySouce(propertyFile)。 例如:-属性文件中有一个参数:- MY_APP_NAME=MY APPLICATION 我正在使用
我想用 java ... -Denv=prod ... 启动我的程序并且有 @PropertySource("classpath:/settings/$idontknowwhat$/database.
配置 @Configuration @PropertySources({ @PropertySource("classpath*:properties/test-database.proper
我遇到了连线问题,但没有找到任何提示。 我正在使用属性文件处理数据库分片配置。我得到了一个负责加载这些属性的类: @Component @PropertySources(value = *arrayO
无论如何我可以在我的程序中知道通过Spring的@PropertySource注释加载的文件的完整路径。我需要它显示在日志中,以便可以知道应用程序中正在使用哪个属性文件 最佳答案 此信息已由 Stan
我是一名优秀的程序员,十分优秀!