spring boot 指定配置文件读取

1.获取Environment对象

2.spring boot读取指定的yaml文件

写道
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;
}
}

}

 

展开阅读全文

没有更多推荐了,返回首页