복붙노트

[SPRING] Spring 부트 외부 구성 및 xml 컨텍스트

SPRING

Spring 부트 외부 구성 및 xml 컨텍스트

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}와 같은 매개 변수를 사용하고 싶습니다.

하지만 작동하지 않습니다. 도와 주셔서 미리 감사드립니다.

해결법

  1. ==============================

    1.@PropertySource를 제거하십시오. @EnableAutoConfiugration을 추가하고 @ImportResource를 사용하여 xml 구성 파일을 가져 오십시오.

    @PropertySource를 제거하십시오. @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 파일의 내용에 따라 일부 파일을 삭제할 수도 있습니다 (스프링 부트는 자동으로 리소스를 자동으로 구성 할 수 있기 때문에).

  2. ==============================

    2.applicationContext.xml에서 를 사용하십시오.

    applicationContext.xml에서 를 사용하십시오.

    그리고 다음과 같이 xml 기반 구성을 가져옵니다.

    @ImportResource({"classpath*:applicationContext.xml"})
    
  3. from https://stackoverflow.com/questions/26634426/spring-boot-external-configuration-and-xml-context by cc-by-sa and MIT license