2017-09-14 1 views
1

INT 활성화를 반환하고 ForgotPassword 잘 작동 int로하지만, 문자열 변수 내가 appSettings는 정적 클래스@PropertySource @Value 정적 문자열) 도착 널

@PropertySource("classpath:appSettings.properties") 
    public class AppSettings { 

     @Value("${Activation}") 
     private static int Activation; 
     @Value("${ForgotPassword}") 
     private static int ForgotPassword; 
     @Value("${CryptoSplit}") 
     private static String CryptoSplit; 
     @Value("${CryptoKey}") 
     private static String CryptoKey; 

     public static String getCryptoSplit() { 
      return CryptoSplit; 
     } 

     public static String getCryptoKey() { 
      return CryptoKey; 
     } 
     public static int getActivation() { 
      return Activation; 
     } 

     public static int getForgotPassword() { 
      return ForgotPassword; 
     } 

    } 

.properties의

Activation=0 
ForgotPassword=1 
CryptoSplit=:OSK: 
CryptoKey=TheBestSecretKey 
+1

쓰기 setter 메소드를 대신 setter 메소드에 값 '@'넣어 변수를 넣는 것 – pvpkiran

+1

'static '필드/메소드에'@ Value'를 자동으로 연결하거나 사용할 수 없습니다. –

+0

값 주석을 사용하여 정적 변수를 정의하려면 [이 답변] (https://stackoverflow.com/a/45192557/3493036)을 살펴보십시오. – Patrick

답변

0

스프링이 지원되지 않는다. t @Value정적 필드에 주입.

확실히 "AppSettings의 정적 클래스"가 필요합니까? 스프링 싱글 톤의 작동 방식에 대한 오해를 나타낼 수도 있습니다.

, 그러나, 당신은 다음과 같이 당신이 이를 달성 할 수있는 "appSettings는 정적 클래스"에 대한 진정한 필요가있는 경우 :

@Value("${CryptoKey}") 
public void setCryptoKey(String cryptoKey) { 
    AppSettings.CryptoKey = CryptoKey; 
} 
0

@Value을 (필요 null을 반환 빈이 초기화 될 때 호출된다. 빈이 초기화 될 필요가 없을 때 초기화된다. 그래서 빈이 초기화되지 않으면 값을 가지지 않는다.