Portability issues with classpath* in spring

Source: Internet
Author: User

classpath* portability is a problem that many people should have encountered. Here is an example (using spring4.1.5 and mybatis3.2.8):

    class= "Org.mybatis.spring.SqlSessionFactoryBean" >      <property name= "DataSource" ref= "DataSource "/>      <property name=" configlocation "value=" Classpath*:config/mybatis-config-master.xml "/>      < Property Name= "Mapperlocations" value= "Classpath*:config/mappers/master/**/*.xml"/>    </bean>

The above configuration, in the Start times error:

 caused by:java.io.FileNotFoundException:Could not open ServletContext resource [/classpath*:config/ Mybatis-config-master.xml "at Org.springframework.web.context.support.ServletContextResource.getInputStream (Servletcontextresource.java:  141) at Org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory ( Sqlsessionfactorybean.java:  358) at Org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet (Sqlsessionfactorybean.java:  340) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods ( Abstractautowirecapablebeanfactory.java:  1625) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean ( Abstractautowirecapablebeanfactory.java:  1562 More 

Spring constructs sqlsessionfactory this bean-times error because the configuration file could not be found:

Could not open ServletContext resource [/classpath*:config/mybatis-config-master.xml]

The path that spring finds is:/classpath*:config/mybatis-config-master.xml

And this path is not what we want at all, is to find the wrong place.

But the path given by our configuration file is: Classpath*:config/mybatis-config-master.xml

We will modify the configuration file in the following configuration to remove the classpath behind the *

<property name= "configlocation" value= "Classpath*:config/mybatis-config-master.xml"/>

Switch

<property name= "configlocation" value= "Classpath:config/mybatis-config-master.xml"/>

After, the boot is normal, no error.

reason :

The wildcard Classpath relies on the getresources () method of the underlying Clas Sloader . As most application servers nowadays supply their own classloader implementation, the behavior might differ Especi Ally when dealing with jar files . A simple test to check if classpath* works are to use the ClassLoader to load a file from with In a jar on the classpath: getclass (). getClassLoader (). Getresources ("<somefileinsidethejar > ") . Try this test with files, that has the same name but is placed inside the different locations. In case a inappropriate result is returned, check the application Server documentation for settings that might affect the ClassLoader behavior.

There is a portability problem with different ClassLoader ClassLoader when dealing with classpath*. If you use the classpath* times wrong, then should be removed * Direct use of classpath:, if the error, then you can remove the Classpath direct use of the path, on the StackOverflow there is an example:/http Stackoverflow.com/questions/6035464/could-not-open-servletcontext-resource

the difference between classpath* and classpath :

classpath* prefix specifies that all classpath Resources This match the given name must be obtained< /c3> (Internally, this essentially happens via a call ClassLoader.getResources(...) ), and then merged to form the final application context Defini tion.

1) classpath* It will search all the classpath, find all eligible files, including the configuration file in the jar file. The classpath does not look in the jar file.

2) classpath* There is a portability problem, you should use classpath when you encounter problems.

3) In fact, in general we do not need to use classpath*, direct use of classpath just fine.

Portability issues with classpath* in spring

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.