반응형
    
    
    
  순수한 Java 구성을 사용하는 Spring 3.2 @value 주석은 작동하지 않지만 Environment.getProperty는 작동합니다.
나는 이것에 대해 내 머리를 부러 뜨렸다. 내가 무엇을 놓치고 있는지 잘 모르겠습니다. @Value순수한 Java 구성 스프링 앱 (웹이 아님)에서 작동 하도록 주석 을 가져올 수 없습니다.
@Configuration
@PropertySource("classpath:app.properties")
public class Config {
    @Value("${my.prop}") 
    String name;
    @Autowired
    Environment env;
    @Bean(name = "myBean", initMethod = "print")
    public MyBean getMyBean(){
         MyBean myBean = new MyBean();
         myBean.setName(name);
         System.out.println(env.getProperty("my.prop"));
         return myBean;
    }
}
속성 파일에는 my.prop=avalue다음과 같은 빈이 포함 됩니다.
public class MyBean {
    String name;
    public void print() {
        System.out.println("Name: " + name);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
환경 변수는 값을 제대로 인쇄하지만 인쇄 @Value하지 않습니다.
 avalue
 Name: ${my.prop} 
메인 클래스는 컨텍스트를 초기화합니다.
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
그러나 내가 사용하면
@ImportResource("classpath:property-config.xml")
이 스 니펫으로
<context:property-placeholder location="app.properties" />
그러면 잘 작동합니다. 물론 이제 환경이 반환 null됩니다.
Config클래스 에 다음 빈 선언을 추가하십시오.
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
위해서는 @Value작업에 주석을 PropertySourcesPlaceholderConfigurer등록해야합니다. 그것은 사용할 때 자동으로 수행됩니다 <context:property-placeholder>XML에 있지만로 등록해야 static @Bean사용할 때 @Configuration.
@PropertySource 문서 및이 Spring Framework Jira 문제를 참조하십시오 .
반응형
    
    
    
  'programing' 카테고리의 다른 글
| 기존 원의 곡선 텍스트 (0) | 2021.01.15 | 
|---|---|
| xAxis에서 시간 형식을 지정하는 방법 d3.js 사용 (0) | 2021.01.14 | 
| Visual Studio 2013을 사용하여 관리 코드를 디버깅 할 수 없음 ( "식을 평가할 수 없음"오류-디버그 빌드 사용 중) (VS 2012가 작동 함) (0) | 2021.01.14 | 
| 플렉스 랩은 align-self, align-items 및 align-content와 어떻게 작동합니까? (0) | 2021.01.14 | 
| 기본 데이터 소스를 업데이트 할 때 DataGridView를 새로 고치는 가장 좋은 방법 (0) | 2021.01.14 |