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

昭湖洗市 昭湖洗市 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
尝试按教程: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下的包路径

求教
...全文
评论 5
收藏 已收藏
分享
返回顶部
昭湖洗市
Web 开发 81,108 成员 341,721 内容