在SpringBootApplication类中使用服务会抛出NullPointerException

时间:2017-02-27 08:07:25

标签: java spring spring-boot

我对SpringBoot很新。我已经创建了一个带有服务类的示例应用程序。

以下是我的 SpringBootApplication

@SpringBootApplication
public class SampleApplication {

    @Autowired
    static AWSService awsService;

    public static void main(String[] args) {

        SpringApplication.run(SampleApplication.class, args);
        awsService.getCertificate();  // Getting an NPE at this point

    }

}

AWSService

@Service
public class AWSService {

    public AWSService() {

    }

    private final Log log = new Log(getClass().getSimpleName());

    public void getCertificate() {
        String accessKey="";
        String secretKey="";
        try {
            Scanner awsCredentials = new Scanner(new File(Constants.AWS_CREDENTIALS));
            accessKey=awsCredentials.next();
            secretKey=awsCredentials.next();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey,secretKey);
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(
                new AWSStaticCredentialsProvider(basicAWSCredentials)).build();
        S3Object s3object = s3Client.getObject(
                new GetObjectRequest(Constants.S3_BUCKET_NAME, Constants.S3_KEY_NAME));
        String temporaryCertificatePath = storeCertificate(s3object);
        Constants.setKeyStoreFile(temporaryCertificatePath);
    }

    private String storeCertificate(S3Object s3Object) {
        try {
            File certificate = File.createTempFile("signingKey",".p12");
            OutputStream outputStream = new FileOutputStream(certificate);
            byte buffer [] = IOUtils.toByteArray(s3Object.getObjectContent());
            outputStream.write(buffer);
            certificate.deleteOnExit();
            return certificate.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return null;
    }
}

以下是错误我正在

Exception in thread "main" java.lang.NullPointerException
2017-02-27 13:24:04.023 at in.juspay.SampleApplication.main(SampleApplication.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 INFO   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
798 at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我的应用程序类中有Autowired个服务,但我得到NullPointerException。如果我正确理解Spring @Service@Autowire应该处理对象的初始化。那么,为什么我会在那时获得NPE?

2 个答案:

答案 0 :(得分:2)

您无法自动装配静态字段。使用应用程序上下文来访问它。

Can you use @Autowired with static fields?

答案 1 :(得分:1)

非常基本的事情,即使在创建bean之前,静态字段也会获得优先级。 这就是为什么你可以使用static关键字来获取实例的访问权。

另请参阅Getting java.lang.NullPointerException when calling Method.invoke