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;
}
}
}
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;
}
}
}
相关推荐
我们使用Spring Boot编写多个模块开发时,我们希望各个模块使用各自的配置文件,避免将整个项目的配置文件都写在一起,从而降低各个模块之间的耦合度。
Spring Boot读取配置文件常用方式
spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,spring boot配置文件参数详解,
本资源对spring-boot中的核心配置文件进行详细介绍,具体内容参考博客:http://blog.csdn.net/pengjunlee/article/details/79640361,包括:核心配置文件介绍,读取配置,动态加载配置等。
1.13 Spring Boot配置ContextPath 1.14 Spring Boot改变JDK编译版本 1.15 处理静态资源(默认资源映射) 1.16 处理静态资源(自定义资源映射) 1.17 Spring Boot定时任务的使用 1.18 Spring Boot使用Druid和监控配置 ...
config|[Spring Cloud Alibaba(二)配置中心多项目、多配置文件、分目录实现](https://github.com/smltq/spring-boot-demo/blob/master/cloud-alibaba/README2.md) Sentinel|[Spring Cloud Alibaba(三)Sentinel...
主要介绍了详解Spring Boot读取配置文件与配置文件优先级,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Spring Boot的properties配置文件读取,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
3)配置加载配置文件的读取,开启注解 4)配置日志文件 … n) 配置完成之后部署 tomcat 调试 可能你还需要考虑各个版本的兼容性,jar 包冲突的各种可行性。 那么使用 Spring Boot 之后我们需要开发一个 web ...
3)配置加载配置文件的读取,开启注解 4)配置日志文件 … n) 配置完成之后部署 tomcat 调试 可能你还需要考虑各个版本的兼容性,jar 包冲突的各种可行性。 那么使用 Spring Boot 之后我们需要开发一个 web ...
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件4
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件3
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件1
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件5
Spring Boot技术知识点:如何读取不同路径里的applicationContext.xml配置文件6
主要介绍了Spring Boot中配置文件application.properties使用及spring boot读取application.properties文件的方式,需要的朋友参考下吧
主要介绍了详解Spring-boot中读取config配置文件的两种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本篇文章主要介绍了spring boot加载第三方jar包的配置文件的方法,详细的介绍了spring boot jar包配置文件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Spring Boot技术知识点:如何获取application.yml配置文件里的相关属性(方法2)