本文源码: GitHub. 点这里 https://github.com/cicadasmile/spring-mvc-parent || GitEE. 点这里 https://gitee.com/cicadasmile/spring-mvc-parent
一, Spring 框架
1, 框架简介
Spring 是一个开源框架, 框架的主要优势之一就是其分层架构, 分层架构允许使用者选择使用哪一个组件, 同时为 J2EE 应用程序开发提供集成的框架. Spring 使用基本的 JavaBean 来完成以前只可能由 EJB 完成的事情. 然而, Spring 的用途不仅限于服务器端的开发. 从简单性, 可测试性和松耦合的角度而言, 任何 Java 应用都可以从 Spring 中受益. 简单来说, Spring 是一个分层的轻量级开源框架.
2, 优点分析
1), 分层架构
一站式, 每一个层都提供的解决方案
web 层: struts,spring-MVC
service 层: spring
dao 层: hibernate,mybatis,jdbcTemplate,JPA
2), 轻量级
依赖资源少, 销毁的资源少.
3), 高内聚低耦合
Spring 就是一个大容器, 可以将所有对象创建和依赖关系统一维护, 交给 Spring 管理.
4),AOP 编程的支持
Spring 提供面向切面编程, 可以方便的实现对程序进行权限拦截, 运行监控等功能.
5), 事务的支持
只需要通过配置就可以完成对事务的管理, 而无需手动编程
6), 集成测试
Spring 对 Junit4 支持, 可以通过注解方便的测试 Spring 程序.
7), 降低 API 的使用难度
Spring 对 JavaEE 开发中非常难用的一些 API(JDBC,JavaMail, 远程调用等), 都提供了封装, 使这些 API 应用难度大大降低
8), 集成各种框架
Spring 不排斥各种优秀的开源框架, 其内部提供了对各种优秀框架的集成, 如: Struts,Hibernate,MyBatis 等.
二, 核心组件分析
1, 核心容器
容器是 Spring 框架的核心模式, 该模块包含 Bean 的创建, 配置, 管理等功能.
2,AOP 编程
AOP 编程可以帮助应用程序解耦, 使用 AOP 编程模式, 可以把系统中的核心点从对象方法中解耦, 统一管理.
3, 数据访问
该模块集成了 JDBC, 解决 JDBC 开发模式导致的大量代码冗余, 集成常用的 Dao 层框架, hibernate,mybatis 等, 使开发环境的搭建更加便捷.
4,Web 编程
Spring 不仅集成各种流程的 MVC 框架, 还自带 springmvc 强大的框架, 有助实现界面逻辑和应用程序分离, 在 Web 层面实现应用的解耦.
三, 环境搭建
项目结构图:
1,Spring 环境配置
spring-context
Spring 框架上下文环境容器配置.
- <!-- 读取外部配置文件 -->
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <!-- 允许 JVM 参数覆盖 -->
- <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
- <!-- 忽略没有找到的配置参数 -->
- <property name="ignoreResourceNotFound" value="true"/>
- <!-- 资源文件位置 -->
- <property name="locations">
- <list>
- <value>classpath:jdbc.properties</value>
- </list>
- </property>
- </bean>
- <!-- 启动组件扫描, 排除 @Controller 组件, 该组件由 SpringMVC 配置文件扫描 -->
- <context:component-scan base-package="com.spring.mvc">
- <context:exclude-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- </context:component-scan>
- <!-- 配置 DRUID 的连接池 -->
- <bean id="druidDataSource" abstract="true">
- <!-- 配置初始化, 最小, 最大 -->
- <property name="initialSize" value="${jdbc.initialSize}"/>
- <property name="minIdle" value="${jdbc.minIdle}" />
- <property name="maxActive" value="${jdbc.maxActive}" />
- <!-- 配置连接等待超时时间 -->
- <property name="maxWait" value="${jdbc.maxWait}" />
- <!-- 配置间隔多久才进行一次检测, 检测需要关闭的空闲连接, 单位是毫秒 -->
- <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
- <!-- 配置一个连接在池中的最小生存时间, 单位毫秒 -->
- <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
- <property name="validationQuery" value="SELECT'x'" />
- <property name="testWhileIdle" value="true" />
- <property name="testOnBorrow" value="false" />
- <property name="testOnReturn" value="false" />
- <!-- 打开 PSCache, 并且指定每个连接上 PSCache 的大小 -->
- <property name="poolPreparedStatements" value="true" />
- <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
- <!-- 配置监控统计拦截的 filters, 去掉后监控界面 SQL 无法统计 -->
- <property name="filters" value="stat" />
- </bean>
- <!-- 设置数据源信息 -->
- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" parent="druidDataSource">
- <!-- 配置连接的基本信息 -->
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <!--Spring 整合 mybatis-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <!-- 读取 mybatis 配置文件 -->
- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
- <!-- 自动扫描 mapper.xml 映射文件 -->
- <property name="mapperLocations" value="classpath:mybatis/mapper/**.xml"/>
- <!-- 分页助手插件 -->
- <property name="plugins">
- <array>
- <bean class="com.github.pagehelper.PageHelper">
- <property name="properties">
- <value>
- dialect=MySQL
- </value>
- </property>
- </bean>
- </array>
- </property>
- </bean>
- <!-- Mapper 接口文件扫描 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.spring.mvc.mapper" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
- </bean>
- <!-- 设置 JDBC 操作数据库 -->
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" lazy-init="false">
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!-- 设置 mybatis 操作数据库 -->
- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="sqlSessionFactory"/>
- </bean>
- <!-- 方式一: spring 事物管理器 -->
- <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <!-- 关联数据源 -->
- <property name="dataSource" ref="dataSource"/>
- </bean>
- <!-- 开启事务控制的注解支持 -->
- <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
- <!-- 配置手动事物管理 -->
- <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
- <property name="transactionManager" ref="dataSourceTransactionManager"/>
- </bean>
- spring-mvc
Mvc 开发环境容器配置.
- <!-- 扫描文件 -->
- <context:component-scan base-package="com.spring.mvc.controller" />
- <!-- MVC 默认的注解映射的方式 -->
- <mvc:annotation-driven />
- <mvc:default-servlet-handler/>
- <!-- 视图解析器 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/page/" />
- <property name="suffix" value=".jsp" />
- </bean>
2,jdbc 参数配置
- jdbc.driverClassName=com.MySQL.jdbc.Driver
- jdbc.url=jdbc:MySQL://localhost:3306/spring-mvc
- jdbc.username=root
- jdbc.password=123
- jdbc.initialSize=10
- jdbc.minIdle=10
- jdbc.maxActive=120
- jdbc.maxWait=60000
- jdbc.timeBetweenEvictionRunsMillis=60000
- jdbc.minEvictableIdleTimeMillis=300000
3,mybatis 配置
mybatis-config.xml 文件
- <configuration>
- <settings>
- <!-- 打印 sql 语句 -->
- <setting name="logImpl" value="STDOUT_LOGGING"/>
- </settings>
- </configuration>
4,Web.xml 配置
- <display-name>frame_spring</display-name>
- <!--Spring 相关配置 -->
- <context-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </context-param>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring*.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.Web.context.ContextLoaderListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>spring-mvc</servlet-name>
- <servlet-class>org.springframework.Web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring-mvc</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- <!-- 编码过滤器, 以 UTF8 编码 -->
- <filter>
- <filter-name>encodingFilter</filter-name>
- <filter-class>org.springframework.Web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- 5,pom.xml 配置
- <build>
- <finalName>${pom.artifactId}</finalName>
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*</include>
- </includes>
- </resource>
- </resources>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- </plugins>
- </build>
四, 源代码地址
GitHub. 地址
https://github.com/cicadasmile/spring-mvc-parent
GitEE. 地址
https://gitee.com/cicadasmile/spring-mvc-parent
来源: http://www.bubuko.com/infodetail-3262494.html