Spring
什么是Spring框架?
- Spring是一个开源框架
- Spring是于2003 年兴起的一个轻量级的Java开发框架,由Rod Johnson在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。
- 它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。
- Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以 从Spring中受益。
- Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。
Spring框架的特点
- 方便解耦,简化开发
* Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理 - AOP编程的支持
* Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能 - 声明式事务的支持
* 只需要通过配置就可以完成对事务的管理,而无需手动编程 - 方便程序的测试
* Spring对Junit4支持,可以通过注解方便的测试Spring程序 - 方便集成各种优秀框架
* Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支持 - 降低JavaEE API的使用难度
* Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等,都提供了封装,使这些API应用难度大大降低
Spring框架的IOC核心功能快速入门
什么是IOC的功能?
- IoC – Inverse of Control,控制反转,将对象的创建权反转给Spring!!
- 使用IOC可以解决程序耦合性高的问题!!
使用步骤
-
步骤一:下载Spring框架的开发包
* 官网:http://spring.io/
* 下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring
* Spring目录结构:
docs – API和开发规范
libs – jar包和源码
schema – 约束 -
步骤二:创建JavaWEB项目,引入Spring的开发包
- 引入Spring框架IOC核心功能需要的具体的jar包
* Spring框架的IOC的功能,那么根据Spring框架的体系结构图能看到,只需要引入如下的jar包- Beans
- Core
- Context
- Expression Language
- Spring框架也需要引入日志相关的jar包
* 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1
com.springsource.org.apache.commons.logging-1.1.1.jar
* 还需要引入log4j的jar包 spring-framework-3.0.2.RELEASE-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15
com.springsource.org.apache.log4j-1.2.15.jar
- 步骤三:创建对应的包结构,编写Java的类,要注意:以后使用Spring框架做开发,都需要来编写接口与实现类!!
- com.itcast.demo1
* UserService – 接口
* UserServiceImpl – 具体的实现类
- 步骤四:想把UserServiceImpl实现类的创建交给Spring框架来管理,需要创建Spring框架的配置文件,完成配置
- 在src目录下创建applicationContext.xml的配置文件,名称是可以任意的,但是一般都会使用默认名称!!
- 引入spring的约束,需要先找到具体的约束头信息!!
spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html
具体的约束如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
- 完成UserService的配置
<!-- Spring的快速入门 -->
<bean id="userService" class="com.itcast.demo1.UserServiceImpl"/>
- 步骤五:编写测试程序,采用Spring框架的工厂方式来获取到UserService接口的具体实现类!!
public void demo(){
// 创建工厂,加载核心配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 从工厂中获取对象
UserService userService = (UserService) applicationContext.getBean("userService");
//调用对象的方法
userService.sayHello();
}
Spring框架中的工厂
-
ApplicationContext接口
* 使用ApplicationContext工厂的接口,使用该接口可以获取到具体的Bean对象
* 该接口下有两个具体的实现类
ClassPathXmlApplicationContext – 加载类路径下的Spring配置文件
FileSystemXmlApplicationContext – 加载本地磁盘下的Spring配置文件 -
BeanFactory工厂(是Spring框架早期的创建Bean对象的工厂接口)
* 使用BeanFactory接口也可以获取到Bean对象
public void run(){
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
UserService us = (UserService) factory.getBean("us");
us.sayHello();
}
- BeanFactory和ApplicationContext的区别
* BeanFactory – BeanFactory采取延迟加载,第一次getBean时才会初始化Bean
* ApplicationContext – 在加载applicationContext.xml时候就会创建具体的Bean对象的实例,还提供了一些其他的功能
- 事件传递
- Bean自动装配
- 各种不同应用层的Context实现
配置Spring框架编写xml的提示
- 步骤一:先复制, http://www.springframework.org/schema/beans/spring-beans.xsd
- 步骤二:搜索XML Catalog,点击Add按钮
- 步骤三:先选择Location的schema的约束地址
* spring-framework-4.2.4.RELEASE-schema\beans\spring-beans-4.2.xsd - 步骤四:注意:Key type要选择:Schema location
- 步骤五:Key把http://www.springframework.org/schema/beans/spring-beans.xsd复制上
Spring框架中标签的配置
- id属性和name属性的区别
* id – Bean起个名字,在约束中采用ID的约束,唯一
取值要求:
必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号
不能出现特殊字符
* name – Bean起个名字,没有采用ID的约束(了解)
取值要求:
可以出现特殊字符.如果没有id的话 , name可以当做id使用
Spring框架在整合Struts1的框架的时候,Struts1的框架的访问路径是以/开头的,例如:/bookAction - class属性 – Bean对象的全路径
- scope属性 – scope属性代表Bean的作用范围
* singleton – 单例(默认值)
* prototype – 多例,在Spring框架整合Struts2框架的时候,Action类也需要交给Spring做管理,配置把Action类配置成多例!!
* request – 应用在Web项目中,每次HTTP请求都会创建一个新的Bean
* session – 应用在Web项目中,同一个HTTP Session 共享一个Bean
* globalsession – 应用在Web项目中,多服务器间的session - Bean对象的创建和销毁的两个属性配置
* 说明:Spring初始化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法
init-method – 当bean被载入到容器的时候调用init-method属性指定的方法
destroy-method – 当bean从容器中删除的时候调用destroy-method属性指定的方法
* 想查看destroy-method的效果,有如下条件
scope= singleton有效
web容器中会自动调用,但是main函数或测试用例需要手动调用(需要使用ClassPathXmlApplicationContext的close()方法)
依赖注入(DI)
- IOC和DI的概念
- IOC – Inverse of Control,控制反转,将对象的创建权反转给Spring!!
- DI – Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中!!
注:IOC是目标,DI是手段!!!
- DI(依赖注入)
- 例如:如果UserServiceImpl的实现类中有一个属性,那么使用Spring框架的IOC功能时,可以通过依赖注入把该属性的值传入进来!!
- 具体的配置如下
<bean id="us" class="com.itheima.demo1.UserServiceImpl">
<property name="uname" value="小风"/>
</bean>
Spring框架的属性注入
对于类成员变量,常用的注入方式有两种
- 构造函数注入
- 属性setter方法注入
- 构造方法的注入方式
* 编写java类,添加相应的构造器
* 编写配置文件
- 属性的setter方法的注入方式
* 编写java类,提供属性和对应的set方法即可
* 编写配置文件
Spring的2.5版本中提供了一种:p名称空间的注入
- 步骤一:需要先引入 p 名称空间
在schema的名称空间中加入该行:
xmlns:p="http://www.springframework.org/schema/p"
- 步骤二:使用p名称空间的语法
* p:属性名 = “”
* p:属性名-ref = “”
<bean id="person" class="com.itheima.demo4.Person" p:pname="老王" p:car2-ref="car2"/>
- 步骤三:测试
Spring的3.0提供了一种:SpEL注入方式
- SpEL:Spring Expression Language是Spring的表达式语言,有一些自己的语法
- 语法: #{SpEL}
- 例如如下的代码
<!-- SpEL的方式 -->
<bean id="person" class="com.itheima.demo4.Person">
<property name="pname" value="#{'小风'}"/>
<property name="car2" value="#{car2}"/>
</bean>
- 还支持调用类中的属性或者方法
集合(List,Set,Map),Properties等的注入
- 如果是数组或者List集合,注入配置文件的方式是一样的
<bean id="collectionBean" class="com.itheima.demo5.CollectionBean">
<property name="arrs">
<list>
<value>美美</value>
<value>小风</value>
</list>
</property>
</bean>
- 如果是Set集合,注入的配置文件方式如下:
<property name="sets">
<set>
<value>哈哈</value>
<value>呵呵</value>
</set>
</property>
- 如果是Map集合,注入的配置方式如下:
<property name="map">
<map>
<entry key="老王2" value="38"/>
<entry key="凤姐" value="38"/>
<entry key="如花" value="29"/>
</map>
</property>
- 如果是properties属性文件的方式,注入的配置如下:
<property name="pro">
<props>
<prop key="uname">root</prop>
<prop key="pass">123</prop>
</props>
</property>
Spring框架的配置文件分开管理
例如:在src的目录下又多创建了一个配置文件,现在是两个核心的配置文件,那么加载这两个配置文件的方式有两种!
- 主配置文件中包含其他的配置文件:
<import resource="applicationContext2.xml"/>
- 工厂创建的时候直接加载多个配置文件:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
Spring框架整合WEB
- 创建JavaWEB项目,引入Spring的开发包。编写具体的类和方法。
- 环境搭建好后,启动服务器来测试项目,发送每访问一次都会加载一次配置文件,这样效率会非常非常慢!!
- 解决上面的问题
- 将工厂创建好了以后放入到ServletContext域中.使用工厂的时候,从ServletContext中获得.
* ServletContextListener:用来监听ServletContext对象的创建和销毁的监听器.
* 当ServletContext对象创建的时候:创建工厂 , 将工厂存入到ServletContext
- Spring整合Web项目
- 引入spring-web-4.2.4.RELEASE.jar包
- 配置监听器
<!-- 配置Spring的核心监听器: -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
- 修改servlet的代码
- 从ServletContext中获得工厂
- 具体代码如下
ServletContext servletContext = ServletActionContext.getServletContext();
// 需要使用WEB的工厂的方式
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
CustomerService cs = (CustomerService) context.getBean("customerService");
cs.save();
Spring框架的IOC之注解方式的快速入门
- 步骤一:导入注解开发所有需要的jar包
* 引入IOC容器必须的6个jar包
* 多引入一个:Spring框架的AOP的jar包,spring-aop的jar包 - 步骤二:创建对应的包结构,编写Java的类
* UserService – 接口
* UserServiceImpl – 具体的实现类 - 步骤三:在src的目录下,创建applicationContext.xml的配置文件,然后引入约束。
注意:因为现在想使用注解的方式,那么引入的约束发生了变化
* 需要引入context的约束,具体的约束如下
<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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- bean definitions here -->
</beans>
- 步骤四:在applicationContext.xml配置文件中开启组件扫描
* Spring的注解开发:组件扫描
<context:component-scan base-package="com.itheima.demo1"/>
注意:可以采用如下配置<context:component-scan base-package="com.itheima"/>
这样是扫描com.itheima包下所有的内容
- 步骤五:在UserServiceImpl的实现类上添加注解
* @Component(value=“userService”) – 相当于在XML的配置方式中<bean id="userService" class="...">
- 步骤六:编写测试代码
public class SpringDemo1 {
@Test
public void run1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ac.getBean("userService");
us.save();
}
}
Spring框架中Bean管理的常用注解
-
@Component:组件.(作用在类上)
-
Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)
* @Controller – 作用在WEB层
* @Service – 作用在业务层
* @Repository – 作用在持久层
* 说明:这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强 -
属性注入的注解(说明:使用注解注入的方式,可以不用提供set方法)
- 如果是注入的普通类型,可以使用value注解
* @Value – 用于注入普通类型 - 如果注入的是对象类型,使用如下注解
@Autowired – 默认按类型进行自动装配
如果想按名称注入
@Qualifier – 强制使用名称注入 - @Resource – 相当于@Autowired和@Qualifier一起使用
- 强调:Java提供的注解
- 属性使用name属性
- 如果是注入的普通类型,可以使用value注解
Bean的作用范围和生命周期的注解
- Bean的作用范围注解
注解为@Scope(value=“prototype”),作用在类上。值如下:
* singleton – 单例,默认值
* prototype – 多例 - Bean的生命周期的配置
* @PostConstruct – 相当于init-method
* @PreDestroy – 相当于destroy-method
Spring框架整合JUnit单元测试
为了简化了JUnit的测试,使用Spring框架也可以整合测试
具体步骤
要求:必须先有JUnit的环境(即已经导入了JUnit4的开发环境)!!
- 步骤一:在程序中引入:spring-test.jar
- 步骤二:在具体的测试类上添加注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo1 {
@Resource(name="userService")
private UserService userService;
@Test
public void demo2(){
userService.save();
}
}
注意:如果有多个配置文件:
@ContextConfiguration("classpath:applicationContext.xml,classpath:applicationContext2.xml,classpath:applicationContext3.xml")
技术分析之Spring框架的核心功能之AOP技术
AOP的概述
- 什么是AOP的技术?
* 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程
* AOP是一种编程范式,隶属于软工范畴,指导开发者如何组织程序结构
* AOP最早由AOP联盟的组织提出的,制定了一套规范.Spring将AOP思想引入到框架中,必须遵守AOP联盟的规范
* 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术
* AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型
* 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率 - AOP:面向切面编程.(思想.—解决OOP遇到一些问题)
- AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视、事务管理、安全检查、缓存)
- 为什么要学习AOP
* 可以在不修改源代码的前提下,对程序进行增强!!
Spring框架的AOP的底层实现
- Srping框架的AOP技术底层也是采用的代理技术,代理的方式提供了两种
- 基于JDK的动态代理
* 必须是面向接口的,只有实现了具体接口的类才能生成代理对象 - 基于CGLIB动态代理
* 对于没有实现了接口的类,也可以产生代理,产生这个类的子类的方式
- 基于JDK的动态代理
- Spring的传统AOP中根据类是否实现接口,来采用不同的代理方式
- 如果实现类接口,使用JDK动态代理完成AOP
- 如果没有实现接口,采用CGLIB动态代理完成AOP
JDK的动态代理
使用Proxy类来生成代理对象的一些代码如下:
/**
* 使用JDK的方式生成代理对象
* @author Administrator
*/
public class MyProxyUtils {
public static UserDao getProxy(final UserDao dao) {
// 使用Proxy类生成代理对象
UserDao proxy = (UserDao) Proxy.newProxyInstance(dao.getClass().getClassLoader(),
dao.getClass().getInterfaces(), new InvocationHandler() {
// 代理对象方法一直线,invoke方法就会执行一次
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if("save".equals(method.getName())){
System.out.println("记录日志...");
// 开启事务
}
// 提交事务
// 让dao类的save或者update方法正常的执行下去
return method.invoke(dao, args);
}
});
// 返回代理对象
return proxy;
}
}
CGLIB的代理技术
- 引入CBLIB的开发包
* 如果想使用CGLIB的技术来生成代理对象,那么需要引入CGLIB的开发的jar包,在Spring框架核心包中已经引入了CGLIB的开发包了。所以直接引入Spring核心开发包即可! - 编写相关的代码
public static OrderDaoImpl getProxy(){
// 创建CGLIB核心的类
Enhancer enhancer = new Enhancer();
// 设置父类
enhancer.setSuperclass(OrderDaoImpl.class);
// 设置回调函数
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy methodProxy) throws Throwable {
if("save".equals(method.getName())){
// 记录日志
System.out.println("记录日志了...");
}
return methodProxy.invokeSuper(obj, args);
}
});
// 生成代理对象
OrderDaoImpl proxy = (OrderDaoImpl) enhancer.create();
return proxy;
}
Spring基于AspectJ的AOP的开发
AOP的相关术语
- Joinpoint(连接点) – 所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点
- Pointcut(切入点) – 所谓切入点是指我们要对哪些Joinpoint进行拦截的定义
- Advice(通知/增强) – 所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
- Introduction(引介) – 引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field
- Target(目标对象) – 代理的目标对象
- Weaving(织入) – 是指把增强应用到目标对象来创建新的代理对象的过程
- Proxy(代理)-- 一个类被AOP织入增强后,就产生一个结果代理类
- Aspect(切面) – 是切入点和通知的结合,以后咱们自己来编写和配置的
AspectJ的XML方式完成AOP的开发
- 步骤一:创建JavaWEB项目,引入具体的开发的jar包
* 先引入Spring框架开发的基本开发包
* 再引入Spring框架的AOP的开发包
spring的传统AOP的开发的包:
spring-aop-4.2.4.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
aspectJ的开发包:
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar - 步骤二:创建Spring的配置文件,引入具体的AOP的schema约束
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
- 步骤三:创建包结构,编写具体的接口和实现类
* com.itheima.demo2
* CustomerDao – 接口
* CustomerDaoImpl – 实现类 - 步骤四:将目标类配置到Spring中
<bean id="customerDao" class="com.itheima.demo3.CustomerDaoImpl"/>
- 步骤五:定义切面类
public class MyAspectXml {
// 定义通知
public void log(){
System.out.println("记录日志...");
}
}
- 步骤六:在配置文件中定义切面类
<bean id="myAspectXml" class="com.itheima.demo3.MyAspectXml"/>
- 步骤七:在配置文件中完成aop的配置
<aop:config>
<!-- 引入切面类 -->
<aop:aspect ref="myAspectXml">
<!-- 定义通知类型:切面类的方法和切入点的表达式 -->
<aop:before method="log" pointcut="execution(public * com.itheima.demo3.CustomerDaoImpl.save(..))"/>
</aop:aspect>
</aop:config>
- 完成测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo3 {
@Resource(name="customerDao")
private CustomerDao customerDao;
@Test
public void run1(){
customerDao.save();
customerDao.update();
customerDao.delete();
}
}
切入点的表达式
在配置切入点的时候,需要定义表达式,重点的格式如下:execution(public * *(…)),具体展开如下:
- 切入点表达式的格式如下:
* execution([修饰符] 返回值类型 包名.类名.方法名(参数)) - 修饰符可以省略不写,不是必须要出现的。
- 返回值类型是不能省略不写的,根据你的方法来编写返回值。可以使用 * 代替。
- 包名例如:com.itheima.demo3.BookDaoImpl
* 首先com是不能省略不写的,但是可以使用 * 代替
* 中间的包名可以使用 * 号代替
* 如果想省略中间的包名可以使用 … - 类名也可以使用 * 号代替,也有类似的写法:*DaoImpl
- 方法也可以使用 * 号代替
- 参数如果是一个参数可以使用 * 号代替,如果想代表任意参数使用 …
AOP的通知类型
- 前置通知
* 在目标类的方法执行之前执行。
* 配置文件信息:<aop:before method="before" pointcut-ref="myPointcut3"/>
* 应用:可以对方法的参数来做校验 - 最终通知
* 在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行。
* 在配置文件中编写具体的配置:<aop:after method="after" pointcut-ref="myPointcut3"/>
* 应用:例如像释放资源 - 后置通知
* 方法正常执行后的通知。
* 在配置文件中编写具体的配置:<aop:after-returning method="afterReturning" pointcut-ref="myPointcut2"/>
* 应用:可以修改方法的返回值 - 异常抛出通知
* 在抛出异常后通知
* 在配置文件中编写具体的配置:<aop:after-throwing method="afterThorwing" pointcut-ref="myPointcut3"/>
* 应用:包装异常的信息 - 环绕通知
* 方法的执行前后执行。
* 在配置文件中编写具体的配置:<aop:around method="around" pointcut-ref="myPointcut2"/>
* 要注意:目标的方法默认不执行,需要使用ProceedingJoinPoint对来让目标对象的方法执行。
Spring框架的AOP技术(注解方式)
- 步骤一:创建JavaWEB项目,引入具体的开发的jar包
* 先引入Spring框架开发的基本开发包
* 再引入Spring框架的AOP的开发包
spring的传统AOP的开发的包:
spring-aop-4.2.4.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
aspectJ的开发包:
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aspects-4.2.4.RELEASE.jar - 步骤二:创建Spring的配置文件,引入具体的AOP的schema约束
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
- 步骤三:创建包结构,编写具体的接口和实现类
- com.itheima.demo1
- CustomerDao – 接口
- CustomerDaoImpl – 实现类
- com.itheima.demo1
- 步骤四:将目标类配置到Spring中
<bean id="customerDao" class="com.itheima.demo1.CustomerDaoImpl"/>
- 步骤五:定义切面类
添加切面和通知的注解
* @Aspect – 定义切面类的注解
通知类型(注解的参数是切入点的表达式)
* @Before – 前置通知
* @AfterReturing – 后置通知
* @Around – 环绕通知
* @After – 最终通知
* @AfterThrowing – 异常抛出通知
具体的代码如下
@Aspect
public class MyAspectAnno {
@Before(value="execution(public void com.itheima.demo1.CustomerDaoImpl.save())")
public void log(){
System.out.println("记录日志...");
}
}
- 步骤六:在配置文件中定义切面类
<bean id="myAspectAnno" class="com.itheima.demo1.MyAspectAnno"/>
- 步骤七:在配置文件中开启自动代理
<aop:aspectj-autoproxy/>
- 完成测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
@Resource(name="customerDao")
private CustomerDao customerDao;
@Test
public void run1(){
customerDao.save();
customerDao.update();
}
}
通知类型
-
通知类型
* @Before – 前置通知
* @AfterReturing – 后置通知
* @Around – 环绕通知(目标对象方法默认不执行的,需要手动执行)
* @After – 最终通知
* @AfterThrowing – 异常抛出通知 -
配置通用的切入点
* 使用@Pointcut定义通用的切入点
@Aspect
public class MyAspectAnno {
@Before(value="MyAspectAnno.fn()")
public void log(){
System.out.println("记录日志...");
}
@Pointcut(value="execution(public void com.itheima.demo1.CustomerDaoImpl.save())")
public void fn(){}
}
Spring框架的JDBC模板技术
Spring框架的JDBC模板技术概述
- Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单
- 提供了JDBC模板,Spring框架提供的
- JdbcTemplate类
- Spring框架可以整合Hibernate框架,也提供了模板类
- HibernateTemplate类
演示JDBC的模板类
- 步骤一:创建数据库的表结构
create database spring_day03;
use spring_day03;
create table t_account(
id int primary key auto_increment,
name varchar(20),
money double
);
- 引入开发的jar包
- 先引入IOC基本的6个jar包
- 再引入Spring-aop的jar包
- 最后引入JDBC模板需要的jar包
* MySQL数据库的驱动包
* Spring-jdbc.jar
* Spring-tx.jar
- 编写测试代码(自己来new对象的方式)
@Test
public void run1(){
// 创建连接池,先使用Spring框架内置的连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_day03");
dataSource.setUsername("root");
dataSource.setPassword("root");
// 创建模板类
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
// 完成数据的添加
jdbcTemplate.update("insert into t_account values (null,?,?)", "测试",10000);
}
使用Spring框架来管理模板类
- 步骤一:Spring管理内置的连接池
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///spring_day03"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
- 步骤二:Spring管理模板类
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
- 步骤三:编写测试程序
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo2 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
public void run2(){
jdbcTemplate.update("insert into t_account values (null,?,?)", "测试2",10000);
}
}
Spring框架管理开源的连接池
- 管理DBCP连接池
* 先引入DBCP的2个jar包
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
* 编写配置文件
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///spring_day03"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
- 管理C3P0连接池
* 先引入C3P0的jar包
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
* 编写配置文件
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring_day03"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
Spring框架的JDBC模板的简单操作
增删改查的操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo3 {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Test
// 插入操作
public void demo1(){
jdbcTemplate.update("insert into account values (null,?,?)", "冠希",10000d);
}
@Test
// 修改操作
public void demo2(){
jdbcTemplate.update("update account set name=?,money =? where id = ?", "思雨",10000d,5);
}
@Test
// 删除操作
public void demo3(){
jdbcTemplate.update("delete from account where id = ?", 5);
}
@Test
// 查询一条记录
public void demo4(){
Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new BeanMapper(), 1);
System.out.println(account);
}
@Test
// 查询所有记录
public void demo5(){
List<Account> list = jdbcTemplate.query("select * from t_account", new BeanMapper());
for (Account account : list) {
System.out.println(account);
}
}
}
class BeanMapper implements RowMapper<Account>{
public Account mapRow(ResultSet rs, int arg1) throws SQLException {
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
return account;
}
}
Spring框架的事务管理相关的类和API
- PlatformTransactionManager接口 – 平台事务管理器.(真正管理事务的类)。该接口有具体的实现类,根据不同的持久层框架,需要选择不同的实现类!
- TransactionDefinition接口 – 事务定义信息.(事务的隔离级别,传播行为,超时,只读)
- TransactionStatus接口 – 事务的状态
- 总结:上述对象之间的关系:平台事务管理器真正管理事务对象.根据事务定义的信息TransactionDefinition 进行事务管理,在管理事务中产生一些状态.将状态记录到TransactionStatus中
- PlatformTransactionManager接口中实现类和常用的方法
- 接口的实现类
* 如果使用的Spring的JDBC模板或者MyBatis框架,需要选择DataSourceTransactionManager实现类
* 如果使用的是Hibernate的框架,需要选择HibernateTransactionManager实现类 - 该接口的常用方法
* void commit(TransactionStatus status)
* TransactionStatus getTransaction(TransactionDefinition definition)
* void rollback(TransactionStatus status)
- 接口的实现类
- TransactionDefinition
- 事务隔离级别的常量
* static int ISOLATION_DEFAULT – 采用数据库的默认隔离级别
* static int ISOLATION_READ_UNCOMMITTED
* static int ISOLATION_READ_COMMITTED
* static int ISOLATION_REPEATABLE_READ
* static int ISOLATION_SERIALIZABLE - 事务的传播行为常量(不用设置,使用默认值)
* 什么是事务的传播行为:解决的是业务层之间的方法调用!!
* PROPAGATION_REQUIRED(默认值) – A中有事务,使用A中的事务.如果没有,B就会开启一个新的事务,将A包含进来.(保证A,B在同一个事务中),默认值!!
* PROPAGATION_SUPPORTS – A中有事务,使用A中的事务.如果A中没有事务.那么B也不使用事务.
* PROPAGATION_MANDATORY – A中有事务,使用A中的事务.如果A没有事务.抛出异常.
* PROPAGATION_REQUIRES_NEW(记)-- A中有事务,将A中的事务挂起.B创建一个新的事务.(保证A,B没有在一个事务中)
* PROPAGATION_NOT_SUPPORTED – A中有事务,将A中的事务挂起.
* PROPAGATION_NEVER – A中有事务,抛出异常.
* PROPAGATION_NESTED(记) – 嵌套事务.当A执行之后,就会在这个位置设置一个保存点.如果B没有问题.执行通过.如果B出现异常,运行客户根据需求回滚(选择回滚到保存点或者是最初始状态)
- 事务隔离级别的常量
Spring框架的事务管理的分类
- Spring的编程式事务管理(不推荐使用)
* 通过手动编写代码的方式完成事务的管理(不推荐) - Spring的声明式事务管理(底层采用AOP的技术)
* 通过一段配置的方式完成事务的管理(重点掌握注解的方式)
Spring框架的事务管理之编程式的事务管理
说明:Spring为了简化事务管理的代码:提供了模板类 TransactionTemplate,所以手动编程的方式来管理事务,只需要使用该模板类即可!!
手动编程方式的具体步骤如下:
- 步骤一:配置一个事务管理器,Spring使用PlatformTransactionManager接口来管理事务,所以需要使用到它的实现类!!
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
- 步骤二:配置事务管理的模板
<!-- 配置事务管理的模板 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
- 步骤三:在需要进行事务管理的类中,注入事务管理的模板.
<bean id="accountService" class="com.itheima.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="transactionTemplate" ref="transactionTemplate"/>
</bean>
- 步骤四:在业务层使用模板管理事务:
// 注入事务模板对象
private TransactionTemplate transactionTemplate;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void pay(final String out, final String in, final double money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
// 扣钱
accountDao.outMoney(out, money);
int a = 10/0;
// 加钱
accountDao.inMoney(in, money);
}
});
}
Spring框架的事务管理之声明式事务管理,即通过配置文件来完成事务管理(AOP思想)
声明式事务管理又分成两种方式:
* 基于AspectJ的XML方式
* 基于AspectJ的注解方式
Spring框架的事务管理之基于AspectJ的XML方式
- 步骤一:引入AOP的开发包
- 步骤二:配置事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
- 步骤三:配置事务增强
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
name :绑定事务的方法名,可以使用通配符,可以配置多个。
propagation :传播行为
isolation :隔离级别
read-only :是否只读
timeout :超时信息
rollback-for:发生哪些异常回滚.
no-rollback-for:发生哪些异常不回滚.
-->
<!-- 哪些方法加事务 -->
<tx:method name="pay" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
- 步骤四:配置AOP的切面
<!-- 配置AOP切面产生代理 -->
<aop:config>
<aop:advisor advice-ref="myAdvice" pointcut="execution(* com.itheima.demo2.AccountServiceImpl.pay(..))"/>
</aop:config>
- 注意:如果是自己编写的切面,使用aop:aspect标签,如果是系统制作的,使用aop:advisor标签。
- 步骤五:编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Demo2 {
@Resource(name="accountService")
private AccountService accountService;
@Test
public void run1(){
accountService.pay("冠希", "美美", 1000);
}
}
Spring框架的事务管理之基于AspectJ的注解方式
- 步骤一:配置事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
- 步骤二:开启注解事务
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
- 步骤三:在业务层上添加一个注解:@Transactional
- 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class Demo3 {
@Resource(name="accountService")
private AccountService accountService;
@Test
public void run1(){
accountService.pay("冠希", "美美", 1000);
}
}