关于Spring的理解
Spring为2002年正式发布,经过发展拓展为七大组件:
| 组件 | 说明 |
|---|---|
| Core | Spring核心的组件,实现对Bean的管理,相当于java自带的JRE |
| AOP | Aspect Oriented Programming(面向切面编程) |
| ORM | Object Relation Mapping(对象关系映射) |
| DAO | Data Access Object(数据持久化对象) |
| Context | 指编程的环境:如两个包的关系 |
| Web | 支持JavaEE的部分 |
| WebMVC | 支持Web开发的独立框架——SpringMVC |
在这里,Core中涉及到一个重要的概念叫做 IOC(InversionOfControl控制反转) 简单说明一下:
正向:A需要使用B类,在A类中实例化B(A依赖B)
反向:A需要使用B类,不用A来new,而是通过Spring注入B给A
所以控制反转可以简单地理解为一种依赖注入。
业务流程
我们先简单谈一下,要想完成一个项目基本的流程是怎样的。
首先接到了项目要去进行需求分析,由需求分析师完成。根据客户的需求和需要进行一个协商交涉,明确用户的需求。确定需求后和数据分析师以及架构师进行商议,给出设计方案。
给出整体方案后就进行数据库设计,由数据分析师完成。明确了需求之后,我们就可以进行数据库的设计,对数据进行合理的存储方式设计。好的数据库设计往往可以极大地提高程序效率,合理建库建表,优秀的sql语句极其重要。
进行后台业务处理,由程序架构师完成。架构师会对后台的逻辑进行一个整体架构,给项目定级定项,给出项目的开发规范和标准,扫清项目的主要难点。大的项目架构师会完成整体的骨架的设计,小项目也可能细致到设定接口名。再由中低级程序员合作进行实现。后台属于程序的核心部分。
对性能要求高的项目还需要进行算法设计。有算法架构师完成。
然后进行前端页面的设计,由前端UI工程师完成。UI负责页面的设计美化。对于程序用户体验感起着至关重要的作用,代表着程序的形象。
最后,进行稳定性测试,由测试工程师完成。他负责程序的稳定性测试,以保证上线后程序可以应对各种挑战。测试出问题会不断地和前后端程序员交涉。
SSM搭建目录
很多人在学习SSM时感觉整体的逻辑性不强,思路不够清晰,往往都不知道干什么。那么首先我们把目录规划一下,一个SSM项目,至少需要这几个部分:
pojo:实体类
dao:持久层(对数据进行操作,CRUD等)
service:业务层
controller:控制层
除此之外,mybatis的部分我们还需要配置:
sqlMapperConfig.xml / mybatisConfig.xml:
· 配置,配置数据源。现由Spring管理。
{
数据源需要配置三个setting属性:
懒加载 lazyLoadingEnabled ——ture 包用到的加载,没用到的不加载
消极加载 aggressiveLazyLoading ——false 属性和方法用到的加载,没用不加载(积极的懒加载,关闭了就是消极加载)
二级缓存 cacheEnabled ——true 打开cache进行缓存操作
}
· 连接,连接数据库。
· 绑定,与pojo层绑定。
· 操作,与dao层的mapper.xml关联,进行CRUD操作。
db.properties:配置数据库连接信息。
Spring部分需要配置:
applicationContext.xml:
Spring整合配置文件,用于整合:
· xxxDao.xml
· xxxService.xml
· springmvc.xml
dao.xml:管理dao层
· 关联db.properties
· 配置数据库连接池
· 配置sqlSessionFactory对象,绑定sqlMapperConfig.xml
· 扫描dao层包,实现动态的dao接口到spring容器的注入
service.xml:用于管理service层
· 扫描service层包
· service层实现类到IOC容器的注入
· 配置事务管理器
· 配置AOP事务的支持
springmvc.xml:用于管理controller层
· 控制注解驱动,可解决json的乱码。
· 配置静态默认servlet
· 配置视图解析器
· 扫描controller层包
然后是最重要的web.xml:负责统揽全局。
· 配置SpringMVC的分发:DispatcherServlet
· 设置过滤器,可实现字符乱码的控制
· 配置Session过期时间
如果运用的idea用到了maven,还需要有一个pom.xml:
· 配置jar包版本号及属性
· 配置maven资源过滤
下面我们来看一下目录层次结构:

展开图:

二、配置文件
配置文件的格式
有两种配置文件的结构:
DTD(Document Type Definition文档类型定义),为较早的传统配置文件结构。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
""
"">
如Mybatis的配置格式:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org/DTD/MyBatis Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
XSD,(Xml SchemaDefinition可拓展标记语言的模式定义),是一个以Xml为基础的文件,于2001年发布,是Spring所采用的配置文件的结构。
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean />
<bean />
</beans>
注:Xml本身也为缩写(EXtensible Markup
Language可拓展标记语言),是Ml(标记语言)的一个拓展包。如:HTML,也是在标记语言上的一种拓展。
配置文件共分为一下几种:web.xml(位于WebContent中WEB-INF下)、SpringMVC.xml、applicationContext.xml、sqlMapperConfig.xml、ClassNameDao.xml
1、springmvc.xml
负责Controller层的管理,有两种编写方式:非注解形式、注解扫描形式。
(1)非注解形式。
所有的配置都在一个文件中,看起来直观,利于后期维护。但需要编写繁杂的配置文件,工作量较大。
SpringMVC.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
">
<!-- handler下的,统一资源处理映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name="mappings">
<props>
<prop key="hello.do">b123</prop>
</props>
</property>
</bean>
<!-- name对应上方的key,class对应Controller的类 -->
<bean name="b123"
class="com.hzyc.lesson12.controllers.MyController" />
<!-- mvc下的,控制处理适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- view下的,内部资源视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
</beans>
beans中写需要用的组件, 基础配置部分: xmlns(xmlnamespace即:Xml的命名空间,也就是Xml的名字)
xmlns:xsi(xml schema instance即:Xml的模式实例) 拓展配置部分
xsi:schemaLocation为模式地址,格式为Key-Value对,二者一一对应: Key:
http://www.springframework.org/schema/context Value:
http://www.springframework.org/schema/context/spring-context-4.2.xsd
// Value部分写入对应包的大版本号即可(4.2),无需继续细化
注:若不需要使用MVC、Context等,则对应的部分不用配置, 剔除多于的配置可提高程序运行效率、避免出现脏数据等。
通过以上配置可看出,关键部分为编写了三个处理器:
统一资源处理映射器(SimpleUrlHandlerMapping)
控制处理适配器(SimpleControllerHandlerAdapter)
内部资源视图解析器(InternalResourceViewResolver)
三个处理器调用路径对比:
| 公共部分 | 衔接 | 包 | 中文直译 |
|---|---|---|---|
| org.springframework.web.servlet | .handler.SimpleUrlHandlerMapping | handler下的 | 简单统一资源定位器的处理映射器 |
| org.springframework.web.servlet | .mvc.SimpleControllerHandlerAdapter | mvc下的 | 简单控制处理器的适配器 |
| org.springframework.web.servlet | .view.InternalResourceViewResolver | view下的 | 内部资源的视图解析器 |
对应Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 工程名 -->
<display-name>lesson12.mvc</display-name>
<welcome-file-list>
<!-- 默认界面 -->
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置一个唯一的Servlet,实现Controller,对所有的请求都进行控制 -->
<servlet>
<!-- 自定义Servlet名为springmvc -->
<servlet-name>springmvc</servlet-name>
<!-- 引入servlet下的“分发Servlet” -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化参数-->
<init-param>
<!--行文配置地址-->
<param-name>contextConfigLocation</param-name>
<!--类路径指向编写的springmvc.xml配置文件-->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<!--对Servlet映射进行配置-->
<servlet-mapping>
<!--映射对应的servlet名-->
<servlet-name>springmvc</servlet-name>
<!--servlet作用域:所有的.do-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
(2)注解扫描形式
在对应类与方法前添加注解,如此做不利于查找以及后期维护,但编写简便,无需进行繁杂的配置。
SpringMVC.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
">
<!--对组件进行上下文扫描,扫描的包为"com.hzyc.controller"-->
<context:component-scan base-package="com.hzyc.controller" />
</beans>
对应的Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>lesson16.ssm</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 读取applicationContext.xml,设置一个上下文加载的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/applicationContext.xml</param-value>
</context-param>
<!-- 读取springmvc.xml -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/springmvc/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 设置编码的过滤器 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2、applicationContext.xml
负责service和dao层的基础配置:
<?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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
">
<!--占位符分离数据源配置-->
<context:property-placeholder location="classpath:db.properties" />
<!--对上下文进行组件扫描,扫描的包为"com.hzyc.service"-->
<context:component-scan base-package="com.hzyc.service" />
<!--数据源配置-->
<bean id="ds123" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--创建Mybatis的SqlSessionFactory-->
<!--ref映射到上面的数据源beanid,将配置路径的值设为类路径的sql映射文件-->
<bean id="ssf456" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="ds123" />
<property name="configLocation" value="classpath:config/mybatis/sqlMapperConfig.xml" />
</bean>
<!--扫描持久层的Dao包,-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.hzyc.dao" />
</bean>
</beans>
db.properties
通过property-placeholder(固定占位符),将数据库的配置分离出来,使结构更加清晰,降低数据源的修改难度。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3308/hzyc98?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
3、sqlMapperConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org/DTD/MyBatis Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!--懒加载开启-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--积极加载关闭-->
<setting name="aggressiveLazyLoading" value="false"/>
<!--二级缓存打开-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--别名类,批量给JavaBean起别名,格式为对应类的类名的首字母变小写-->
<!--使用此标签resultType和paramerterType以及一些需要全类名的地方就不需要再写全类名,直接写别名提高编程效率-->
<typeAliases>
<package name="com.hzyc.pojo"/>
</typeAliases>
<!--匹配持久层方法配置文件-->
<mappers>
<mapper resource="com/hzyc/mapper/StudentDao.xml" />
</mappers>
</configuration>
| setting | statement | 直译 |
|---|---|---|
| lazyLoadingEnabled | true | 懒加载 |
| aggressiveLazyLoading | false | 积极加载 |
| cacheEnabled | true | 二级缓存 |
| 名称 | 解释 |
|---|---|
| 懒加载 | 就是延时加载,当对象需要用到的时候再去加载,也会造成数据库的查询级联,置true可有效缓解服务器压力 |
| 积极加载 | 对象的属性按需加载 |
| 二级缓存 | 提高运行速度 |
| StudentDao.xml |
进行sql语句的编写,实现Dao层的方法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org/DTD/MyBatis Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hzyc.dao.StudentDao" >
<select id="find123" parameterType="_int" resultType="studentModel" >
select * from student where id = #{id}
</select>
</mapper>
三、结构梳理


-
JavaBean 标准:Java软件的最小粒子应该是JavaBean:包含private属性、get、set方法
-
MVC
view 视图(用户直接可见的)—— web前端工程师(UI==处理用户接口)
Controller 控制器(选择一个合适的模型,把用户的数据封装起来)—— Model(有了数据) 实际建立的包:
com.hzyc.controller
com.hzyc.model(pojo)
springmvc.xml
- 需要在web.xml中注册一个分发Servlet(服务器端应用程序 server applet)
org.springframework.web.servlet.DispatcherServlet
- springmvc的核心配置文件:
【1】org.springframework.web.servlet.
Url处理映射器: handler.SimpleUrlHandlerMapping
控制处理适配器: mvc.SimpleControllerHandlerAdapter
视图解析器: view.InternetResourceViewResolver【2】使用注解扫描
<context:component-scan base-package=“com.hzyc.controller” />
- service
业务逻辑层(算法)—— 数据加工(response)
实际建立的包:
com.hzyc.service
applicationContext.xml【0】自己的事(业务逻辑) <context:component-scan
base-package=“com.hzyc.service” />
【1】代理了Mybatis的数据源
<context:property-placeholder location=“classpath:db.properties” />
<bean id="ds123" class="org.springframework.jdbc.datasouce.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
【2】代理Mybatis的会话工厂(SqlSessionFactory)
<bean id="ssf456" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="ds123" />
<property name="configLocation" value="classpath:sqlMapperConfig.xml" />
</bean>
【2.1】由于代理了Mybatis的功能,需要把mapper(dao)扫入
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hzyc.dao" />
</bean>
【3】使用监听器,在软件项目(工程)启动的时候,就加载spring核心配置文件
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
需要配置他的位置(指向applicationContext.xml这个文件)
- dao
数据访问层(持久化)—— 备份的功能(数据闪回)—— POJO(纯JavaBean) 【**】实际建立的包:
com.hzyc.dao(mapper包存映射xml文件) com.hzyc.pojo
sqlMapperConfig.xml(替代原来的mybatis-config.xml)
<settings>
lazyLoadingEnabled true
aggressiveLazyLoading false
cacheEnabled true
<typeAliases>
<package name="com.hzyc.pojo" />
<mappers>
<mapper resource=".../.../映射文件.xml" >
本文概述了Spring的核心组件,包括IoC、AOP、ORM、DAO等,介绍了项目流程、SSM搭建步骤,并详细解读了配置文件如SpringMVC.xml、applicationContext.xml和sqlMapperConfig.xml。重点讲解了MVC架构和配置细节,适合Spring初学者。
2291

被折叠的 条评论
为什么被折叠?



