component-scan扫描不到其他module下的包路径

昭湖洗市 2019-10-21 05:35:54
尝试按教程:Spring+Spring MVC+MySQL的简单开发
用IDEA+SpringMVC+Tomcat+MySQL做一个登录demo。
但是配置ApplicationContext文件的时候,使用<context:component-scan base-package="******"/>扫描时无法识别其他模块下的类。
于是登录一直出错

项目结构:


smart-context.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

<!--扫描类包,将标注为注解的类自动转化为Bean,同时完成Bean的注入--> smart 、dao、service无法识别
<context:component-scan base-package="com.smart.dao"/>

<context:component-scan base-package="com.smart.service"/>

<!-- dataSource 配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" primary="true">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!-- mysql6.0.x以上需要指定时区 -->
<property name="url" value="jdbc:mysql://localhost:3306/sampledb?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>

<!-- 引入jdbc配置文件 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod" expression="(execution(* com.smart.service..*(..))) and
(@annotation(org.springframework.transaction.annotation.Transactional))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
</beans>


smart-servlet.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.smart.web"/>
smart和web两个包都无法识别

<!--配置视图解析器,将ModelAndView及字符串解析为具体页面-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/jsp/" /> prefix和sufix两个属性也无法识别
<property name="suffix" value=".jsp" />
</bean>
</beans>


查到了一个解决办法,但是不太明白:component-scan扫描不到其他module下的包路径

求教
...全文
1750 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
_沉浮_ 2020-01-02
  • 打赏
  • 举报
回复
是有什么错误提示你没有扫描到包,还是你的主观判断
TTTiaotiao 2020-01-01
  • 打赏
  • 举报
回复
你好,解决了吗,我也是扫描不到。com下面就扫描不到了
昭湖洗市 2019-10-23
  • 打赏
  • 举报
回复
引用 2 楼 weixin_43307865 的回复:
<context:component-scan base-package="com.smart"/>这样配置就行了 不要配置多个标签,要扫描多个包的话加逗号分隔<context:component-scan base-package="com.smart.dao,com.smart.service"/>
没有用,我从smart包开始就扫描不出来了
昭湖洗市 2019-10-22
  • 打赏
  • 举报
回复
又写了个别的……还是扫描不到包
瘦死的黑骆驼 2019-10-22
  • 打赏
  • 举报
回复
<context:component-scan base-package="com.smart"/>这样配置就行了 不要配置多个标签,要扫描多个包的话加逗号分隔<context:component-scan base-package="com.smart.dao,com.smart.service"/>

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧