目標:從我的 Java 類中的 application.properties 檔案中讀取屬性值。
當前行為:獲取空值
預期行為:
value1
value3
應用程式屬性
category1.subcategory2=value1
category1.subcategory3=value3
greeting.salutation=Hello
TryValueApplication.java
package com.example.tryvalue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TryValueApplication {
@Value("${category1.subcategory2}")
private String category1;
@Value("basic value")
private String category3;
public static void main(String[] args) {
SpringApplication.run(TryValueApplication.class, args);
TryValueApplication tryValueApplication = new TryValueApplication();
System.out.println("Running main method");
tryValueApplication.printCategory();
}
public void printCategory(){
System.out.println("Print environment values");
System.out.println(category1);
System.out.println(category3);
}
}

試過: 我嘗試從 @RestController 類中訪問相同的值,它正在作業。GreetingController.java
package com.example.tryvalue.controller;
import com.example.tryvalue.EnvironmentValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@Value("${greeting.salutation}")
private String greetingSalutation;
@Autowired
private EnvironmentValue environmentValue;
@GetMapping
public String greeting(){
return greetingSalutation " world";
}
@GetMapping("/category1")
public String getCategory1(){
return environmentValue.getCategory1();
}
}
環境值.java
package com.example.tryvalue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentValue {
@Value("${category1.subcategory2}")
private String category1;
@Value("${category1.subcategory3}")
private String category2;
@Value("basic value")
private String category3;
public EnvironmentValue(){
System.out.println("Creating instance of EnvironmentValue");
}
public String getCategory1(){
return category1;
}
public String getCategory2(){
return category2;
}
public String getCategory3(){
return category3;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>tryValue</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tryValue</name>
<description>tryValue</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

這種行為差異背后的原因是什么?謝謝 。
uj5u.com熱心網友回復:
您正在 main 方法中創建 TryValueApplication 的第二個非托管實體,因此不會將屬性注入其中。
TryValueApplication 的托管實體應該具有注入的值。將此添加到 TryValueApplication 以列印值 @Value("${category1.subcategory2}")
private String category1;
@Value("${category1.subcategory3}")
private String category3;
public static void main(String[] args) {
SpringApplication.run(TryValueApplication.class, args);
}
@PostConstruct
public void printCategory(){
System.out.println("Print environment values");
System.out.println(category1);
System.out.println(category3);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/513986.html
標籤:弹簧靴特性应用程序属性
上一篇:如何在springboot應用程式的單個JPARepository中添加多個物體類
下一篇:上傳檔案教程
