Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring boot中为application.properties创建自定义属性将返回NullPointerException_Java_Spring_Spring Boot - Fatal编程技术网

Java 在spring boot中为application.properties创建自定义属性将返回NullPointerException

Java 在spring boot中为application.properties创建自定义属性将返回NullPointerException,java,spring,spring-boot,Java,Spring,Spring Boot,当我试图使用自定义属性时,我被一个空指针卡住了。但我看不出我做错了什么。你能看到异常来自哪里吗 这是我创建自定义配置的地方 @ConfigurationProperties("custom") public class CustomProperties { private String awsKey ="testvalue"; private String awsSecret="testvalue"; //getters and setters } 在applicatio

当我试图使用自定义属性时,我被一个空指针卡住了。但我看不出我做错了什么。你能看到异常来自哪里吗

这是我创建自定义配置的地方

@ConfigurationProperties("custom")
public class CustomProperties {


    private String awsKey ="testvalue";

    private String awsSecret="testvalue";
//getters and setters
}
在application.properties中,我覆盖这些值

当我使用
RestController
时,它工作正常,属性被
application.properties中的值覆盖

@RestController
@EnableConfigurationProperties(CustomProperties.class)
public class TestController {

    @Autowired
    private CustomProperties properties;

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(TestController.class);



    @RequestMapping(value = "/createstuf", method = RequestMethod.POST)
    public  Map<String,Object> createStuff(@RequestBody Stuff stuff ,@AuthenticationPrincipal Principal principalUser) throws IOException {


        //THIS WORKS and returns the values i want
        System.out.println(this.properties.getAwsKey());
       System.out.println(this.properties.getAwsSecret());


        //BUT WHEN I USE THIS CLASS I GET A NullPointerException
        testClass testClass = new testClass();

        testClass.ShowME();


          return model;

    }
这是我的application.properties文件

custom.aws-key =xxxxxxxxxxxxxxxx
custom.aws-secret=xxxxxxxxxxxxxxxxx
这是我得到的全部错误

2015-12-18 10:51:52.011 ERROR 6620 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

@Component
注释您的
CustomProperties.class
,您就可以将bean自动连接到其他组件中。

Spring只会将依赖项注入到它知道的bean实例中。然而,在您的代码中,您正在执行
newtestclass()
,这将在Spring的范围之外创建一个新的bean实例。结果是没有注入依赖项,因此得到
NullPointerException


如果需要,请向控制器添加一个
testClass
字段,并在其上放置
@Autowired
。现在,您将获得正确配置的实例。

为什么要在您自己创建的bean实例中进行自动连接……您是否也可以显示stacktrace?这样,我们就可以看到空指针的确切来源。我猜你的类名和实例名都被称为“testClass”(而类名应该以大写字母开头,即testClass)。当然,在控制台中添加错误输出,它会为你提供这些信息…@chieftwoils我更新我的答案。你的
testClass
是什么意思。您真的每次都需要新的实例吗?或者它可以是一个spring管理的bean。这取决于你想要实现什么。但是您的问题(为什么是空指针)的答案是spring根本不知道您的bean实例。
2015-12-18 10:51:52.011 ERROR 6620 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause