Spring hybrid configuration, when you encounter the configuration file path NotFound, use Pathmatchingresourcepatternresolver to resolve

Source: Internet
Author: User
Tags addall

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.