作为Spring Boot项目的一部分,我试图在按如下方式创建Spring ApplicationContext之前先阅读application.properties,
主班
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurationClass con = new ConfigurationClass();
con.readProperties();
ApplicationContext c = SpringApplication.run(DemoApplication.class, args);
}
}
配置类
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:application.properties")
public class ConfigurationClass {
private String value1;
private String value2;
@Autowired
private Environment env;
public void readProperties() {
value1 = env.getProperty("app.value1");
value2 = env.getProperty("app.value2");
}
public String getValue1() {
return value1;
}
public String getValue2() {
return value2;
}
}
我想从application.properties获取一些基本值并进行一些配置。此代码发生的事情是,由于nullpointerexception
在env
期间为空,因此它抛出了value1 = env.getProperty("app.value1");
。
我在这里做错了什么?
如何在创建springapplication上下文之前的这个时间点读取application.properties中的值?
任何帮助将不胜感激!
答案 0 :(得分:1)
@Configuration
@PropertySource("classpath:application.properties")
public class ConfigurationClass {
@Value("${app.value1}")
private String value1;
@Value("${app.value2}")
private String value2;
public String getValue1() {
return value1;
}
public String getValue2() {
return value2;
}
}
然后调用
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext c = SpringApplication.run(DemoApplication.class, args);
ConfigurationClass con = c.getBean(ConfigurationClass.class);
System.out.println(con.getValue1());
System.out.println(con.getValue2());
}
}
答案 1 :(得分:0)
Spring只会将依赖项注入到Spring Bean中,例如,如果您使用new
关键字显式创建任何对象,则它不是Spring Bean,例如
ConfigurationClass con = new ConfigurationClass();
我也知道实例变量不能在静态区域中访问,这可能会使您无法使用new
关键字创建对象,因此有两种方法可以解决此问题
第一种方法:建议使用ApplicationRunner
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication implements ApplicationRunner {
@Autowire
private ConfigurationClass con;
public static void main(String[] args) {
ApplicationContext c = SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(ApplicationArguments args) throws Exception {
con.readProperties();
System.out.println(con.getValue1());
System.out.println(con.getValue2());
}
}
第二种方法仅用于共享知识
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext c = SpringApplication.run(DemoApplication.class, args);
ConfigurationClass con = c.getBean(ConfigurationClass.class);
con.readProperties();
System.out.println(con.getValue1());
System.out.println(con.getValue2());
}
}
注意::您还可以使用@Value批注来读取属性