,可以通过以下步骤实现:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "pf")
public class ExternalPropertiesReader {
private String property1;
private String property2;
// Getters and setters for the properties
public String getProperty1() {
return property1;
}
public void setProperty1(String property1) {
this.property1 = property1;
}
public String getProperty2() {
return property2;
}
public void setProperty2(String property2) {
this.property2 = property2;
}
}
pf.property1=value1
pf.property2=value2
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class YourApplication {
@Autowired
private ExternalPropertiesReader externalPropertiesReader;
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
public void someMethod() {
String property1 = externalPropertiesReader.getProperty1();
String property2 = externalPropertiesReader.getProperty2();
// 使用获取到的属性进行后续操作
}
}
通过以上步骤,你就可以在你的Java项目中访问外部Spring Boot项目中的application.properties属性了。注意,需要确保两个项目在同一个环境中运行,并且外部项目的属性文件中包含了需要访问的属性。
领取专属 10元无门槛券
手把手带您无忧上云