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

前端 未结 2 1168
抹茶落季
抹茶落季 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

    The following sample code worked for me.

    Main Application

    package app;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.util.Assert;
    
    import javax.sql.DataSource;
    
    public class Application {
    
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(Config.class);
            DataSource dataSource = context.getBean("dataSource", DataSource.class);
            Assert.notNull(dataSource);
        }
    
    }
    

    Spring Java Config

    package app;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.*;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    
    import javax.sql.DataSource;
    
    @Configuration
    @EnableAutoConfiguration
    @EnableJpaRepositories
    @ComponentScan
    @ImportResource("classpath:config.xml")
    public class Config {
    
        @Autowired
        private DataSource dataSource;
    
    }
    

    BarService

    package app.service;
    
    import org.springframework.util.Assert;
    import javax.annotation.PostConstruct;
    import javax.sql.DataSource;
    
    public class BarService {
    
        private DataSource dataSource;
    
        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }
    
        @PostConstruct
        public void init() {
            Assert.notNull(dataSource);
        }
    }
    

    FooService

    package app.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.util.Assert;
    
    import javax.annotation.PostConstruct;
    import javax.sql.DataSource;
    
    @Service
    public class FooService {
    
        @Autowired
        DataSource dataSource;
    
        @PostConstruct
        public void init() {
            Assert.notNull(dataSource);
        }
    
    }
    

    config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="barService" class="app.service.BarService">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
    </beans>
    
    0 讨论(0)
  • 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...

    <bean id="barService" class="app.service.BarService">
        <property name="dataSource" ref="dataSource"/>
        <property name="connectionFactory" ref="jmsConnectionFactory"/>
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    

    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

    0 讨论(0)
提交回复
热议问题