Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决

Posted 博观约取 厚积薄发

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

    }

}

 

以上是关于Spring混合配置时,遇到配置文件路径NotFound,使用PathMatchingResourcePatternResolver解决的主要内容,如果未能解决你的问题,请参考以下文章

Spring xml配置文件相对路径问题

Spring YAML 配置文件配置

Java框架spring Boot学习笔记:bean管理(注解和配置文件混合使用)

spring的bean管理(注解和配置文件混合使用)

将xml和java配置与spring混合

Spring Boot 多环境配置