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 ...
随机推荐
- openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
我们目前大部分使用的openssl库还是基于TLS1.2协议的1.0.2版本系列,如果要支持更高的TLS1.3协议,就必须使用openssl的1.1.1版本或3.0版本.升级openssl库有可能会导 ...
- Web思维导图实现的技术点分析(附完整源码)
简介 思维导图是一种常见的表达发散性思维的有效工具,市面上有非常多的工具可以用来画思维导图,有免费的也有收费的,此外也有一些可以用来帮助快速实现的JavaScript类库,如:jsMind.KityM ...
- NC224938 加减
NC224938 加减 题目 题目描述 小红拿到了一个长度为 \(n\) 的数组.她每次操作可以让某个数加 \(1\) 或者某个数减 \(1\) . 小红最多能进行 \(k\) 次操作.她希望操作结束 ...
- 深入理解Apache Hudi异步索引机制
在我们之前的文章中,我们讨论了多模式索引的设计,这是一种用于Lakehouse架构的无服务器和高性能索引子系统,以提高查询和写入性能.在这篇博客中,我们讨论了构建如此强大的索引所需的机制,异步索引机制 ...
- Java 中的对象池实现
点赞再看,动力无限.Hello world : ) 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. 最近在 ...
- 软件测试—Day2
day2 Q:面试过程中,性能测试你测试什么?关注的点是什么? A:程序的响应时间,系统的吞吐量,以及并发用户数,和tps,qps,以及DB的IOPS,和服务器的系统资源(CPU和内存).通过一定的工 ...
- 2022-07-15/16 第一小组 田龙月 管理系统javaSE
JavaSE小项目(基础语法:二分查找:冒泡排序)--还是存在bug:删除一个数组内一组数据后面只有一组后面数据能向前移位 (YY:使用"方法"应该会好很多,代码架构会清晰一点)未 ...
- Java中修饰符的分类及用法
访问权限修饰符: public 修饰class,方法,变量: 所修饰类的名字必须与文件名相同,文件中最多能有一个pulic修饰的类. private class不可用,方法,变量可以用: 只限于本类成 ...
- 【Go语言】(一)环境搭建与了解VScode工具
视频链接(p1~p8): golang入门到项目实战 [2022最新Go语言教程,没有废话,纯干货!] 参考链接: 用vscode开发go的时候,安装go包报错:connectex: A connec ...
- 【新人福利】使用CSDN 官方插件,赠永久免站内广告特权 >>电脑端访问:https://t.csdnimg.cn/PVqS
[新人福利]使用CSDN 官方插件,赠永久免站内广告特权 >>电脑端访问:CSDN开发助手 [新人福利]使用CSDN 官方插件,赠永久免站内广告特权 >>电脑端访问:https ...