Locating configuration resources with tomcat6 and

2019-09-03 15:09发布

问题:

I have a legacy web application using the following setup:

  • Spring 3.1.0
  • Mybatis 3.0.3
  • Tomcat 6.0.43

The project deploys a web application (.war) with multiple maven modules (i.e. .jar dependencies). Now I am struggling to let Tomcat detect mybatis mapper .xml's both in the application and the dependencies.

I have the following definition:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="classpath*:*-mappers.xml" />
</bean>

This works fine for detecting the .xml's in the module that is being built as .war. If I replace this with the absolute path to the .xml resources in the .jar, this successfully detects these other resources:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mapperLocations" value="jar:file:/my-repository/my-project/my-app/target/my-app-1.0.0/WEB-INF/lib/my-dependency.jar!/*-mappers.xml" />
</bean>

However, this formulation is way too explicit in that it needs to point both to the path to my dependencies in the deployed application, and the dependency name as well. Furthermore, this second formulation excludes the resources found in the main web application.

So my question is: how should I define my mapperLocations so that it can find the .xml resources both in the .war application and the dependent .jar's, or a full list of dependencies.

I'm debugging the contents of the array that on startup arrives as a parameter into org.mybatis.spring.SqlSessionFactory.setMapperLocations(Resources[]). This allows me to see what mybatis mapper .xml's are being detected.

A few further details:

  • Te first generic classpath reference successfully detects both resource locations with tomcat8.
  • Even with tomcat6, classes from the .jar dependency are loaded fine. However, apparently tomcat6 has a different way of resolving resources from the classpath.

Update: I've seen that PathMatchingResourcePatternResolver does not tokenise the value of mapperLocations, thus providing multiple values will not be parsed successfully.

Update2: I've extracted the related question of how to make the path pattern as a relative path in a separate question.