记一次包扫描的犯错

2018-06-18 01:59:57来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

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层

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:MyBatis(2)——MyBatis 深入学习

下一篇:020.2.5 Calender对象