1. 程式人生 > >SprintBoot如何使用已經發布的Bean【三種方式】

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的類,直接呼叫方法獲取物件

以上三種方法,推薦使用前面兩種,不推薦使用第三種方法