使用Spring读取属性会引发异常

时间:2016-11-23 13:28:00

标签: java spring nullpointerexception spring-properties

我正在尝试使用Spring从文件中读取属性。但是我经常得到NullPointerException s。我不确定自己做错了什么,因为我已经在RestController类中使用@Configuration注释完成了此操作。

@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 个答案:

答案 0 :(得分:0)

好的,谢谢M.Deinum"猜测"我解决了这个问题。我没有意识到我将一个豆子与一个组件混合在一起并忘记了一个新的"弄乱春天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);
  }