복붙노트

[SPRING] @PropertySource의 클래스 패스 와일드 카드

SPRING

@PropertySource의 클래스 패스 와일드 카드

콩을 만들기 위해 Spring Java 설정을 사용하고 있습니다. 하지만이 빈은 2 개의 응용 프로그램에 공통적으로 사용됩니다. 둘 다 하나의 특성 파일 abc.properties를 가지지 만 다른 클래스 경로 위치를가집니다. 내가 명시 적 클래스 패스를 넣을 때

@PropertySource("classpath:/app1/abc.properties")

그때 그것은 작동하지만 내가 와일드 카드를 사용하려고 할 때

@PropertySource("classpath:/**/abc.properties")

그러면 작동하지 않습니다. 나는 많은 와일드 카드 조합을 시도하지만 여전히 작동하지 않습니다. @ProeprtySource에서 와일드 카드 작동 여부 @Configurations로 표시된 클래스의 속성을 읽을 수있는 다른 방법이 있습니까?

해결법

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

    1.@PropertySource API : 리소스 위치 와일드 카드 (예 : ** / *. properties)는 허용되지 않습니다. 각 위치는 정확히 하나의 .properties 자원으로 평가되어야합니다.

    @PropertySource API : 리소스 위치 와일드 카드 (예 : ** / *. properties)는 허용되지 않습니다. 각 위치는 정확히 하나의 .properties 자원으로 평가되어야합니다.

    해결 방법 : 시도하십시오.

    @Configuration
    public class Test {
    
        @Bean
        public PropertyPlaceholderConfigurer getPropertyPlaceholderConfigurer()
                throws IOException {
            PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
            ppc.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties"));
            return ppc;
        }
    
  2. ==============================

    2.dmay에 추가적으로 해결 방법 :

    dmay에 추가적으로 해결 방법 :

    Spring 3.1 이후 PropertySourcesPlaceholderConfigurer는 PropertyPlaceholderConfigurer보다 우선적으로 사용되어야하고 bean은 정적이어야한다.

    @Configuration
    public class PropertiesConfig {
    
      @Bean
      public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertyConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/**/abc.properties"));
        return propertyConfigurer;
      }
    
    }
    
  3. from https://stackoverflow.com/questions/14389995/classpath-wildcard-in-propertysource by cc-by-sa and MIT license