如何在 SpringBoot 中配置 YML 文件?
How to configure a YML file in SpringBoot?
我正在尝试配置我的 .yml,但我总是遇到空指针异常...
我有以下.yml:
search_files:
csv:
path: ./
name: export_files_
extension: .csv
我有以下 pom 配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dspring.config.location=file:${home}/config/application.yml</argLine>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/application.yml</exclude>
</excludes>
</resource>
</resources>
</build>
我的实现:
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.victorprojects.utils.searchFiles.entity.bean.Result;
@Component
public class CSVUtils {
@Value("${search_files.csv.path}")
private static String PATH;
@Value("${search_files.csv.name}")
private static String NAME;
@Value("${search_files.csv.extension}")
private static String EXTENSION;
public void createCsv(ArrayList<Result> results) {
try {
String fullPath = PATH.concat(NAME.concat(getCurrentDateTime().concat(EXTENSION)));
FileWriter fileWriter = new FileWriter(fullPath, true);
CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.RFC4180);
// HEADER
csvPrinter.printRecord("Name", "Extension", "Size (Kb)", "Path");
for (Result result : results) {
// DATA
csvPrinter.printRecord(result.getName(), result.getExtension(), result.getSize(), result.getPath());
}
csvPrinter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当我执行它时,我收到以下消息:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.springframework.core.env.Environment.getProperty(String)" because "this.env" is null
at com.victorprojects.utils.searchFiles.utils.CSVUtils.createCsv(CSVUtils.java:28)
我在网上查了很多资料,但总是出现同样的错误...
我该如何解决这个问题?
编辑
我添加一张关于我的项目结构的照片:
您可以在这里下载项目:
我假设您的项目设置如下:
projectName
/config/application.yml
/src/main/java/...
/src/main/resources/...
/src/test/java/...
/src/test/resources/...
pom.xml
当您的项目结构如上所示时,您无需提供spring.config.location
,Spring会找到您的配置文件。
但是你可以使用:-Dspring.config.location=config/application.yml
不允许直接在 static 字符串 PROP_NAME 上使用 @Value
所以只需使用:
@Value("${search_files.csv.path}")
private String PATH;
或者使用构造函数注入
@Component
public class CSVUtils {
private String PATH;
private String NAME;
private String EXTENSION;
public CSVUtils(
@Value("${search_files.csv.path}") String path,
@Value("${search_files.csv.name}") String name,
@Value("${search_files.csv.extension}") String extention){
this.NAME=name;
this.PATH=path;
this.EXTENSION=extention;
}
或者使用一个setter(这里可以使用static String PROP_NAME)
private static String PATH
@Value("${search_files.csv.path}")
public void setPath(String path) {
CSVUtils.PATH = path;
}
您可以创建一个 class 并将配置属性添加为:-
@ConfigurationProperties(prefix = "anyClass", locations = "file:config/abc.properties")
public class AnyClass{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
并在您的主应用程序 class 中提供注解 @EnableConfigurationProperties,如下所示:-
@SpringBootApplication
@EnableConfigurationProperties({AnyClass.class)
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication .class, args);
}
}
我已经解决了我的问题...我正在像这样初始化 CSVUtils 构造函数:
Utils utils = new Utils();
但我需要这样初始化它:
@Autowired
Utils utils;
使用第一种方法,我正在显式初始化构造函数,但我需要使用 Spring 注释来完成此操作才能使用 @Value
.
我正在尝试配置我的 .yml,但我总是遇到空指针异常...
我有以下.yml:
search_files:
csv:
path: ./
name: export_files_
extension: .csv
我有以下 pom 配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dspring.config.location=file:${home}/config/application.yml</argLine>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/application.yml</exclude>
</excludes>
</resource>
</resources>
</build>
我的实现:
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.victorprojects.utils.searchFiles.entity.bean.Result;
@Component
public class CSVUtils {
@Value("${search_files.csv.path}")
private static String PATH;
@Value("${search_files.csv.name}")
private static String NAME;
@Value("${search_files.csv.extension}")
private static String EXTENSION;
public void createCsv(ArrayList<Result> results) {
try {
String fullPath = PATH.concat(NAME.concat(getCurrentDateTime().concat(EXTENSION)));
FileWriter fileWriter = new FileWriter(fullPath, true);
CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.RFC4180);
// HEADER
csvPrinter.printRecord("Name", "Extension", "Size (Kb)", "Path");
for (Result result : results) {
// DATA
csvPrinter.printRecord(result.getName(), result.getExtension(), result.getSize(), result.getPath());
}
csvPrinter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
当我执行它时,我收到以下消息:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.springframework.core.env.Environment.getProperty(String)" because "this.env" is null
at com.victorprojects.utils.searchFiles.utils.CSVUtils.createCsv(CSVUtils.java:28)
我在网上查了很多资料,但总是出现同样的错误...
我该如何解决这个问题?
编辑
我添加一张关于我的项目结构的照片:
您可以在这里下载项目:
我假设您的项目设置如下:
projectName
/config/application.yml
/src/main/java/...
/src/main/resources/...
/src/test/java/...
/src/test/resources/...
pom.xml
当您的项目结构如上所示时,您无需提供spring.config.location
,Spring会找到您的配置文件。
但是你可以使用:-Dspring.config.location=config/application.yml
不允许直接在 static 字符串 PROP_NAME 上使用 @Value
所以只需使用:
@Value("${search_files.csv.path}")
private String PATH;
或者使用构造函数注入
@Component
public class CSVUtils {
private String PATH;
private String NAME;
private String EXTENSION;
public CSVUtils(
@Value("${search_files.csv.path}") String path,
@Value("${search_files.csv.name}") String name,
@Value("${search_files.csv.extension}") String extention){
this.NAME=name;
this.PATH=path;
this.EXTENSION=extention;
}
或者使用一个setter(这里可以使用static String PROP_NAME)
private static String PATH
@Value("${search_files.csv.path}")
public void setPath(String path) {
CSVUtils.PATH = path;
}
您可以创建一个 class 并将配置属性添加为:-
@ConfigurationProperties(prefix = "anyClass", locations = "file:config/abc.properties")
public class AnyClass{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
并在您的主应用程序 class 中提供注解 @EnableConfigurationProperties,如下所示:-
@SpringBootApplication
@EnableConfigurationProperties({AnyClass.class)
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication .class, args);
}
}
我已经解决了我的问题...我正在像这样初始化 CSVUtils 构造函数:
Utils utils = new Utils();
但我需要这样初始化它:
@Autowired
Utils utils;
使用第一种方法,我正在显式初始化构造函数,但我需要使用 Spring 注释来完成此操作才能使用 @Value
.