简体   繁体   English

Spring boot @Value NullPointerException

[英]Spring boot @Value NullPointerException

I'm writing a Spring Boot application and am trying to load some values from a properties file using the @Value annotation.我正在编写一个 Spring Boot 应用程序,并尝试使用@Value注释从属性文件加载一些值。 However, the variables with this annotation remain null even though I believe they should get a value.但是,带有此注释的变量仍然为空,即使我认为它们应该获得一个值。

The files are located in src/main/resources/custom.propertes and src/main/java/MyClass.java .这些文件位于src/main/resources/custom.propertessrc/main/java/MyClass.java

(I have removed parts of the code that I believe are irrelevant from the snippets below) (我已经删除了我认为与下面的代码片段无关的部分代码)

MyClass.java我的类

@Component
@PropertySource("classpath:custom.properties")
public class MyClass {
    @Value("${my.property:default}")
    private String myProperty;

    public MyClass() {
        System.out.println(myProperty); // throws NullPointerException
    }
}

custom.properties自定义属性

my.property=hello, world!

What should I do to ensure I can read the values from my property file?我应该怎么做才能确保我可以从我的属性文件中读取值?

Thanks!谢谢!

@value will be invoked after the object is created. 创建对象后将调用@value Since you are using the property inside the constructor hence it is not available. 由于您在构造函数内部使用属性,因此该属性不可用。

You should be using constructor injection anyway. 无论如何,您都应该使用构造函数注入。 It makes testing your class easier. 它使测试您的课程变得更加容易。

public MyClass(@Value("${my.property:default}") String myProperty) {
    System.out.println(myProperty); // doesn't throw NullPointerException
}

You are getting this error because you are initializing the class with new keyword.您收到此错误是因为您正在使用 new 关键字初始化类。 To solve this, first you need to create the configuration class and under this class you need to create the bean of this class.要解决这个问题,首先需要创建配置类,在这个类下需要创建这个类的bean。 When you will call it by using bean then it will work..当您使用 bean 调用它时,它将起作用..

My code:我的代码:

        @Component
        @PropertySource("db.properties")
        public class ConnectionFactory {
            @Value("${jdbc.user}")
            private String user;
            @Value("${jdbc.password}")
            private String password;
            @Value("${jdbc.url}")
            private String url;
            Connection connection;

            @Bean
            public String init(){

                return ("the value is: "+user);
            }

        My Config.class:

        @Configuration
        @ComponentScan
        public class Config {

            @Bean
            public Testing testing() {
                return new Testing();
            }

            @Bean
            public ConnectionFactory connectionFactory() {
                return new ConnectionFactory();
            }
        }

        Calling it:

        public static void main(String[] args) {
                AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(Config.class);
                ConnectionFactory connectionFactory= context.getBean(ConnectionFactory.class);
                System.out.println(connectionFactory.init());
            }








        



        


        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM