Remember a packet scan error

Keywords: Java Maven Apache Mybatis xml

For a long time, I was very distressed about the problem of unable to inject dependency. For similar problems, I made the following summary:

1. For distributed architecture

If the prompt indicates that the dependency cannot be injected, consider which project is not referenced. For the war package, all references should be made, for example:

    

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>tms</groupId>
        <artifactId>tms-parent</artifactId>
        <version>1.0.0</version>
    </parent>
    <artifactId>tms-yinliu</artifactId>
    <name>tms-yinliu</name>
    <dependencies>
        <dependency>
            <artifactId>tms-util</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-api</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

For such a distributed system without war package, you only need to refer to the project you need.

 

For the war package, to refer to all:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>tms</groupId>
        <artifactId>tms-parent</artifactId>
        <version>1.0.0</version>
    </parent>
    <artifactId>tms-yinliu-war</artifactId>
    <packaging>war</packaging>
    <name>tms-yinliu-war</name>
    <dependencies>
        <dependency>
            <artifactId>tms-number</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-order</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-user</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-info</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-tk</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-data</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-mobile</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-contract</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-weixin</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-product</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-company</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-yinliu</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <artifactId>tms-business</artifactId>
            <groupId>tms</groupId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</project>

 

 

2. For a certain level (controller,service,dao,mapper), dependency cannot be injected:

  1. Check whether there is annotation on each level, @ Controller,@Service,@Repository
  2. Check if package scanning is added
        <! -- Annotation driven -- >
        <mvc:annotation-driven/>
        <! -- auto scanned packages -- >
        <context:component-scan base-package="com.pyq.weixin"/>  

3. Especially for the mapper layer, we need to pay attention to the problem that the corresponding mapper interface in mybatis cannot be injected because it is not scanned to this mapper

 <!--FALSE -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.pyq.weixin.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- Correct scanning mapper(dao)package -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.pyq.**.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

Since there is more than one mapper layer in a project, you need to scan all mapper layers. You need to use wildcard com.pyq.**.mapper refers to all mapper layers under PYQ

Posted by shams on Wed, 01 Apr 2020 11:06:51 -0700