In the process of changing the XML configuration of spring to Java configuration, some problems have been encountered, and one problem with block time is that the path of the resource (. xml,. properties) is not found, Finally, it is solved by using pathmatchingresourcepatternresolver.
Background: Spring+mybatis
Entrance:
@Configuration @import ({ dalconfig. class xxdbconfig. class = {"Classpath*:spring/applicationcontext.xml", "Classpath*:spring-dao/applicationcontext.xml"}) Publicclass Config { @Bean public Pathmatchingresourcepatternresolver Pathmatchingresourcepatternresolver () { returnNew pathmatchingresourcepatternresolver (); }}
Dalconfig
@Configuration Public classDalconfig {@Bean Publicdaldatasourcefactory Xxdaldatasource () {return Newdaldatasourcefactory (); } @Bean Publicpropertyplaceholderconfigurer Configbean (pathmatchingresourcepatternresolver pathmatchingresourcepattern Resolver)throwsIOException {List<Resource> resources =NewArraylist<>(); Resources.addall (Arrays.aslist (pathmatchingresourcepatternresolver.getresources ("Classpath*:config.properties"))); Resources.addall (Arrays.aslist (pathmatchingresourcepatternresolver.getresources ("Classpath*:/meta-inf/app.properties"))); Propertyplaceholderconfigurer Propertyplaceholderconfigurer=NewPropertyplaceholderconfigurer (); Propertyplaceholderconfigurer.setlocations (Resources.toarray (Newresource[resources.size ()]); Propertyplaceholderconfigurer.setignoreunresolvableplaceholders (true); returnPropertyplaceholderconfigurer; }}
Xxdbconfig
@Configuration Public classXxdbconfig {@Bean PublicDataSource datasourcexxxdb (@Value ("${dbdatacenter}") String Dbdatacenter, @Value ("${cfx_datasource_serviceurl}") String Cfxdatasourceserviceurl, @Value ("${app.id}") String appId, daldatasourcefactory xxxdaldatasource)throwsException {returnXxxxdaldatasource.createdatasource ("XXX" +Dbdatacenter, Cfxdatasourceserviceurl, appId); } @Bean PublicSqlsessionfactorybean sqlsessionfactoryxxxdb (DataSource datasourcexxxdb, PATHMATCHINGRESOURCEP Atternresolver pathmatchingresourcepatternresolver)throwsIOException {Sqlsessionfactorybean Sqlsessionfactorybean=NewSqlsessionfactorybean (); Sqlsessionfactorybean.setdatasource (DATASOURCEXXXDB); Sqlsessionfactorybean.setmapperlocations (Pathmatchingresourcepatternresolver.getresources ("Classpath:com/xx/xxxdb/mapper/**/*.xml")//* * indicates iterative lookups ); returnSqlsessionfactorybean; } @Bean Publicmapperscannerconfigurer mapperscannerconfigurerxxxdb () {mapperscannerconfigurer mapperscannerconfigurer=NewMapperscannerconfigurer (); returnMapperscannerconfigurer; }}
Test
@RunWith (Springjunit4classrunner. Class ) @ContextConfiguration (classes =config. class ) Class dbconfigtest {@Autowired private< /span> ApplicationContext CTX; @Autowired private environment env; @Test public void Checkxxxdb () {mapperscannerconfigurer mapperscannerconfigurerxxxdb = (mapperscannerconfigurer) Ctx.getBean ( "Mapperscannerconfigurerxxxdb" ); Assertnotnull (MAPPERSCANNERCONFIGURERXXXDB); }}
Spring hybrid configuration, when you encounter the configuration file path NotFound, use Pathmatchingresourcepatternresolver to resolve