记一次包扫描的犯错
Posted 左手程序,右手诗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记一次包扫描的犯错相关的知识,希望对你有一定的参考价值。
很长时间对于无法注入依赖的问题很是苦恼,对于类似的问题我做出如下总结:
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)来说,无法注入依赖:
- 检查每个层面有没有加注解,@Controller,@Service,@Repository
- 检查有没有添加包扫描
<!-- 注解驱动 --> <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层
以上是关于记一次包扫描的犯错的主要内容,如果未能解决你的问题,请参考以下文章