1. 程式人生 > >記一次包掃描的犯錯

記一次包掃描的犯錯

架構 bus 長時間 fig nfa mvc 提示 property prope

很長時間對於無法註入依賴的問題很是苦惱,對於類似的問題我做出如下總結:

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層

記一次包掃描的犯錯