java - Spring Boot 外部配置和 xml 上下文

标签 java spring spring-boot

我想使用 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}。

但是它不起作用。预先感谢您的帮助。

最佳答案

删除 @PropertySource,因为 Spring Boot 已完成此操作,而是添加 @EnableAutoConfiguration 并使用 @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 可以很轻松地为您自动配置资源)。

关于java - Spring Boot 外部配置和 xml 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26634426/

相关文章:

java - JFrame 最小化后不会重新最大化

java - 如何解决此错误 abc_list_selector_disabled_holo_light.9.png?

java - Quartz Job @ExecuteInJTATransaction - 无法在 wildfly 10 中获取 UserTransaction

java - Spring 启动: The Tomcat connector configured to listen on port 8080 failed to start

java - 在 weblogic 中部署耳朵的巨大延迟

java - Spring Boot docker无法连接到mysql(连接被拒绝/createCommunicationsException)

java - Redis PubSub 与 Jedis 重新连接

java - 使用 SQL 构建数据访问层的指南

spring - JNDI 和 Spring 配置 - jee :lookup

spring-boot - 在Kubernetes Pod中访问Spring Boot Controller端点