spring boot 指定配置文件读取ITeye - AG环亚娱乐

spring boot 指定配置文件读取ITeye

2019-01-12 04:17:37 | 作者: 昌淼 | 标签: | 浏览: 742

package com.ctzj.biz.monitor.persist;
import javax.sql.DataSource;
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.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;
@Configuration
@MapperScan(basePackages={"com.ctzj.biz.monitor.persist"})
public class DataBaseConfig implements EnvironmentAware {

private RelaxedPropertyResolver mybatisPropertyResolver;

@Autowired private DbProperties dbProperties;
@Autowired private MybatisProperties mybatisProperties;

@Override
public void setEnvironment(Environment env) {
this.mybatisPropertyResolver = new RelaxedPropertyResolver(env, "mybatis.");
}

@Bean
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(dbProperties.getUrl());
datasource.setUsername(dbProperties.getUsername());
datasource.setPassword(dbProperties.getPassword());
return datasource;
}

@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}

@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
System.out.println(mybatisPropertyResolver
.getProperty("mapperLocations"));
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver()
.getResources(mybatisPropertyResolver
.getProperty("mapperLocations")));
sessionFactory.setConfigLocation(
new DefaultResourceLoader().getResource(mybatisPropertyResolver
.getProperty("configLocation")));
System.out.println(mybatisPropertyResolver
.getProperty("configLocation"));
return sessionFactory.getObject();
}

@Configuration
@ConfigurationProperties(locations="classpath:mybatis.yml",prefix="jdbc")
static class DbProperties {
private String url;
private String username;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@Configuration
@ConfigurationProperties(locations="classpath:mybatis.yml",prefix="mybatis")
static class MybatisProperties{
private String mapperLocations;
private String configLocation;
public String getMapperLocations() {
return mapperLocations;
}
public void setMapperLocations(String mapperLocations) {
this.mapperLocations = mapperLocations;
}
public String getConfigLocation() {
return configLocation;
}
public void setConfigLocation(String configLocation) {
this.configLocation = configLocation;
}
}

}

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表AG环亚娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章