【发布时间】:2015-05-06 14:54:23
【问题描述】:
我是 Spring 的新手。我遇到了 Spring-Boot 的问题。我正在尝试将外部配置文件中的字段自动装配到自动装配的 bean 中。我有以下课程
App.java
public class App {
@Autowired
private Service service;
public static void main(String[] args) {
final SpringApplication app = new SpringApplication(App.class);
//app.setShowBanner(false);
app.run();
}
@PostConstruct
public void foo() {
System.out.println("Instantiated service name = " + service.serviceName);
}
}
AppConfig.java
@Configuration
@ConfigurationProperties
public class AppConfig {
@Bean
public Service service() {
return new Service1();
}
}
服务接口
public interface Service {
public String serviceName ="";
public void getHistory(int days , Location location );
public void getForecast(int days , Location location );
}
服务1
@Configurable
@ConfigurationProperties
public class Service1 implements Service {
@Autowired
@Value("${serviceName}")
public String serviceName;
//Available in external configuration file.
//This autowiring is not reflected in the main method of the application.
public void getHistory(int days , Location location)
{
//history code
}
public void getForecast(int days , Location location )
{
//forecast code
}
}
我无法在 App 类的 postconstruct 方法中显示服务名称变量。我这样做对吗?
【问题讨论】:
标签: java spring spring-boot