Вот мой боб, который я хочу быть условно созданным < /p>
Код: Выделить всё
@Service
@ConditionalOnProperty(value = "polling.enabled", havingValue = "true")
public class MessageQueueService {
@Scheduled(fixedDelay = INTERVAL_MS)
public void execute() {
//sysout
}
}
< /code>
Пара вещей, которые нужно отметить. < /p>
- Наша услуга нагружает свойства непосредственно из консула (из-за устаревших причин), а не в Spring Environment < /p>
- я взломал Bean-Bean (ниже), чтобы добавить Custom Propertyresource в среду. Я не знал, как это сделать без создания другого экземпляра бобов. < /P>
@Configuration
public class AppConfig {
@Autowired
ConfigurableEnvironment env;
@Bean(name = "consulProps")
public Properties properties() {
Properties consulProperties = new ConsulDriver(env.getConsulUrl()).loadProperties();
return properties;
}
@Bean
@Autowired
public RestTemplate restTemplate(@Qualifier(consulProps) Properties props) {
MutablePropertySources sources = env.getPropertySources();
sources.addFirst(pollingEnabled(props));
return new RestTemplate();
}
private MapPropertySource pollingEnabled(Properties props) {
String enabled = props.getProperty("polling.enabled"); // line 25
Map map = new HashMap();
if (StringUtils.isNotBlank(enabled)) {
map.put("polling.enabled", enabled);
}
else {
map.put("polling.enabled", "false");
}
return new MapPropertySource("polling", map);
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
return new PropertySourcesPlaceholderConfigurer();
}
}
< /code>
Когда я запускаю службу, она начинается. Я настроил opporling.enabled
Подробнее здесь: https://stackoverflow.com/questions/508 ... ot-project