2012-05-29 5 views
0

많은 응용 프로그램 속성이 저장소를 통해 주입하려는 데이터베이스에서 제공됩니다. 이것이 봄과 함께 할 수 있는지 궁금합니다. 누군가가 해결책을 제안 할 수 있다면 행복 할 것입니다. 난 그냥 그것을보고 서비스에 PlatformSettingRepositoy를 주입 할 수 알고스프링 설정 저장소

@Component 
    public class ExampleService implements Service { 

     private PlatformSetting setting1; 

     @Required 
     @Qualifier("setting1") 
     public void setSetting1(PlatformSetting setting1) { 
      this.setting1 = setting1; 
     } 

     public String getMessage() { 
      return "Hello world!" + setting1.getValue();  
     } 

    } 


    @Repository 
    public class PlatformSettingRepository { 


     private HashMap<String, PlatformSetting> settings; 
     { 
      settings = new HashMap<String, PlatformSetting>(); 
      settings.put("setting1", new PlatformSetting("bla1")); 
      settings.put("setting2", new PlatformSetting("bla2")); 

     } 

     @Bean 
     public PlatformSetting findSetting(@Qualifier String qual) { 
      return settings.get(qual); 
     } 
    } 

: 내가 생각 해요 코드는 다음과 liek 뭔가를 보인다. 하지만 나는 스프링 컨테이너가 시작할 때 그들을 수행하기를 원할 때 호출 시간에 이러한 조회를하고 싶지 않다.

답변

1

스프링 표현식 언어를 사용하십시오.

1 단계 : 설정이 @Bean을 사용하여 해시 맵 노출은 ID = "appSettings는"

2 단계 말할 수 있습니다 :

@Value("#{ appSettings['setting1'] }") 
private PlatformSetting setting1; 

: 이제 그냥 주석을 사용, 설정 1 주입 할 위치 참고 :이 값은 일반 값과 ​​함께 사용되며 사용자의 경우에도 올바르게 작동합니다. 나는 그것을 시도하지는 않았지만. 이 방법이 효과가 없다면 이미 bean을 초기화하는 자바 설정 방법을 사용하고 있기 때문에 직접적으로 표현 파서를 사용할 수 있습니다.

+0

감사합니다. 이것은 정확히 내가 찾고 있었던 것입니다. – cproinger

3

PropertyPlaceholderConfigurer와 CommonsConfigurationFactory를 함께 사용하면 답이됩니다. 게시물 this을 살펴보십시오.

0

당신은 InitializingBean를 사용할 수 있습니다

@Component 
public class Config implements InitializingBean { 

    /** 
    * Used to hold app properties. 
    */ 
    private Properties properties = new Properties(); 

    //Getters, setters and filling properties from where you need 

    public void afterPropertiesSet() throws Exception { 
     //Initialize some static properties of other objects here. 
    } 
} 

를 주입 구성보다 다른 클래스.