When spring is configuring Mybatis, multiple relevant configuration files of MyBatis need to be loaded. Among them, the XML corresponding to Mapper of MyBatis is usually placed in other JAR packages. Mybatis -conf file is usually in the current project, so, which leads to the problems encountered today. So what’s the difference between classpath* and classpath?

Incorrect configuration and exceptions seen

  1. The configuration in the configuration file looks fine

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceM"/>
        <property name="mapperLocations" value="classpath*:/mappings/*.xml"/>
        <property name="configLocation" value="classpath*:/spring/mybatis-config.xml"></property>
    </bean>
    Copy the code
  2. Exception seen after starting the server

    Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/classpath*:/spring/mybatis-config.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(AbstractAutowireCapableBe anFactory.java:1687) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanF actory.java:1624) ... 61 moreCopy the code
  3. Analyze exceptions and solve problems

As you can see from the above exception, the file is obviously not found, but why?

As you can see from the exception, The path that Spring looks for is:

However, this path is not the path we want, obviously in the wrong place. But the path given by our configuration file is classpath*:/spring/mybatis-config.xmlCopy the code

We modified the following configuration file slightly to remove the * after classpath

<property name="configLocation" value="classpath*:/spring/mybatis-config.xml"></property>
Copy the code

To:

<property name="configLocation" value="classpath*:/spring/mybatis-config.xml"></property>
Copy the code

After that, the startup is normal, no error is reported, and the problem is solved. classpath* Here’s why:

Classpath * differs from Classpath:

  1. Classpath * It searches all classpath and finds all files that match the criteria, including configuration files in the JAR files that the current project depends on. The classpath does not look for jar files that the current project depends on.

  2. Classpath * has portability issues, and classpath should be used when you encounter problems.

  3. In most cases you don’t need to use classpath*, just use classpath.

extra

LIGHTCONF is a configuration management platform based on Netty implementation. Its core design goal is to “provide unified configuration management services for business”, which can be done out of the box.

  • Netty – based lightweight distributed application configuration center