在将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解决的更多相关文章

  1. Spring和junit测试之配置文件路径

    本人在测试一个方法时需要加载XML配置文件,spring提供了相应的方法,就小小研究了下,在此记录下具体的过程,方便初学者和自己日后回顾. Spring容器最基本的接口就是BeanFactory. B ...

  2. spring 通过启动命令配置文件路径

    公司使用dubbo开发,提供了很多的服务,每个服务中一些配置都是一样的,比如注册中心地址,公共码表库等一下配置,这样在部署每一个程序的时候,修改每一个服务的配置增加很多的工作量.且领导不想对程序有大的 ...

  3. 三大框架SSH(struts2+spring+hibernate)整合时相关配置文件的模板

    最近在学SSH三大框架的整合,在此对他们整合时相关配置文件做一简单的模板总结,方便以后复用! 首先是web.xml配置文件,这里面就配置一些简单的监听器.过滤器,包括spring核心配置文件appli ...

  4. Spring——ClassPathXmlApplicationContext(配置文件路径解析 1)

    ClassPathXmlApplicationContext     在我的 BeanFactory 容器文章中主要提及了 BeanFactory 容器初始化(Spring 配置文件加载(还没解析)) ...

  5. spring结合时,web.xml的配置

    <!-- 1. web.xml配置 <context-param> <param-name>webAppRootKey</param-name> <pa ...

  6. java spring 等启动项目时的异常 或 程序异常的解决思路

    今天搭建ssm项目的时候,因为pagehelper的一个jar包没有导入idea的web项目下的lib目录中,异常报错找不到pagehelper,这个问题在出异常的时候疯狂crash,让人心情十分不舒 ...

  7. springboot查找配置文件路径的过程

    spring加载配置文件是通过listener监视器实现的,在springboot启动时: 在容器启动完成后会广播一个SpringApplicationEvent事件,而SpringApplicati ...

  8. 使用Spring加载properties配置文件.md

    背景 类似于datasource.properties之类的配置文件,最初通过Java的Properties类进行处理.这种方式有许多弊端,如每次都需要读取配置文件:若将Properties作为成员变 ...

  9. 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 ...

随机推荐

  1. c# 求数组的最大值

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 《Andorid开源》greenDao 数据库orm框架

       一 前言:以前没用框架写Andorid的Sqlite的时候就是用SQLiteDatabase ,SQLiteOpenHelper ,SQL语句等一些东西,特别在写SQL语句来进行 数据库操作的时 ...

  3. 「TJOI2015」线性代数

    题目链接 戳我 \(Describe\) 题目描述 为了提高智商,\(ZJY\)开始学习线性代数.她的小伙伴菠萝给她出了这样一个问题:给定一个\(n×n\)的矩阵\(B\)和一个\(1×n\)的矩阵\ ...

  4. [SinGuLaRiTy] 复习模板-数据结构

    [SinGuLaRiTy-1040] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 二维线段树 2D_Segment_Tree //示例:单 ...

  5. Spring之BeanFactory与ApplicationConText

    :BeanFactory基本的工厂解析,管理,实例化所有容器内的bean的接口,spring中所有解析配置文件的类都直接或者间接实现该接口ApplicationContext接口implements ...

  6. MATLAB版本(2012b 64bit),在尝试调用svmtrain函数时报错

    问题:MATLAB版本(2012b 64bit),在尝试调用svmtrain函数时报错: 解决方案:参照https://blog.csdn.net/TIME_LEAF/article/details/ ...

  7. (转)2-SAT小结

    2-sat小结 原文作者:老K 原文传送门 2-sat是什么 一类问题是这样的: (两个符号的意思 \(\lor \ or,\land \ and\)) 有n个布尔变量,现在对它们做出限制,比如\(a ...

  8. 在DZ 中 showmessage 中可以再次执行 JS

    showmessage ( '登录', '', array (), array (                         'showdialog' => 0,              ...

  9. 3 hql语法及自定义函数(含array、map讲解) + hive的java api

    本博文的主要内容如下: .hive的详细官方手册    .hive支持的数据类型   .Hive Shell .Hive工程所需依赖的jar包  .hive自定义函数 .分桶4   .附PPT hiv ...

  10. 移动像素的px ,dp/pt,dpr的关系

    一:基础知识 px:逻辑像素,浏览器使用的抽象单位 dp,pt:设备无关像素 也叫物理像素 dpr:devicePixelRatio 设备像素缩放比 计算公式:1px=(dpr)^2*dp; ipho ...