繁体   English   中英

在Spring @Configuration中引用applicationContext.xml bean

[英]Referring applicationContext.xml bean in Spring @Configuration

我有这样的事情:

    @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;

谢谢你的时间!

我如何在@Configuration中引用这些bean?

根据http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06.html ,请尝试:

...
@Configuration
@AnnotationDrivenConfig // enable the @Autowired annotation (??)
@ImportXml("classpath:<*path*/to/your/*>applicationContext.xml")
public class SpringConfigUtil { ...

让我拥有50多个属性。 有没有比为每个属性定义如下内容更好的引用它们的方法呢?

没有,我可以想象:-( ...与“按名称”-“按索引”一样,您如何“更好地访问” 50多个属性,就像数组一样!?尽管可以耦合某些属性,但最后”中的每一个都有(应该)有其特殊的意义和目的-并为此进行单独的触摸/接线/配置。(最佳做法是尽可能地做到这一点)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM