尝试按教程: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下的包路径求教
用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下的包路径求教
解决方案 »
- 为什么验证码的图标突然无法显示了?
- hibernate3里的inner join必须将一对多中的多的一方作为主表来写吗
- jsp判断如何空字符串??
- 关于JavaScript的一个问题 请各位看下
- 巨牛B巨郁闷的关于jsp中数据库连接的问题。(高分)
- 请高手们介绍几本JSP和Servlet好书,小弟刚入门!
- 请教高手了~~ 偶是新新手
- SQLServer 2000 Jdbc执行insert语句错误,请高手指点
- 公告栏
- 数据库中文乱码问题
- IO流 下载的文件 打开报错 提示:发现无法读取的内容 是否恢复此文档内容
- 阿里云上部署springboot项目,在本地tomcat上测试正常,在服务器上测试则会抛异常
不要配置多个标签,要扫描多个包的话加逗号分隔<context:component-scan base-package="com.smart.dao,com.smart.service"/>