首页 > 代码库 > Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决
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
@Configurationpublic 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
@Configurationpublic 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); }}
Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。