[英]How do I use a configuration class to configure properties for my application in Spring Boot?
I am trying my hand at using a Spring Configuration class to set up configuration for my project.我正在尝试使用 Spring Configuration 类为我的项目设置配置。 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:演示应用程序.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:配置.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:应用程序.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?我做错了什么?
Here lies the problem这就是问题所在
@Autowired
public ---> static <---- Config configuration;
Spring can't autowire static
fields. Spring 不能自动装配
static
字段。 You need to remove static from the autowired field您需要从自动装配字段中删除静态
Then you would not be able to use this field inside the main
method because main
is declared as static.然后您将无法在
main
方法中使用此字段,因为main
被声明为静态。 The solution here is the following.这里的解决方案如下。 You need to retrieve the bean after the application context is loaded programmatically without the use of
@Autowired
您需要在以编程方式加载应用程序上下文后检索 bean,而不使用
@Autowired
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
Config configuration = ctx.getBean(Config.class);
String fullname = configuration.name + " " + configuration.surname + " " + configuration.age;
System.out.println(fullname);
}
}
Config对象是静态的,所以一开始会被创建,Spring容器接管对象的创建已经来不及了。
The reason that you get null pointer exception, you can't initialize spring like that.你得到空指针异常的原因,你不能像那样初始化spring。 Your static variable isn't initialized.
您的静态变量未初始化。
My suggestion:我的建议:
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 implements ApplicationRunner{
@Autowired
private Config configuration;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(ApplicationArguments args) {
String fullname = configuration.name + " " + configuration.surname + " " + configuration.age;
System.out.println(fullname);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.