Spring 框架基础知识

来源:互联网 发布:javascript是什么 编辑:程序博客网 时间:2024/06/04 20:05
一、Spring 是什么:
    Spring是一个分层的JavaSE/EEfull-stack(一站式)轻量级开源框架;Spring是潜在地一站式解决方案,定位于与典型应用相关的大部分基础结构。

二、Spring 框架的特证:
 1、轻量级    框架的 jar 包只有 1M 多的,spring 是非侵入式的,不依赖于 其它类;
 2、面向切面    提供分离业务逻辑与系统级服务,进行内聚性的开发,例如日志和事务;
 3、控制反转    通过IOC促进了低耦合,一个对象依赖的其它对象通过动态代理方式实现实例化,降低对象之间的依赖关系;
 4、容器    包含管理应用对象的配置和生命周期,使用 bean 标签可以对类进行生命周期管理,不用关心对象的实例化、销毁过程;
 5、mvc        客户端发送请求,服务器完成请求转发,Controller调用Service、DAO进行数据操作,Model从DAO中取出数据,并响应视图;
关键词:AOP、事务、IOC.

三、Spring 控制反转,依赖注入(IOC DI):
 1、不创建对象,但是描述创建它们的方式;
 2、在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务;
 3、容器(IOC容器)负责将这些联系在一起,在典型的 IOC 场景中,容器创建了所有对象,并设置必要的属性将它们连接在一起,决定什么时间调用方法;
 4、注入的两种方式,设置、构造注入(property、constructor);

四、AOP实现方式:
 1、基于代理的AOP;
 2、AspectJ注解驱动的切面;
 3、纯POJO切面;
 4、注入式AspectJ切面;
 相关术语(Spring事务实现方式之一):
   1、切面(Aspect): 业务流程运行到某个特定的步骤,也就是一个应用运行的关注点,关注点可能横切多个对象,所以常常也被称为横切关注点;
   2、连接点(Joinpoint): 程序执行过程明确的点,如方法的调用、异常的抛出,Spring AOP中连接点总是方法的调用;
   3、通知(Advice): AOP框架在特定的切入点执行的增强处理,处理有around、before和After等类型;
   4、切入点(Pointcut): 可以插入增强处理的连接点,简而言之,当某个连接点满足指定要求时,该连接点将被添加增强处理,该连接点也就变成了切入点;
   还有其它的引入、代理、织入什么的具体没用过,不太了解。

五、Spring的事务:
  1、spring事务不需要与任何特定事务API耦合,对不同的持久化层访问技术,编程式事务提供一致事务编程风格,通过模板化的操作一致性的管理事务;
  2、声明式事务基于Spring AOP实现,并不需要程序开发人员成为AOP专家,亦可以轻易使用Spring的声明式事务管理;
  常用实现方式可以通过AOP切入事务,配置切面、连接点、切入点、通知;第二种在 service 方法上加 @Transactions, spring-mvc 中进行相关配置;

下面贴 spring 相关配置文件:

spring-mvc:

<?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.1.xsd                            http://www.springframework.org/schema/context                            http://www.springframework.org/schema/context/spring-context-3.1.xsd                            http://www.springframework.org/schema/mvc                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><context:property-placeholder location="classpath:config/config.properties"/><mvc:annotation-driven /><mvc:resources mapping="/resources/**" location="/resources/" /><mvc:resources mapping="/css/**" location="/css/" /><mvc:resources mapping="/images/**" location="/images/" /><mvc:resources mapping="/js/**" location="/js/" /><mvc:resources mapping="/common/**" location="/common/" /><mvc:resources mapping="/ueditor/**" location="/ueditor/" /><!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --><context:component-scan base-package="com.chinadatapay.controller" /><!--避免IE执行AJAX时,返回JSON出现下载文件 --><bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean><!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 --></list></property></bean><!-- 定义跳转的文件的前后缀 ,视图模式配置 --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 默认编码 --><property name="defaultEncoding" value="utf-8" /><!-- 文件大小最大值 --><property name="maxUploadSize" value="10485760000" /><!-- 内存中的最大值 --><property name="maxInMemorySize" value="40960" /></bean><!-- 初始化加载全局图片路径 --><bean id="globalListener" class="com.chinadatapay.util.GlobalListener"></bean><!-- 自定义拦截链配置 :用户访问后台未登录跳转到登录页面 --><mvc:interceptors><mvc:interceptor><mvc:mapping path="/*/*" /><mvc:mapping path="/toIndex" /><mvc:exclude-mapping path="/user/login" /><mvc:exclude-mapping path="/user/code" /><mvc:exclude-mapping path="/css/*" /><mvc:exclude-mapping path="/js/*" /><mvc:exclude-mapping path="/images/*" /><bean class="com.chinadatapay.base.Interceptor"></bean></mvc:interceptor></mvc:interceptors></beans> 

spring:

<?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:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd       http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.1.xsd      http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop.xsd">      <!-- 自动扫描 --><context:component-scan base-package="com.chinadatapay" /><!-- 引入配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations">        <list>            <value>classpath:config/jdbc.properties</value>            <value>classpath:config/config.properties</value>            <value>classpath:config/redis-cluster.properties</value>            <value>classpath:config/redis-single-point.properties</value>        </list>        </property></bean><import resource="classpath*:spring/spring-mybatis.xml"/>   <!-- <import resource="classpath*:redis/redis-cluster.xml"/>  --><import resource="classpath*:redis/redis-single-point.xml"/></beans>



1 0
原创粉丝点击