码迷,mamicode.com
首页 > 其他好文 > 详细

记一次包扫描的犯错

时间:2018-04-18 16:27:44      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:map   com   repo   one   artifact   sql   div   apach   sep   

很长时间对于无法注入依赖的问题很是苦恼,对于类似的问题我做出如下总结:

1.对于分布式架构来说

  如果提示无法注入依赖,就考虑没有引用哪个工程,对于war包来说要全部引用,举个栗子:

    

<?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>

对于这样的没有war包的分布式,你只需引用你所需要的工程。

 

  对于war包来说要全部引用:

<?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.对于某一个层面(controller,service,dao,mapper)来说,无法注入依赖:

  1.   检查每个层面有没有加注解,@Controller,@Service,@Repository
  2.   检查有没有添加包扫描
        <!-- 注解驱动 -->
        <mvc:annotation-driven/>
        <!-- 自动扫描的包 -->
        <context:component-scan base-package="com.pyq.weixin"/>  

  3.   特别对于mapper层,需要注意一个问题,mybatis中对应的mapper接口无法注入,原因可能是没有扫描到此mapper

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

    <!-- 正确的扫描mapper(dao)包 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.pyq.**.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    由于一个项目中的mapper层不止一个,所以需要扫描到所有的mapper层,需要用到通配符 com.pyq.**.mapper指的是pyq下面所有的mapper层

 

记一次包扫描的犯错

标签:map   com   repo   one   artifact   sql   div   apach   sep   

原文地址:https://www.cnblogs.com/pangyangqi/p/8875577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!