2016-11-23 69 views
-1

我想從Spring讀取文件的屬性。不過,我不斷得到NullPointerException s。我不確定我做錯了什麼,因爲我已經在我的RestController類中使用@Configuration註釋完成了此操作。用Spring讀取屬性會拋出異常

@Component 
@PropertySource("classpath:recaptcha.properties") 
public class RecaptchaProvider { 

    @Value("${com.xxx.user.recaptcha.verify.secret}") 
    private String secret; 

    @Value("${com.xxx.user.recaptcha.verify.url}") 
    private String serviceUrl; 

    public RecaptchaProvider() { 
    //do nothing 
    } 

    public String getSecret() { 
    return secret; 
    } 

    public String getServiceUrl() { 
    return serviceUrl; 
    } 
} 

而且手機號碼類:

@Component 
public class RecaptchaServiceImpl implements RecaptchaService { 

    @Autowired 
    RecaptchaProvider provider; 

    @Override 
    public boolean isCaptchaValid(String response) { 
    String secret = provider.getSecret(); // Nullpointer here 
    String serviceUrl = provider.getServiceUrl(); 

    CaptchaValidation captchaValidator = new CaptchaValidation(); 
    ReCaptchaVerfiyResponse result = 
     captchaValidator.postForCaptchaValidation(secret, response, serviceUrl); 

    return Boolean.valueOf(result.getSuccess()); 
    } 
} 

項目設置:

enter image description here

和我的屬性文件中:

com.xxx.user.recaptcha.verify.url=https://www.google.com/recaptcha/api/siteverify 
com.xxx.user.recaptcha.verify.secret=xxxxxxxxxxxx 

除外:

java.lang.NullPointerException 
    at com.xxx.user.recaptcha.impl.RecaptchaServiceImpl.isCaptchaValid(RecaptchaServiceImpl.java:24) 
    at com.xxx.rest.user.impl.UserController.checkReCaptcha(UserController.java:135) 
    at com.xxx.rest.user.impl.UserController.addUser(UserController.java:87) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) 
+1

讓我在你的'UserController'你'新RecaptchaServiceImpl()'猜。另外'@ PropertySource'應該放在'@ Configuration'類上,否則它不會有太多的功能。 –

+1

是的,我做RecaptchaServiceImpl captchaservice =新RecaptchaServiceImpl(); 返回captchaservice.isCaptchaValid(captchaResponse); – Maevy

回答

0

好的感謝M.Deinum「guess」我解決了這個問題。我沒有意識到我混豆與組件,並忘記了一個「新」弄亂了春天LFC

現在它看起來像這樣

@Component 
public class RecaptchaProvider { 

    private String secret; 

    private String serviceUrl; 


    public RecaptchaProvider(String secret, String serviceUrl) { 
    this.secret = secret; 
    this.serviceUrl = serviceUrl; 
    } 

    public String getSecret() { 
    return secret; 
    } 

    public String getServiceUrl() { 
    return serviceUrl; 
    } 
} 

酒店提供

@Configuration 
@PropertySource("classpath:recaptcha.properties") 
public class RecaptchaConfig { 

    @Value("${com.xxx.user.recaptcha.verify.secret}") 
    private String secret; 

    @Value("${com.xxx.user.recaptcha.verify.url}") 
    private String serviceUrl; 

    @Bean 
    public RecaptchaProvider getRecaptchaConfig() { 
    return new RecaptchaProvider(secret,serviceUrl); 
    } 
} 

服務

@Autowired 
    RecaptchaConfig config; 

    @Override 
    public boolean isCaptchaValid(String response) { 
    RecaptchaProvider provider = config.getRecaptchaConfig(); 
    String secret = provider.getSecret(); 
     String serviceUrl = provider.getServiceUrl(); 
     CaptchaValidation captchaValidator = new CaptchaValidation(); 
     ReCaptchaVerfiyResponse result = 
      captchaValidator.postForCaptchaValidation(secret, response, serviceUrl); 

     return Boolean.valueOf(result.getSuccess()); 
    } 
} 

最後在我的控制器中的方法

@Autowired 
    protected RecaptchaServiceImpl recaptchaService; 

    private boolean checkReCaptcha(String captchaResponse){ 
    return recaptchaService.isCaptchaValid(captchaResponse); 
    }