我有一个非常简单的Spring Boot应用程序,我正在尝试使用一些外部化配置.我试图按照弹簧启动文档中的信息,但我正在遇到路障.
当我运行app.properties文件中的外部配置下面的应用程序时,不会填充bean中的变量.我确定我做的事情很愚蠢,谢谢你的任何建议.
MyBean.java(位于/ src/main/java/foo/bar /)
package foo.bar;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
@Value("${some.prop}")
private String prop;
public MyBean() {
System.out.println("================== " + prop + "================== ");
}
}
Run Code Online (Sandbox Code Playgroud)
Application.java(位于/ src/main/java/foo /)
package foo;
import foo.bar.MyBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
@Autowired
private MyBean myBean;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} …Run Code Online (Sandbox Code Playgroud) 语境:
我使用@Scheduled注释处理报告,并且当Component从Service属性调用时未使用@Value即使它物理存在于.properties并在 中打印出来,@PostConstruct。
描述:
ReportProcessor接口及InventoryReportProcessor实现:
@FunctionalInterface
interface ReportProcessor {
public void process(OutputStream outputStream);
}
@Component
public class InventoryReportProcessor implement ReportProcessor {
@Value("${reportGenerator.path}")
private String destinationFileToSave;
/*
@PostConstruct
public void init() {
System.out.println(destinationFileToSave);
}
*/
@Override
public Map<String, Long> process(ByteArrayOutputStream outputStream) throws IOException {
System.out.println(destinationFileToSave);
// Some data processing in here
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我用它从
@Service
public class ReportService {
@Value("${mws.appVersion}")
private String appVersion;
/* …Run Code Online (Sandbox Code Playgroud)