如何使用配置类在Spring Boot中为我的应用程序配置属性?
我正在尝试使用春季配置类为项目设置配置。我创建了一个新的试用项目,因为我不想编辑我的原始项目(对不起,所有这些废话,但是如果用户是新的,该系统几乎没有宽恕所有代码帖子)
demoapplication.java.java:config.java:config.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
@Autowired
public static Config configuration;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
String fullname = configuration.name + " " + configuration.surname + " " + configuration.age;
System.out.println(fullname);
}
}
:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:application.yml")
public class Config {
@Value("${person.name}")
public String name;
@Value("${person.surname}")
public String surname;
@Value("${person.age}")
public Integer age;
}
application.yml:
person:
name: name1
surname: surname1
age: 25
例外:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
at com.example.demo.DemoApplication.main(DemoApplication.java:15)
... 5 more
我做错了什么?
I am trying my hand at using a Spring Configuration class to set up configuration for my project. I created a new trial project for I did not want to edit my original project (Sorry for all this crap, but it seems the system does not forgive an almost all code post if the user is new)
DemoApplication.java:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
@Autowired
public static Config configuration;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
String fullname = configuration.name + " " + configuration.surname + " " + configuration.age;
System.out.println(fullname);
}
}
Config.java:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:application.yml")
public class Config {
@Value("${person.name}")
public String name;
@Value("${person.surname}")
public String surname;
@Value("${person.age}")
public Integer age;
}
application.yml:
person:
name: name1
surname: surname1
age: 25
Exception:
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
at com.example.demo.DemoApplication.main(DemoApplication.java:15)
... 5 more
What have I done wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是问题所在的问题
。您需要从自动化字段中删除静态
,然后您将无法在
main
方法中使用此字段,因为main
将其声明为静态。这里的解决方案是以下内容。在应用程序上下文通过编程加载后,您需要检索bean,而无需使用@Autowired
Here lies the problem
Spring can't autowire
static
fields. You need to remove static from the autowired fieldThen you would not be able to use this field inside the
main
method becausemain
is declared as static. The solution here is the following. You need to retrieve the bean after the application context is loaded programmatically without the use of@Autowired
配置对象是静态的,因此它首先会创建,这对于弹簧容器接管对象的创建为时已晚。
The Config object is static,so it would be created at first,which is too late for spring container to take over the creation of object.
您获得Null指针异常的原因,您不能这样初始化Spring。您的静态变量未初始化。
我的建议:
The reason that you get null pointer exception, you can't initialize spring like that. Your static variable isn't initialized.
My suggestion: