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 ...
随机推荐
- linux安装源码包指定安装目录
当下载完一个源码包并且解压后 文件夹下会有一个重要的文件configure configure 文件是一个可执行的脚本文件,它将检查目标系统的配置和可用功能,比如一些检查依赖或者启用禁用一些模块,它有 ...
- SAP 文件操作类 CL_GUI_FRONTEND_SERVICES
1 .文件下载. DATA: l_filename TYPE string, "file name l_path TYPE string, "file path l_fullpat ...
- kvm虚拟机在线扩容
fdisk -l查看当前虚拟机磁盘容量 1. 镜像扩容 先操作镜像,给镜像增加2T容量: 关闭虚拟机back_log,然后再宿主机上给虚拟机扩容 qemu-img info /home/kvm/bac ...
- SpringBoot项目启动org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException解决方法
将Pom文件中的SpringBoot版本调低即可. 我的是调成了2.5.6
- POI 给单元格添加批注
图中红框框是处理单元格内容和批注的地方. 参考:https://blog.csdn.net/qq_38974638/article/details/114837631 //SXSSFWorkbook ...
- 9.4 苹果macOS电脑如何安装Android开发环境(Android Studio)
下载 来到官方下载界面(需要 科 学 上 网),下载最新版本,点击Download,然后同意协议,在点击下载:如果平常看文档,可以点击Google中国Android开发者官网(部分用户可能也需要科 学 ...
- 使用纯 CSS 实现超酷炫的粘性气泡效果
最近,在 CodePen 上看到这样一个非常有意思的效果: 这个效果的核心难点在于气泡的一种特殊融合效果. 其源代码在:CodePen Demo -- Goey footer,作者主要使用的是 SVG ...
- 2m高分辨率土地利用分类数据
数据下载链接:百度云下载链接 土地利用数据是在根据影像光谱特征,结合野外实测资料,同时参照有关地理图件,对地物的几何形状,颜色特征.纹理特征和空间分布情况进行分析,建立统一解译标志的基础之上,依据多源 ...
- javascript基本属性访问对象的属性和方法
var myName = "Shelley"; //字符串基本类型 alert(myName.length); //隐式创建String对象,数值与myName相同,并执行len ...
- 常用Linux音译
su:Swith user 切换用户,切换到root用户 cat: Concatenate 串联 uname: Unix name 系统名称 df: Disk free 空余硬盘 du: Disk u ...