serverfaces
serverfaces

Reputation: 1175

Referring applicationContext.xml bean in Spring @Configuration

I have something like this:

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

It works fine. Let's say I also have an applicationContext.xml file which has loaded some beans.

How do I refer to those beans here in @Configuration? I don't want to create beans programmatically again as they are already created by loading applicationContext.xml.

Let's have I have 50+ properties. Is there a better way to refer to them than defining something like the following for every property?

  @Value("#{ systemProperties['activemq.URL'] }") 
    private String brokerURL;

Thanks for your time!

Upvotes: 1

Views: 705

Answers (1)

xerx593
xerx593

Reputation: 13299

How do I refer to those beans here in @Configuration?

According to http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06.html, please try:

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

Let's have I have 50+ properties. Is there a better way to refer to them than defining something like the following for every property?

None, that I can imagine :-( ... how could you "better access" 50+ properties, than "by their name" - "by index", array-like!? Though some properties can be coupled, at the end of the day" each has (should have) its special meaning and purpose - and for that touched/wired/configured individually. (the best practice tip is just to do it as rare as possible)

Upvotes: 1

Related Questions