按照此处的这些说明,我要从application.properties文件中加载我的MQConnection类属性。
问题在于,仅当我在每个属性上都使用@Value(“ $ {attributename}”)批注时,才加载属性。我不想标记每个属性,而是设置类前缀并让spring相关并将我的类属性映射到application.properties中的那些属性。
我的设置:
- application.properties在src / main / resources中的类路径上
- 我的@Configuration类还具有@EnableConfigurationProperties
- 我的MQConnection类同时具有@Component和@ConfigurationProperties(prefix =“ mq”)
配置类:
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties
@ComponentScan
public class Application implements CommandLineRunner {
...
}
MQConnection类:
@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
您的MQConnection
类不是Java Bean(没有getter和setter),因此Spring无法绑定到它。如果您不喜欢使用吸气剂和吸气剂,请使用Groovy或Project Lombok。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句