SpringBoot多重属性文件配置方案笔记
SpringBoot多重属性文件配置方案笔记
需要重写PropertyPlaceholderConfigurer
同时要忽略DataSourceAutoConfiguration
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//@EnableTransactionManagement 2. 尝试xml配置事务
@ImportResource(locations = {"classpath:spring-tx.xml"})
public class DemoTransactionApplication { public static void main(String[] args) {
SpringApplication.run(DemoTransactionApplication.class, args);
} }
由于忽略了DataSourceAutoConfiguration,所以要配置datasource的Bean,和sqlSessionFactory 的Bean
@Configuration
@MapperScan(basePackages = {MyBatisConfig.MAPPER_PACKAGE}, sqlSessionFactoryRef = MyBatisConfig.SESSIONFACTORY_NAME)
public class MyBatisConfig { /**SqlSessionFactory名称.*/
public final static String SESSIONFACTORY_NAME = "sqlSessionFactory";
/**mapper包路径,必须与其他SqlSessionFactory-mapper路径区分.*/
public final static String MAPPER_PACKAGE = "com.grady.demotransaction.mapper";
/**mapper.xml文件路径,必须与其他SqlSessionFactory-mapper路径区分.*/
public final static String MAPPER_XML_PATH = "classpath:mapper/*.xml"; @Value("${spring.datasource.url}")
private String datasourceUrl; @Value("${spring.datasource.driver-class-name}")
private String driverClassName; @Value("${spring.datasource.username}")
private String userName; @Value("${spring.datasource.password}")
private String password; @Bean(name = "dataSource")
public DataSource dataSource() {
//建议封装成单独的类
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(datasourceUrl);
dataSource.setDriverClassName(driverClassName);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
} //默认Bean首字母小写,简化配置
//将SqlSessionFactory作为Bean注入到Spring容器中,成为配置一部分。
@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_XML_PATH));
return sqlSessionFactoryBean.getObject();
}
}
这样就可以将本来都写在application.properties中的配置拆成多个了(这里是两个)
application.properties
spring.main.allow-bean-definition-overriding=true
server.port=8080
mybatis.config-location=classpath:config/mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
datasource.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/sampledb
spring.datasource.username=root
spring.datasource.password=123456
SpringBoot多重属性文件配置方案笔记的更多相关文章
- Springboot多属性文件配置
Springboot 多属性文件配置 配置文件后缀有两种: .properties和.yml 要完成多属性配置需要自定义PropertySourcesPlaceholderConfigurer 这个B ...
- 初识spring boot maven管理--属性文件配置
在使用springboot的时候可以使用属性文件配置对属性值进行动态配置,官方文档原文如下: Spring Boot uses a very particular PropertySource ord ...
- 使用外部属性文件配置Bean以及Bean的生命周期方法
1.使用外部属性文件配置Bean 在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean ...
- Spring 使用外部属性文件配置
1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...
- Spring Boot属性文件配置文档(全部)
This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...
- VS2010默认属性文件配置
问题: 在VS2010中,同一个解决方案下有多个项目,都需要使用某一个库. 如果项目比较多,或者编译链接环境属性变动频繁,分别对项目进行配置就很麻烦. 解决: 在VS的配置文件中统一配置属性: 我的配 ...
- SpringBoot多profile文件配置
1.多Profile文件 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml默认使用application.properties的配置: ...
- 黄聪:MYSQL5.6缓存性能优化my.ini文件配置方案
使用MYSQL版本:5.6 [client] …… default-character-set=gbk default-storage-engine=MYISAM max_connections=10 ...
- spring常用的连接池属性文件配置
(一) DBCP 引入jar文件 commons-dbcp-1.2.1.jar commons-pool-1.3.jar二个包. spring配置文件 <bean id="dataSo ...
随机推荐
- Vue.js与ElementUI搭建无限级联层级表格组件
前言 今天,回老家了.第一件事就是回家把大屏安排上,写作的感觉太爽了,终于可以专心地写文章了.我们今天要做的项目是怎么样搭建一个无限级联层级表格组件,好了,多了不多说,赶快行动起来吧!项目一览 到底是 ...
- spring中的bean生命周期
1.实例化(在堆空间中申请空间,对象的属性值一般是默认值.通过调用createBeanInstance()方法进行反射.先获取反射对对象class,然后获取默认无参构造器,创建对象) 2.初始化(就是 ...
- RPA微信机器人汇总
一.微信广告PDF对账单数据提取机器人 [机器人详情] 微信广告对账结算单为PDF文件,从每一期对账单文件中提取结算数据,统计成excel表格,便于与腾讯广告业务结算审核 [机器人步骤] 1.启动机器 ...
- JavaScript扩展原型链浅析
前言 上文对原型和原型链做了一些简单的概念介绍和解析,本文将浅析一些原型链的扩展. javaScript原型和原型链 http://lewyon.xyz/prototype.html 扩展原型链 使用 ...
- Elasticsearch面试题
Elasticsearch面试题 1.Elasticsearch是如何实现master选举的? 1.对所有可以成为master的节点根据nodeId排序,每次选举每个节点都把自己所知道节点排一次序,然 ...
- 机器学习基础:用 Lasso 做特征选择
大家入门机器学习第一个接触的模型应该是简单线性回归,但是在学Lasso时往往一带而过.其实 Lasso 回归也是机器学习模型中的常青树,在工业界应用十分广泛.在很多项目,尤其是特征选择中都会见到他的影 ...
- Eolink 推出面向中小企业及初创企业支持计划,为企业赋能!
2022,疫情持续蔓延,Eolink 作为一家初创公司,深切地感受到疫情下中小企业和初创企业的不易. Eolink 宣布正式推出「 Eolink 微光计划」,面向中小企业和初创企业,提供免费一年的私有 ...
- k8s的部署
一.k8s的二进制部署 1.环境准备: IP 节点 172.16.10.1 k8s-master01 172.16.10.3 ...
- SpringMVC底层——请求参数处理流程描述
在DispatcherServlet.java的doDispatch方法中,springmvc通过handlermapping里面找哪个handler能处理请求,handler封装了目标方法的信息, ...
- ACWing94.递归实现指数型枚举
https://www.acwing.com/problem/content/description/94/ 题面 \92. 递归实现指数型枚举 从 1∼n 这 n 个整数中随机选取任意多个,输出所有 ...