我正在嘗試配置我的.yml,但我總是得到一個空指標例外......
我有以下的.yml:
search_files:
csv:
path: ./
name: export_files_
extension: .csv
我有以下的pom配置:
<build>
<plugins>/span>
<plugin>/span>
<groupId>org.springframework.boot</groupId>/span>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>/span>
<plugin>/span>
<groupId>/span>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>/span>
<argLine>-Dspring.config.location=file:${home}/config/application.yml</argLine>/span>
</configuration>/span>
</plugin>/span>
</plugins>/span>
<resources>/span>
<resource>/span>
<directory>/span>src/resources</directory>/span>
<filtering>true</filtering>
<excludes>/span>
<exclude>**/application.yml</exclude>
</excludes>
</resource>/span>
</resources>/span>
</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: 不能呼叫"org.springframework.core.env.Environment.getProperty(String)",因為"this.env" 是 null。
at com.victorprojects.utils.searchFiles.utils.CSVUtils.createCsv(CSVUtils.java:28)
我在互聯網上搜索了很多資訊,但我總是得到同樣的錯誤......
我如何解決這個問題?
我怎樣才能解決這個問題呢?
編輯我添加了一張關于我的專案結構的照片:
您可以下載該專案。
你可以在這里下載該專案:
uj5u.com熱心網友回復:
我假定你的專案設定為:
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
在一個靜態字串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}"/span>)
public void setPath(String Path) {
CSVUtils.PATH = path。
uj5u.com熱心網友回復:
你可以創建一個類并添加配置屬性為:-
(prefix = "anyClass"/span>, locations = "file:config/abc.properties")
public class AnyClass{
private String name;
public String getName() {
return name;
}
public void setName(String name)。{
this.name = name;
}
在你的主應用程式類中,提供注解@EnableConfigurationProperties如下:-
@SpringBootApplication。
@EnableConfigurationProperties({AnyClass.class)
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication .class, args)。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/324183.html
標籤:
