【发布时间】:2015-04-23 00:55:21
【问题描述】:
我有这样的事情:
@Configuration
public class SpringConfigUtil {
private static final Logger logger = Logger.getLogger(SpringConfigUtil.class);
@Value("#{ systemProperties['activemq.username'] }")
private String userName;
@Value("#{ systemProperties['activemq.password'] }")
private String password;
@Value("#{ systemProperties['activemq.URL'] }")
private String brokerURL;
@Value("#{ systemProperties['activemq.queueName'] }")
private String queueName;
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean(name="producer")
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setDefaultDestination(new ActiveMQQueue(queueName));
jmsTemplate.setConnectionFactory(connectionFactory());
return jmsTemplate;
}
@Bean
public ActiveMQConnectionFactory connectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(brokerURL);
activeMQConnectionFactory.setUserName(userName);
activeMQConnectionFactory.setPassword(password);
return activeMQConnectionFactory;
}
}
它工作正常。 假设我还有一个 applicationContext.xml 文件,它已经加载了一些 bean。
如何在 @Configuration 中引用这些 bean? 我不想再次以编程方式创建 bean,因为它们已经通过加载 applicationContext.xml 创建。
让我有 50 多个属性。有没有比为每个属性定义类似以下内容更好的方法来引用它们?
@Value("#{ systemProperties['activemq.URL'] }")
private String brokerURL;
感谢您的宝贵时间!
【问题讨论】:
标签: java spring spring-annotations