三、spring的profile的使用,以及javaconfig配置整合mybatis,以及el表达式的使用

1.在不同的应用环境下,会出现很多关于配置的问题,本篇将介绍解决不同部署环境时,遇到的一些基础配置是如何配置,在不同环境使用不同的配置,例如:数据库配置,采用spring的profile和引入外部的properties文件进行加载配置。以及使用java注解配置,将简化spring配置,更加易于java开发对spring的理解,下面样例,使用spring的注解配置mybatis、以及el表达式的使用

package nui.honest.spring.demo.profile;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * pgconfig
 * Created by 15692 on 2019/3/9.
 */
@Configuration
@PropertySource("classpath:mybatis/pgconfig.properties")
@MapperScan(basePackages = {"nui.honest.spring.demo.profile.rcm"}, sqlSessionFactoryRef = "sqlSessionFactory")
@Profile("uat")
public class PgDataSourceConfig {

    @Value("${data.driverClassName}")
    private String driverClassName;
    @Value("${data.url}")
    private String url;
    @Value("${data.username}")
    private String username;
    @Value("${data.passwd}")
    private String passwd;

    public String getUrl() {
        return this.url;
    }

    @Bean("dataSource")
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUsername(username);
        dataSource.setUrl(url);
        dataSource.setPassword(passwd);
        return dataSource;
    }
    @Bean("sqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置扫描 mybatis-config.xml
        sqlSessionFactoryBean.setConfigLocation(null);
        //设置扫描mapper.xml
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:/mybatis/mapper/*Mapper.xml");
        sqlSessionFactoryBean.setMapperLocations(resources);
        return sqlSessionFactoryBean.getObject();
    }

}
package nui.honest.spring.demo.profile;

import nui.honest.spring.demo.jdbc.vo.UserBeanVO;
import nui.honest.spring.demo.profile.rcm.IUserDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试类
 * Created by 15692 on 2019/3/9.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = PgDataSourceConfig.class)
@ActiveProfiles("uat")
public class PgDataSourceTest {

    @Autowired
    private IUserDao userDao;

    @Test
    public void getUser() {

        UserBeanVO userBeanVO = userDao.queryUserById(1);
        System.out.println(userBeanVO.getUserName());
    }
}

package nui.honest.spring.demo.profile;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * Created by 15692 on 2019/2/27.
 */
@Configuration
@PropertySource("classpath:mybatis/mysqlconfig.properties")
@MapperScan(basePackages = {"nui.honest.spring.demo.profile.rcm"}, sqlSessionFactoryRef = "sqlSessionFactory")
@Profile("dev")
public class MySqlDataSourceConfig {

    @Autowired
    private Environment env;

    @Bean("dataSource")
    public DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(env.getProperty("data.driverClassName"));
        dataSource.setUrl(env.getProperty("data.url"));
        dataSource.setUsername(env.getProperty("data.username"));
        dataSource.setPassword(env.getProperty("data.passwd"));
        return dataSource;
    }

    @Bean("sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置扫描 mybatis-config.xml
        sqlSessionFactoryBean.setConfigLocation(null);
        //设置扫描mapper.xml
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:/mybatis/mapper/*Mapper.xml");
        sqlSessionFactoryBean.setMapperLocations(resources);
        //设置扫描实体类
        sqlSessionFactoryBean.setTypeAliasesPackage("nui.honest.spring.demo.profile.rcm");

        return sqlSessionFactoryBean.getObject();
    }

}
package nui.honest.spring.demo.profile;

import nui.honest.spring.demo.jdbc.vo.UserBeanVO;
import nui.honest.spring.demo.profile.rcm.IUserDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * junit测试
 * Created by 15692 on 2019/3/9.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MySqlDataSourceConfig.class})
@ActiveProfiles("dev")
public class MysqlDataSourceTest {

    @Autowired
    private IUserDao userDao;

    @Test
    public void testDataSource() {
        //加载java配置类
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(MySqlDataSourceConfig.class);
        ctx.refresh();
        MySqlDataSourceConfig dataSourceConfig = ctx.getBean("mySqlDataSourceConfig", MySqlDataSourceConfig.class);
        dataSourceConfig.getDataSource();
    }

    @Test
    public void getUser() {
//        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
//        ctx.register(DataSourceConfig.class);
//        ctx.refresh();
//        DataSource dataSource = ctx.getBean("dataSource",DataSource.class);
//        SqlSessionFactory sqlSessionFactory = ctx.getBean("sqlSessionFactory",SqlSessionFactory.class);
//        IUserDao userDao = ctx.getBean("userDao",IUserDao.class);
        UserBeanVO userBeanVO = userDao.queryUserById(1);
        System.out.println(userBeanVO.getUserName());
    }
}


通过样例,profile是通过注解@Profile说明应用环境,再spring初始化时,使用@ActiveProfiles注解激活相应环境使用不同的配置

你可能感兴趣的:(个人学习)