Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决
在将spring的xml配置改为java配置的过程中,遇到了一些问题,block时间比较长的一个问题是资源(.xml, .properties)的路径找不到,最后是使用PathMatchingResourcePatternResolver解决的。
背景:Spring+MyBatis
入口:
@Configuration
@Import({
DalConfig.class
XXDBConfig.class
})
@ImportResource(locations = {"classpath*:spring/applicationContext.xml", "classpath*:spring-dao/applicationContext.xml"})
public class Config {
@Bean
public PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver(){
return new PathMatchingResourcePatternResolver();
}
}
DalConfig
@Configuration
public class DalConfig {
@Bean
public DalDataSourceFactory xxDalDataSource() {
return new DalDataSourceFactory();
} @Bean
public PropertyPlaceholderConfigurer configBean(
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver) throws IOException {
List<Resource> resources = new ArrayList<>();
resources.addAll(Arrays.asList(pathMatchingResourcePatternResolver.getResources("classpath*:config.properties")));
resources.addAll(Arrays.asList(pathMatchingResourcePatternResolver.getResources("classpath*:/META-INF/app.properties"))); PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setLocations(resources.toArray(new Resource[resources.size()]));
propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
return propertyPlaceholderConfigurer;
}
}
XXDBConfig
@Configuration
public class XXDBConfig { @Bean
public DataSource dataSourceXXXDB(
@Value("${DBDataCenter}") String dbDataCenter,
@Value("${CFX_DataSource_ServiceUrl}") String cfxDataSourceServiceUrl,
@Value("${app.id}") String appId,
DalDataSourceFactory xxxDalDataSource) throws Exception {
return xxxxDalDataSource.createDataSource(
"xxx" + dbDataCenter,
cfxDataSourceServiceUrl,
appId);
} @Bean
public SqlSessionFactoryBean sqlSessionFactoryXXXDB(
DataSource dataSourceXXXDB,
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver) throws IOException {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceXXXDB);
sqlSessionFactoryBean.setMapperLocations(
pathMatchingResourcePatternResolver.getResources("classpath:com/xx/xxxdb/mapper/**/*.xml") //**表示迭代查找
);
return sqlSessionFactoryBean;
} @Bean
public MapperScannerConfigurer mapperScannerConfigurerXXXDB() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
return mapperScannerConfigurer;
}
}
Test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=Config.class)
public class DBConfigTest {
@Autowired
private ApplicationContext ctx; @Autowired
private Environment env; @Test
public void checkXXXDB(){
MapperScannerConfigurer mapperScannerConfigurerXXXDB = (MapperScannerConfigurer)ctx.getBean("mapperScannerConfigurerXXXDB");
assertNotNull(mapperScannerConfigurerXXXDB); } }
由于不同db的代码在一个jar里,通过配置来实现按需初始化db连接
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Conditional(DBConfigLoadCondition.class)
public @interface DBConfigLoadConditional {
String value();
}
public class DBConfigLoadCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (context.getEnvironment() != null) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(DBConfigLoadConditional.class.getName());
if (attrs != null) {
Properties properties = getProperties();
String strDBList = properties.getProperty("db.list");
if(strDBList != null){
String[] arrDBList = strDBList.split(",");
String condition = (String)attrs.getFirst("value");
if(condition == null){
return true;
}
for(String db : arrDBList){
if(db != null && db.trim().equalsIgnoreCase(condition)){
return true;
}
}
}
}
}
return false;
}
private Properties getProperties() {
Properties pro = new Properties();
try{
pro.load(DBConfigLoadCondition.class.getResourceAsStream("/META-INF/app.properties"));
}catch (IOException ex){
ex.printStackTrace();
}
return pro;
}
}
Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决的更多相关文章
- Spring和junit测试之配置文件路径
本人在测试一个方法时需要加载XML配置文件,spring提供了相应的方法,就小小研究了下,在此记录下具体的过程,方便初学者和自己日后回顾. Spring容器最基本的接口就是BeanFactory. B ...
- spring 通过启动命令配置文件路径
公司使用dubbo开发,提供了很多的服务,每个服务中一些配置都是一样的,比如注册中心地址,公共码表库等一下配置,这样在部署每一个程序的时候,修改每一个服务的配置增加很多的工作量.且领导不想对程序有大的 ...
- 三大框架SSH(struts2+spring+hibernate)整合时相关配置文件的模板
最近在学SSH三大框架的整合,在此对他们整合时相关配置文件做一简单的模板总结,方便以后复用! 首先是web.xml配置文件,这里面就配置一些简单的监听器.过滤器,包括spring核心配置文件appli ...
- Spring——ClassPathXmlApplicationContext(配置文件路径解析 1)
ClassPathXmlApplicationContext 在我的 BeanFactory 容器文章中主要提及了 BeanFactory 容器初始化(Spring 配置文件加载(还没解析)) ...
- spring结合时,web.xml的配置
<!-- 1. web.xml配置 <context-param> <param-name>webAppRootKey</param-name> <pa ...
- java spring 等启动项目时的异常 或 程序异常的解决思路
今天搭建ssm项目的时候,因为pagehelper的一个jar包没有导入idea的web项目下的lib目录中,异常报错找不到pagehelper,这个问题在出异常的时候疯狂crash,让人心情十分不舒 ...
- springboot查找配置文件路径的过程
spring加载配置文件是通过listener监视器实现的,在springboot启动时: 在容器启动完成后会广播一个SpringApplicationEvent事件,而SpringApplicati ...
- 使用Spring加载properties配置文件.md
背景 类似于datasource.properties之类的配置文件,最初通过Java的Properties类进行处理.这种方式有许多弊端,如每次都需要读取配置文件:若将Properties作为成员变 ...
- ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址
首先需要JAR包 Spring整合Structs2的JAR包 struts2-spring-plugin-2.3.4.1.jar 下载地址 链接: https://pan.baidu.com/s/1o ...
随机推荐
- [原创]Java源代码学习
一.一些关键字 方法声明中的native:调用本地方法,该方法一般是用C或者C++写的 变量声明中的transient:在序列化过程中会忽略该变量,即不进行序列化保存 变量声明中的volatile:编 ...
- vs2010 在win8附加进程调试小技巧
在win8 附加进程居然找不到 我要的是iis 名为HKFlight的web的进程(下面2个勾也勾上了,就是找不到它)(下图是管理员身份运行截图) 解决方法:打开vs2010 用管理员身份打开...其 ...
- Socket 简易静态服务器 WPF MVVM模式(一)
整体代码下载 主要实现功能: Socket的简单应用 可修改IP和端口 显示来访信息 界面设计: 界面采用MVVM设计,很简陋. 前台的主要目的是 输入IP地址 输入端口 输入文件目录 开启监听和停止 ...
- Redis在window下安装以及配置
一.安装Redis 1.Redis官网下载地址:http://redis.io/download,下载相应版本的Redis,在运行中输入cmd,然后把目录指向解压的Redis目录. 2.启动服务命令 ...
- Mysql数据库自动定时备份软件推荐--MySqlBackupFTP(免费,亲测可用,附使用图示)
MySqlBackupFTP是一款Mysql数据库自动定时备份软件,免费版本就基本上可以满足我们的需求,不需要什么破解版,可直接官网下载安装使用. 先看结果(日志): 软件界面: 可以设定计划任务,每 ...
- 【ARC069F】Flags 2-sat+线段树优化建图+二分
Description 数轴上有 n 个旗子,第 ii 个可以插在坐标 xi或者 yi,最大化两两旗子之间的最小距离. Input 第一行一个整数 N. 接下来 N 行每行两个整数 xi, ...
- python操作RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- Tomcat类加载机制触发的Too many open files问题分析(转)
https://blog.csdn.net/ctrip_tech/article/details/53337137 说起Too many open files这个报错,想必大家一定不陌生.在Linux ...
- SpringMVC返回JSON数据
1.导入json的jar包2.在Controller类中添加 //查看用户信息 ?json //params="json"的意思是访问view这个方法的时候,必须有一个参数json ...
- C# winform间窗体传值简单Demo
form1是用来接收值的 using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...