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 ...
随机推荐
- duilib入门简明教程 -- 第一个程序 Hello World(3)
小伙伴们有点迫不及待了么,来看一看Hello World吧: 新建一个空的win32项目,新建一个main.cpp文件,将以下代码复制进去: #include <windows.h> #i ...
- List_Delete
/*Sorting from little to large use List*/ #include <stdio.h> /* printf, scanf, NULL */ #includ ...
- ORA-14402:更新分区关键字列将导致分区更改
开启行迁移就好了:alter table TABLE_NAME enable row movement; 注意:表分区的时候要确定分区字段是否会UPDATE,如果会的话一定要开启行迁移,否则就会报这个 ...
- 配置Java web的一次经历
最近在完成数据库作业,重新拾起了以前学过的Java,讲下自己的 Java web 配置过程. 1.安装 Tomcat 在官网下载 Tomcat7.0版本:https://tomcat.apache.o ...
- 表单校验--js部分
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- luogu3703 [SDOI2017]树点涂色(线段树+树链剖分+动态树)
link 你谷的第一篇题解没用写LCT,然后没观察懂,但是自己YY了一种不用LCT的做法 我们考虑对于每个点,维护一个fa,代表以1为根时候这个点的父亲 再维护一个bel,由于一个颜色相同的段一定是一 ...
- 在Discuz X 中增加一个单独的页面
如果在DZ中增加一个新的页面,并且取得DZ中相关的用户等乱七八糟的属性,在旧的版本中只要引用一个 -. comm.php 文件就可以,但是在 X 版本以后好像就没.还好,X版本中还是有办法解决的,使用 ...
- 深入解析Close()和Dispose()的区别
很多人都认为Close()方法内部会调用Dispose()方法,所以并没有本质的区别!实际上这个看法不是很准确,对有 些类来说,的确Close()和Dispose()没有本质区别,但是对有些类来说并非 ...
- XAF ORMDataModel构建的基础资料对象无法被调用显示的解决办法
修正,其实只要在基础资料类中加入[XafDefaultProperty("名称")]标签即可. namespace NEO.ERP.Module.BusinessObjects.B ...
- c#Udp分包组包方法
udp通信协议,相信大家都知道这个.由于是无连接的协议,所有udp的传输效率比tcp高.但是udp协议传输较大的数据文件得分包 最近写了个分包组包的方法,拿来和大家分享,如果有什么不妥的地方,欢迎点评 ...