我有一个用@Component
注释的类,该类用于初始化application.yml配置属性。服务类正在使用配置属性。但是有时我的Service类实例在Configuration类之前创建,并且在Service类中获得了空属性值,它是随机的而不是特定的模式。
配置初始化器类..
@Component
public class ConfigInitializer implements InitializingBean {
private static final Logger log = LoggerFactory.getLogger(ConfigInitializer.class);
@Autowired
ProxyConfig proxyConfig;
/*@PostConstruct
public void postConstruct(){
setProperties();
}
*/
@Override
public void afterPropertiesSet() {
setProperties();
}
private void setSystemProperties(){
log.debug("Setting properties...");
Properties props = new Properties();
props.put("PROXY_URL", proxyConfig.getProxyUrl());
props.put("PROXY_PORT", proxyConfig.getProxyPort());
System.getProperties().putAll(props);
}
}
@Component
@ConfigurationProperties(prefix = "proxy-config")
public static class ProxyConfig {
private String proxyUrl;
private String proxyPort;
public String getProxyUrl() {
return proxyUrl;
}
public void setProxyUrl(String proxyUrl) {
this.proxyUrl = proxyUrl;
}
public String getProxyPort() {
return proxyPort;
}
public void setProxyPort(String proxyPort) {
this.proxyPort = proxyPort;
}
}
服务等级..
@Service("receiverService")
public class ReceiverService {
private static final Logger logger = LoggerFactory.getLogger(ReceiverService.class);
private ExecutorService executorService = Executors.newSingleThreadExecutor();
@Autowired
public ReceiverService() {
initClient();
}
private void initClient() {
Future future = executorService.submit(new Callable(){
public Object call() throws Exception {
String value = System.getProperty("PROXY_URL"); **//Here I am getting null**
logger.info("Values : " + value);
}
});
System.out.println("future.get() = " + future.get());
}
}
以上Service类获得null
个值String value = System.getProperty("PROXY_URL")
当我在Service类上使用@DependsOn
注释时,它可以正常工作。
据我所知,我知道Spring没有特定的bean创建顺序。
我想知道是否像下面这样在@Configuration
类上使用@Component
而不是ConfigInitializer
,请问是否会初始化ConfigInitializer
在其他豆类之前上课?。
@Configuration
public class ConfigInitializer implements InitializingBean {
//code here
}