0
votes

Following these instructions here I want to have my MQConnection class properties to be loaded from the application.properties file.

The problem is that the properties are only loaded if I put a @Value("${attributename}") annotation on each and every property. I do not want to tag each and every property, I rather set the class prefix and let spring correlate and map my class properties to those found in application.properties.

My setup:

  • application.properties is on the classpath in src/main/resources
  • My @Configuration class also has @EnableConfigurationProperties
  • My MQConnection class has both @Component and @ConfigurationProperties(prefix="mq")

Configuration class:

@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ComponentScan
public class Application implements CommandLineRunner {
 ...
}

MQConnection class:

@Component
@ConfigurationProperties(prefix="mq")
public class MQConnection{

  @Value("${mq.hostname}") // will only work if @Value is here, don't want this
  private String hostname;
  private int port;
  private String qmanager;
  private String queue;
  private String channel;
}

application.properties:

mq.hostname=localhost
mq.port=5120
mq.qmanager=MyQueueManager
mq.queue=MyQueue
mq.channel=MyChannel
2

2 Answers

2
votes

Your MQConnection class is not a Java bean (no getters and setters) so Spring can't bind to it. If you don't like getters and setters use Groovy or Project Lombok.

0
votes

The root cause of this problem is likely due to be missing the @EnableConfigurationProperties annotation in your configuration file(s) via @Configuration.