用了大半年的Spring MVC3.0,用着感觉不错。简单写一个搭建Spring MVC3.0的流程(以Spring3.0.5为列),数据库交互使用spring JDBC Template,附件有项目(没有jar包)。整个项目架构如下图所示:
1、去官网下载3.0.5所有jar包,所需jar包,见附件图片,每个jar包得用处如下:
org.springframework.aop- 3.0.0.RELEASE--------------------Spring的面向切面编程,提供AOP(面向切面编程)实现
org.springframework.asm- 3.0.0.RELEASE--------------------Spring独立的asm程序,相遇Spring2.5.6的时候需要asmJar 包.3.0开始提供他自己独立的asmJar
org.springframework.aspects- 3.0.0.RELEASE----------------Spring提供对AspectJ框架的整合\
org.springframework.beans- 3.0.0.RELEASE------------------SpringIoC(依赖注入)的基础实现
org.springframework.context.support- 3.0.0.RELEASE--------Spring-context的扩展支持,用于MVC方面
org.springframework.context- 3.0.0.RELEASE----------------Spring提供在基础IoC功能上的扩展服务,此外还提供许多企业级服务的支持,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存以及各种视图层框架的封装等
org.springframework.core- 3.0.0.RELEASE-------------------Spring3.0的核心工具包
org.springframework.expression- 3.0.0.RELEASE-------------Spring表达式语言
org.springframework.instrument.tomcat- 3.0.0.RELEASE------Spring3.0对Tomcat的连接池的集成
org.springframework.instrument- 3.0.0.RELEASE-------------Spring3.0对服务器的代理接口
org.springframework.jdbc- 3.0.0.RELEASE-------------------对JDBC的简单封装
org.springframework.jms- 3.0.0.RELEASE--------------------为简化JMS API的使用而作的简单封装
org.springframework.orm- 3.0.0.RELEASE--------------------整合第三方的ORM框架,如hibernate,ibatis,jdo,以及spring的JPA实现
org.springframework.oxm-3.0.0.RELEASE--------------------Spring 对Object/XMl的映射支持,可以让Java与XML之间来回切换
org.springframework.test- 3.0.0.RELEASE--------------------对Junit等测试框架的简单封装
org.springframework.transaction- 3.0.0.RELEASE-------------为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理
org.springframework.web.portlet- 3.0.0.RELEASE-------------SpringMVC的增强
org.springframework.web.servlet- 3.0.0.RELEASE-------------对JEE6.0 Servlet3.0的支持
org.springframework.web.struts- 3.0.0.RELEASE--------------整合Struts的时候的支持
org.springframework.web- 3.0.0.RELEASE--------------------SpringWeb下的工具包
2、借鉴spring官网写法,建立一个src-resources Source Folder,再新建目录META-INF,存放springmvc-servlet.xml和jdbc-context.xml文件(事务和数据库连接池的管理);以及database.properties和log4j.properties。
JDBC-context.xml文件:
- <span style="font-size: x-small;">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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
-
- <context:property-placeholder location="classpath:database.properties"/>
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="${mysql.driverclass}">property>
- <property name="jdbcUrl" value="${mysql.jdbcurl}">property>
- <property name="user" value="${mysql.user}">property>
- <property name="password" value="${mysql.password}">property>
- <property name="acquireIncrement" value="5">property>
- <property name="initialPoolSize" value="10">property>
- <property name="minPoolSize" value="5">property>
- <property name="maxPoolSize" value="20">property>
-
- <property name="maxStatements" value="20">property>
- bean>
-
- <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource">property>
- bean>
-
-
- <aop:config>
- <aop:advisor pointcut="execution(* com.aokunsang.service.impl.*ServiceImpl.*(..))" advice-ref="myAdvice"/>
- aop:config>
- <tx:advice id="myAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="add*" propagation="REQUIRED"/>
- <tx:method name="update*" propagation="REQUIRED"/>
- <tx:method name="*" read-only="true" rollback-for="com.aokunsang.util.DaoException"/>
- tx:attributes>
- tx:advice>
-
-
- <context:component-scan base-package="com.aokunsang">
- <context:exclude-filter type="regex" expression="com.aokunsang.web.*"/>
- context:component-scan>
-
- beans>span>
springmvc-servlet.xml文件:
- <span style="font-size: xx-small;">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:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
-
-
- <context:component-scan base-package="com.aokunsang.web"/>
-
-
- <mvc:interceptors>
- <mvc:interceptor>
- <mvc:mapping path="/user/MyHome"/>
- <mvc:mapping path="/usermanager/*"/>
- <bean class="com.aokunsang.web.interceptor.MyInterceptor">bean>
- mvc:interceptor>
- mvc:interceptors>
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/views/">property>
- <property name="suffix" value=".jsp">property>
- bean>
-
- beans>span>
3、修改web.xml文件如下: