0

I've got a project (this will be used as a dependency) which uses SpringBoot and which populates a POJO from a queues.properties file in the following way:

@Component
@PropertySource({"classpath:queues.properties"})
@ConfigurationProperties("queue")
public class QueuesConfig {

private String messagingBrokerXml;
private String messagingBrokerJolokia;
private String messagingBrokerApi;
private List<QueuesConfig.QueueModel> queues = new ArrayList();

public QueuesConfig() {
}

public String getMessagingBrokerXml() {
  return this.messagingBrokerXml;
}

 ...

By dragging this dependency in a parent SpringBoot project which has a "queues.properties" file on its classpath, QueuesConfig object gets populated with the right values.

I am currently trying to achieve the same behaviour by using this dependency in a Plain Spring project. I can confirm that the PropertySource annotation gets "executed" and that the queues.properties file is part of the StandardServletEnvironment (being an entry in the propertySourceList).

The thing is that the "ConfigurationPropertiesBindingPostProcessor" bean is not being registered (not part of the singletonObjects) and therefore the code which is supposed to populate the POJO is not being executed.

Is there any workaround for this?

Many thanks !

  • So what you are basically saying is *I want to use this feature of Spring Boot without using Spring Boot*... How do you expect that is going to work? You will need Spring Boot jars on your classpath and manually do what Spring Boot does for you. Then what have you gained but complexity and additional jars on a classpath. – M. Deinum Oct 18 '17 at 21:09

1 Answers1

0

Try to add this in your parent Spring @Configuration class:

@EnableConfigurationProperties(QueuesConfig.class) 

For more details read this section of Spring Boot documentation. It is few screens of text, but it worth it ;-)

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties

Probably it worked well in your test app because you used some hi-level magic with @SpringBootApplication :)

  • I think you also need to use this annotation in a SpringBoot project – Olivier Boissé Oct 18 '17 at 14:32
  • Thanks for the reply! However, that is a spring boot annotation. I don't wanna import spring boot into a plain spring app. – Catalin Lupuleti Oct 18 '17 at 14:33
  • My current workaround is to manually configure the "ConfigurationPropertiesBindingPostProcessor" bean in the parent's application context. However, I don't consider this the right way of doing it. – Catalin Lupuleti Oct 18 '17 at 14:36
  • For plain Spring you may just put @Autowired QueuesConfig config field to your parent project. Not super-attractive, but should work. – Max Alekseev Oct 18 '17 at 14:39
  • @MaxAlekseev I do get QueuesConfig injected as a bean ... ; the issue I am having is that the queues.properties don't get resolved to the POJO. Therefore, I am ending with an empty instantiated object, instead of an object which would be pre-populated with the values from queues.properties. – Catalin Lupuleti Oct 18 '17 at 14:43
  • You may put Value annotation on each field of QueuesConfig with corresponding property name in properties file. For lists and other non-trivial cases use Spring expression language (SpEL). But actually it looks cumbersome to use both ConfigurationProperties and Value annotations to fill in all the data. You may fill those fields in constructor, since all the data will be available from classpath during construction time. – Max Alekseev Oct 18 '17 at 14:55