在将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; } }
转载于:https://www.cnblogs.com/liqipeng/p/6833414.html