SprintBoot如何使用已经发布的Bean【三种方式】

现在有如下问题,需要解决

package com.pp.config;

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 这里配置了两个数据源
 */
@Configuration
public class DataSourceConfiguration
{
	@Bean(name="mysqlDataSource")  
    public DataSource createMysqlDataSource() {  
		return DataSourceUtil.createMysqlDataSource();
    } 

	@Bean(name="oracleDataSource")  
    public DataSource createOracleDataSource() {  
		return DataSourceUtil.createOracleDataSource();
    } 
}

package com.pp.config;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class MybatisConfiguration
{
	@Primary
	@Bean(name="mysqlSqlSessionFactory")   
    public SqlSessionFactory createMysqlSqlSessionFactory() throws Exception {  
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
        //这里需要一个dataSource  
        //上面的已经发布了两个dataSource的bean,这里如何把上面发布的mysqlDataSource注入过来  
        //就像 applicationContext.getBean("mysqlDataSource") 一样,拿到;  
        sqlSessionFactoryBean.setDataSource(dataSource);  
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));  
        return sqlSessionFactoryBean.getObject();  
    }  
	
	@Bean(name="oracleSqlSessionFactory")   
    public SqlSessionFactory createOracleSqlSessionFactory() throws Exception {  
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
        //这里需要一个dataSource  
        //上面的已经发布了两个dataSource的bean,这里如何把上面发布的oracleDataSource注入过来  
        //就像 applicationContext.getBean("oracleDataSource") 一样,拿到;  
        sqlSessionFactoryBean.setDataSource(dataSource);  
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));  
        return sqlSessionFactoryBean.getObject();  
    }  
}

注意,这两个方法里面需要的dataSource,不能直接调用DataSourceUtil(该方法每次都会生成一个新的实例对象)的静态方法获取,因为两者持有的是不同的对象

解决上面注释的问题,有如下三种方法


一:

package com.pp.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class MybatisConfiguration
{
	@Autowired
	@Qualifier("mysqlDataSource")
	private DataSource mysqlDataSource;
	
	@Autowired
	@Qualifier("oracleDataSource")
	private DataSource oracleDataSource;
	
    @Primary
    @Bean(name="mysqlSqlSessionFactory")   
    public SqlSessionFactory createMysqlSqlSessionFactory() throws Exception {  
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
        sqlSessionFactoryBean.setDataSource(mysqlDataSource);  
        System.out.println(mysqlDataSource.getConnection().getCatalog());
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));  
        return sqlSessionFactoryBean.getObject();  
    }  
	
    @Bean(name="oracleSqlSessionFactory")   
    public SqlSessionFactory createOracleSqlSessionFactory() throws Exception {  
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
        sqlSessionFactoryBean.setDataSource(oracleDataSource);  
        System.out.println(oracleDataSource.getConnection().getCatalog());
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));  
        return sqlSessionFactoryBean.getObject();  
    }  
}


二:

package com.pp.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class MybatisConfiguration
{
    @Primary
    @Bean(name="mysqlSqlSessionFactory")   
    public SqlSessionFactory createMysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {  
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
        sqlSessionFactoryBean.setDataSource(dataSource);  
        System.out.println(dataSource.getConnection().getCatalog());
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));  
        return sqlSessionFactoryBean.getObject();  
    }  
	
    @Bean(name="oracleSqlSessionFactory")   
    public SqlSessionFactory createOracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {  
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
        sqlSessionFactoryBean.setDataSource(dataSource);  
        System.out.println(dataSource.getConnection().getCatalog());
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));  
        return sqlSessionFactoryBean.getObject();  
    }  
}

三:(不推荐)

package com.pp.config;

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 这里配置了两个数据源
 */
@Configuration
public class DataSourceConfiguration
{
	public static final DataSource mysqlDataSource = DataSourceUtil.createMysqlDataSource();
	
	public static final DataSource oracleDataSource = DataSourceUtil.createOracleDataSource();
	
	@Bean(name="mysqlDataSource")  
    public DataSource createMysqlDataSource() {  
		return mysqlDataSource;
    } 

	@Bean(name="oracleDataSource")  
    public DataSource createOracleDataSource() {  
		return oracleDataSource;
    } 
}

package com.pp.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class MybatisConfiguration
{
	@Primary
	@Bean(name="mysqlSqlSessionFactory")   
    public SqlSessionFactory createMysqlSqlSessionFactory() throws Exception {  
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
        sqlSessionFactoryBean.setDataSource(DataSourceConfiguration.mysqlDataSource);  
        System.out.println(DataSourceConfiguration.mysqlDataSource.getConnection().getCatalog());
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));  
        return sqlSessionFactoryBean.getObject();  
    }  
	
	@Bean(name="oracleSqlSessionFactory")   
    public SqlSessionFactory createOracleSqlSessionFactory() throws Exception {  
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();  
        sqlSessionFactoryBean.setDataSource(DataSourceConfiguration.oracleDataSource);  
        System.out.println(DataSourceConfiguration.oracleDataSource.getConnection().getCatalog());
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));  
        return sqlSessionFactoryBean.getObject();  
    }  
}

注意:DataSourceUtil.createMysqlDataSource()、DataSourceUtil.createOracleDataSource()如果每次放回的都是相同的对象(单例)

那就不需要在第一个类里面声明public static final的类,直接调用方法获取对象


以上三种方法,推荐使用前面两种,不推荐使用第三种方法

你可能感兴趣的:(SprintBoot如何使用已经发布的Bean【三种方式】)