在将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. Microsoft.Office.Interop.Word.DocumentClass.SaveAs 命令失败

    asp.net 常用的生成word功能,代码也是网上常见的,自己本地反复测试过没问题.serves 2003下运行没问题,可是发布到2008上就出错.组件权限已配置,windows目录下temp权限已 ...

  2. 在C#中使用科大讯飞Web API进行语音合成

    最近工作中需要用到讯飞语音合成接口,网上看了下基本都是Java,PHP,Python版本的,正好补上C# 版本,代码比较简单.  首先在讯飞开放平台上创建一个WebApi项目,取到APPID与APIK ...

  3. sqlserver中 多条数据合并成一条数据 (stuff 与 for xml path 连用)

    SQL 列转行,即多行合并成一条   需求:按照分组,将多条记录内容合并成一条,效果如下: 数据库示例: CREATE TABLE [t2]([NID] [bigint] NULL,[district ...

  4. go 常见问题

    以下是我在go项目中碰到问题 1. 如何只测试指定的test文件,而不是所有的单元测试都跑一遍. go tool vet -test -v src\github.com\astaxie\beego\c ...

  5. MultiTrigger

    MultiTrigger是多条件触发器.意为多个条件同时满足时才会触发. 用法和Trigger差不多. 但是MultiTrigger的条件是在写在自身的判断环境之中. 基本的语法是: <Mult ...

  6. 【leetcode 144. 二叉树的前序遍历】解题报告

    前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> preorderTraversal(TreeNode* roo ...

  7. Django + Gunicorn + Nginx 部署 Ubuntu 服务器

    Django + Gunicorn + Nginx 部署服务器 获取腾讯云 root权限 本人的服务器使用的是腾讯云,腾讯云默认是没有开放 root 用户的,我们来创建 root 用户. 创建 roo ...

  8. Laravel-安装composer

    一.系统环境   Laravel框架有些系统上的要求,因此需要保证自己运行环境.要求的环境有:对于PHP的版本要求比较法高,其他的是扩展,可以在php.ini文件中开启 PHP >= 5.5.9 ...

  9. [Swift实际操作]九、完整实例-(3)创建和安装开发证书、发布证书及开发证书配置文件、发布证书配置文件

    本文将为你演示,如何创建开发证书和发布证书,以及其他辅助内容.首先打开浏览器,进入[苹果开发者网站]输入[Apple ID]和[密码],点击登录按钮,进入开发者管理后台. 点击左侧的[Membersh ...

  10. LVS解决高并发,大数据量

    http://www.360doc.com/content/14/0726/00/11962419_397102114.shtml LVS的全称Linux vitual system,是由目前阿里巴巴 ...