在Application.java

时间:2017-04-05 05:38:14

标签: spring-boot dependency-injection mybatis

我有一些奇怪的情况(spring-boot):
我在bean中有一个循环,但在Application.javamain with SpringApplication.run(Application.class, args))中没有循环。在其他类(循环发生的地方)中,我使用@Configuration

注释类

怎么可能?
这些bean是:

 @ConfigurationProperties(prefix = "my2")
    @PostConstruct
    @Bean
    public javax.sql.DataSource dataSource2() {
        return ...
    }

    @ConfigurationProperties(prefix = "my1")
    @PostConstruct
    @Bean
    public javax.sql.DataSource dataSource1() {
        return ...
    }

    @Primary
    @Bean
    public RoutingDataSource dataSource() {
        RoutingDataSource rDS= new RoutingDataSource();
        routingDataSource.setDefaultTargetDataSource(dataSource1);

        Map<Object, Object> tds = new HashMap<Object, Object>();
        tds.put("my2", dataSource2);
        tds.put("my1", dataSource1);
        rDS.setTargetDataSources(tds );
        return rDS;
    }

修改

@Configuration
class DataConfig {  
     @ConfigurationProperties(prefix = "my2")
        @PostConstruct
        public javax.sql.DataSource dataSource2() {
            return ...
        }

        @ConfigurationProperties(prefix = "my1")
        @Bean
        public javax.sql.DataSource dataSource1() {
            return ...
        }

        @DependOn(value = {"dataSource2", "dataSource1"})
        @Primary
        @Bean
        public RoutingDataSource dataSource() {
            RoutingDataSource rDS= new RoutingDataSource();
            routingDataSource.setDefaultTargetDataSource(dataSource1);

            Map<Object, Object> tds = new HashMap<Object, Object>();
            tds.put("my2", dataSource2);
            tds.put("my1", dataSource1);
            rDS.setTargetDataSources(tds );
            return rDS;
        }
}

0 个答案:

没有答案