【问题标题】:Spring Boot external configuration and xml contextSpring Boot 外部配置和 xml 上下文
【发布时间】:2014-12-25 09:33:00
【问题描述】:

我想用 Spring Boot 外部化我的配置,但我想继续部分使用我的 xml 上下文。

我的主类 SpringServerApplication.java :

@Configuration
@PropertySources(value = {@PropertySource("classpath:/application.properties")})
public class SpringServerApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(new Object[] {
                SpringServerApplication.class, "classpath:ApplicationContextServer.xml" }, args);
    }

}

我将我的配置放在 application.properties 中。

在 ApplicationContextServer.xml 中,我想使用一些像这样的参数:${user}。

但它不起作用。提前感谢您的帮助。

【问题讨论】:

  • 错误?例外?乍一看我会说,不需要属性源注释,因为这是默认值
  • 也许可以试试@ImportResource 而不是显式参数
  • 是例外,因为变量没有被它们的值替换。 @ImportResource 出现同样的错误。

标签: java spring spring-boot


【解决方案1】:

applicationContext.xml 中使用<context:property-placeholder/>

并像这样导入基于xml的配置:

@ImportResource({"classpath*:applicationContext.xml"})

【讨论】:

  • 不,我有这个错误:java.lang.IllegalArgumentException:无法解析字符串值“${user}”中的占位符“用户”
【解决方案2】:

删除 @PropertySource,因为 Spring Boot 已经完成了,而是添加 @EnableAutoConfiugration 并使用 @ImportResource 导入您的 xml 配置文件。

@Configuration
@EnableAutoConfiguration
@ImportResource("classpath:ApplicationContextServer.xml")
public class SpringServerApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(new Object[] {SpringServerApplication.class}, args);
    }
}

这应该足以做你想做的事。根据您的 xml 文件中的内容,您甚至可以删除其中的一些内容(因为 Spring Boot 可以很容易地为您自动配置资源)。

【讨论】:

  • 我刚刚在 10 分钟前找到了解决方案...我添加了 @EnableAutoConfiugration 并且它有效。不过谢谢!我还将删除 PropertySource。
  • 这似乎在 Springboot 1.4 中不起作用。知道如何解决吗?
猜你喜欢
  • 2014-10-10
  • 2014-11-09
  • 2017-11-26
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多