首页 > 代码库 > 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解决