How can I use Spring Boot auto-configured beans in XML configuration files?

前端 未结 2 1169
抹茶落季
抹茶落季 2021-02-01 20:45

I\'d like to take advantage of some of the Spring Boot auto-configured beans in XML configuration files, but I keep running into exceptions and errors when I try to do so.

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 21:14

    I used your autoconfig-test sample project and was able to get it to work. The only problem I found with your xml is as follows...

    I'm assuming you want to use the embedded broker. When I ran your project, that was what was auto configured...

    @Bean
    public ConnectionFactory jmsConnectionFactory() {
        return this.properties.createConnectionFactory();
    }
    

    This creates a bean named jmsConnectionFactory, however your xml is trying to wire a bean named connectionFactory. Changing the bean name to jmsConnectionFactory fixes that...

    
        
        
        
    
    

    I don't know why you would want to use xml, but it does work.

    Edit: I will also add there may be some misunderstanding of how to use spring jpa integration. Despite the fact the autowiring of the EntityManagerFactory does work, it really shouldn't be used directly. You should be wiring an EntityManager as follows...

    @PersistenceContext
    private EntityManager entityManager
    

    I know its beyond the scope of this question, but just thought I should add that

提交回复
热议问题