如何使用配置 class 在 Spring Boot 中为我的应用程序配置属性?
How do I use a configuration class to configure properties for my application in Spring Boot?
我正在尝试使用 Spring 配置 class 为我的项目设置配置。我创建了一个新的试用项目,因为我不想编辑我的原始项目(很抱歉所有这些废话,但如果用户是新用户,系统似乎不会原谅几乎所有代码 post)
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 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
我做错了什么?
问题就出在这里
@Autowired
public ---> static <---- Config configuration;
Spring 无法自动装配 static
字段。您需要从自动装配字段中删除静态
那么您将无法在 main
方法中使用此字段,因为 main
被声明为静态的。这里的解决方案如下。在不使用 @Autowired
以编程方式加载应用程序上下文后,您需要检索 bean
@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 容器来不及接管对象的创建。
你得到空指针异常的原因,你不能像那样初始化spring。您的静态变量未初始化。
我的建议:
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);
}
}
我正在尝试使用 Spring 配置 class 为我的项目设置配置。我创建了一个新的试用项目,因为我不想编辑我的原始项目(很抱歉所有这些废话,但如果用户是新用户,系统似乎不会原谅几乎所有代码 post)
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 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
我做错了什么?
问题就出在这里
@Autowired
public ---> static <---- Config configuration;
Spring 无法自动装配 static
字段。您需要从自动装配字段中删除静态
那么您将无法在 main
方法中使用此字段,因为 main
被声明为静态的。这里的解决方案如下。在不使用 @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 容器来不及接管对象的创建。
你得到空指针异常的原因,你不能像那样初始化spring。您的静态变量未初始化。
我的建议:
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);
}
}