一、Spring
1.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(一站式) 轻量级开源框架。
2.Spring框架特点
方便解耦,简化开发:Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理;
AOP编程的支持:Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能;
声明对事务的支持:只需要通过配置就可以完成对事务的管理,而无需手动编程;
方便程序的测试:Spring对Junit4支持,可以通过注解方便的测试Spring程序;
方便集成各种优秀框架:Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis、Quartz等)的直接支持;
降低JavaEE API的使用难度:Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低。
3.IOC核心功能
1. 什么是IOC的功能?
IoC-- Inverse of Control,控制反转,将对象的创建权反转给Spring,使用IOC可以解决程序耦合性高的问题。
2. 步骤一:下载Spring框架的开发包。
官网:http://spring.io/
下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring
3. 步骤二:创建JavaWEB项目,引入Spring的开发包。
Spring框架的IOC的功能,需要引入4个jar包:Beans、Core、Context、Expression Language
Spring框架也需要引入日志相关2个jar包:com.springsource.org.apache.commons.logging-1.1.1.jar、 com.springsource.org.apache.log4j-1.2.15.jar
4. 步骤三:创建对应的包结构,编写Java的类,要注意:以后使用Spring框架做开发,都需要来编写接口与实现类。
com.itcast.demo1
UserService -- 接口
UserServiceImpl -- 具体的实现类
5. 步骤四:想把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的配置
<bean id="userService" class="com.itcast.demo1.UserServiceImpl"/>
6. 步骤五:编写测试程序,采用Spring框架的工厂方式来获取到UserService接口的具体实现类!!
public void demo2(){
// 使用Spring的工厂:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过工厂获得类:
UserService userService = (UserService) applicationContext.getBean("userService");
userService.sayHello();
}
4.Spring框架中的工厂
1)ApplicationContext接口
使用ApplicationContext工厂的接口,使用该接口可以获取到具体的Bean对象,该接口下有两个具体的实现类
ClassPathXmlApplicationContext -- 加载类路径下的Spring配置文件
FileSystemXmlApplicationContext -- 加载本地磁盘下的Spring配置文件
2)BeanFactory工厂(是Spring框架早期的创建Bean对象的工厂接口,已经过时)
使用BeanFactory接口也可以获取到Bean对象
public void run(){
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
UserService us = (UserService) factory.getBean("us");
us.sayHello();
}
3)BeanFactory和ApplicationContext的区别
BeanFactory -- BeanFactory采取延迟加载,第一次getBean时才会初始化Bean
ApplicationContext -- 在加载applicationContext.xml时候就会创建Bean对象实例,还提供:事件传递、Bean自动装配、各种不同应用层的Context实现
5.Bean管理的配置文件
1)id属性和name属性的区别
id -- 为Bean管理对象命名,在约束中采用ID的约束,唯一,必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号,id不能出现特殊字符
name -- 为Bean管理对象命名,没有采用ID的约束(了解),name可以出现特殊字符。<bean>没有id的话,name可以做id使用
2)class属性 -- Bean对象的全路径
3)scope属性 -- scope属性代表Bean的作用范围
singleton -- 单例(默认值)
prototype -- 多例,在Spring框架整合Struts2框架时,Action类也需要交给Spring做管理,把Action类配置成多例!!
request -- 应用在Web项目中,每次HTTP请求都会创建一个新的Bean
session -- 应用在Web项目中,同一个HTTP Session 共享一个Bean
globalsession -- 应用在Web项目中,多服务器间的session
4)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()方法,即使用applicationcontext的实现类)
<bean id="userService" class="it.dragon.UserServiceImpl" scope="singleton" init-method="init" destroy-method="destory">
二、依赖注入(DI)
1.DI的概念
DI -- Dependency Injection,依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中
2. DI(依赖注入)
如果UserServiceImpl的实现类中有一个属性,可使用Spring框架的IOC功能,通过依赖注入把该属性的值传入进来。
<bean id="us" class="com.itheima.demo1.UserServiceImpl">
<property name="uname" value="小风"/>
</bean>
3.属性注入
1)对于类成员变量,常用的注入方式有两种
构造函数注入
属性setter方法注入
2)构造方法的注入方式
编写Java的类,提供构造方法
public class Car {
private String name;
private double money;
public Car(String name, double money) {
this.name = name;
this.money = money;
}
}
编写配置文件
<bean id="car" class="com.itheima.demo4.Car">
<constructor-arg name="name" value="大奔"/>
<constructor-arg name="money" value="100"/>
</bean>
3)属性的setter方法的注入方式
编写Java的类,提供属性和对应的set方法
编写配置文件,配置文件的属性用:<property name="cname" value="大奔"/>
4)如果Java类的属性是另一个Java的类,使用ref进行注入
<property name="name" rel="具体的Bean的ID或者name的值"/>,例如:
<bean id="person" class="com.itheima.demo4.Person">
<property name="pname" value="美美"/>
<property name="car2" ref="car2"/>
</bean>
4.p名称空间注入(了解)
1)步骤一:需要先引入 p 名称空间
在schema的名称空间中加入该行:xmlns:p="http://www.springframework.org/schema/p"
2)步骤二:使用p名称空间的语法
p:属性名 = ""
p:属性名-ref = ""
3)步骤三:注意在类中需要使用setter方法,如果是构造方法,无法注入。
<bean id="person" class="com.itheima.demo4.Person" p:pname="老王" p:car2-ref="car2"/>
5.SpEL注入方式(了解)
SpEL:Spring Expression Language是Spring的表达式语言,有一些自己的语法
语法:#{SpEL}
<bean id="person" class="com.itheima.demo4.Person">
<property name="pname" value="#{'小风'}"/>
<property name="car2" value="#{car2}"/>
</bean>
还支持调用类中的属性或者方法
<bean id="carl" class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="#{kenny.song}" />
</bean>
kenny 是 Bean Id 而 song 是属性的名字,这样配置就如同我们写了如下的代码:
Instrumentalist carl = new Instrumentalist();
carl.setSong(kenny.getSong());
6.集合、Properties注入
1)如果是数组或者List集合,注入配置文件的方式是一样的
<bean id="collectionBean" class="com.itheima.demo5.CollectionBean">
<property name="arrs">
<list>
<value>美美</value>
<value>小风</value>
</list>
</property>
</bean>
2)如果是Set集合,注入的配置文件方式如下:
<property name="sets">
<set>
<value>哈哈</value>
<value>呵呵</value>
</set>
</property>
3)如果是Map集合,注入的配置方式如下:
<property name="map">
<map>
<entry key="老王" value="38"/>
<entry key="凤姐" value="38"/>
<entry key="如花" value="29"/>
</map>
</property>
4)如果是properties属性文件的方式,注入的配置如下:
private Properties pro;在实现类中有个properties属性。
<property name="pro">
<props>
<prop key="uname">root</prop>
<prop key="pass">123</prop>
</props>
</property>
7.配置文件分开管理
两个核心的配置文件,那么加载这两个配置文件的方式有两种!
1)主配置文件中包含其他的配置文件:<import resource="applicationContext2.xml"/>
2)工厂创建的时候直接加载多个配置文件:ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
三、Spring框架整合WEB
1. 创建JavaWEB项目,引入Spring的开发包。引入spring-web-4.2.4.RELEASE.jar。编写具体的类和方法。
环境搭建好后,启动服务器来测试项目,每访问一次都会加载一次配置文件,这样效率会非常非常慢
2. 解决上面的问题
将工厂创建好了以后放入到ServletContext域中,使用工厂的时候,从ServletContext中获得。
ServletContextListener:用来监听ServletContext对象的创建和销毁的监听器,当ServletContext对象创建的时候:创建工厂,将工厂存入到ServletContext。
配置监听器,在web.xml中
<!-- 配置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 = ServletActionContext.getServletContext();
// 需要使用WEB的工厂的方式
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
CustomerService cs = (CustomerService) context.getBean("customerService");
cs.save();
四、IOC功能之注解
1.入门
1.步骤一:导入注解开发所有需要的jar包
引入IOC容器必须的6个jar包,多引入一个Spring框架AOP的jar包:spring-aop-4.2.4.RELEASE.jar。
2. 步骤二:创建对应的包结构,编写Java的类
UserService -- 接口
UserServiceImpl -- 具体的实现类
3. 步骤三:在src的目录下,创建applicationContext.xml的配置文件,然后引入约束。注意:因为现在想使用注解的方式,那么引入的约束发生了变化:
<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">
</beans>
4. 步骤四:在applicationContext.xml配置文件中开启组件扫描
<context:component-scan base-package="com.itheima"/> 这样是扫描com.itheima包下所有的内容
5. 步骤五:在UserServiceImpl的实现类上添加注解
@Component(value="userService")
相当于在XML的配置方式中 <bean id="userService" class="...">
6. 步骤六:编写测试代码
public void run1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ac.getBean("userService");
us.save();
}
2.常用注解
1)@Component:作用在类上
2)Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)
@Controller -- 作用在WEB层
@Service -- 作用在业务层
@Repository -- 作用在持久层
这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强
3)属性注入的注解(使用注解注入的方式,可以不用提供set方法)
如果是注入的普通类型,可以使用value注解
@Value -- 用于注入普通类型
例如:在name属性上添加如下,
@Value(value = "gggg")
private String name;
如果注入的是对象类型,使用如下注解
@Autowired -- 默认按类型进行自动装配
如果想按名称注入
@Qualifier -- 强制使用名称注入,即类上面使用什么名称,就需要使用什么名称才能注入
例如:
@Autowired
@Qualifier(value = "userDao")
private UserDao ud;
@Resource -- 相当于@Autowired和@Qualifier一起使用,这是Java提供的注解,使用name属性
例如:
@Resource(name = "userDao")
private UserDao ud;
3.Bean的作用范围和生命周期的注解
1)Bean的作用范围注解
@Scope(value="prototype"),作用在类上。
singleton -- 单例,默认值
prototype -- 多例
2)Bean生命周期的配置
@PostConstruct -- 在类中方法上,相当于init-method
@PreDestroy -- 在类中方法上,相当于destroy-method
4.Spring框架整合JUnit
1)为了简化了JUnit的测试,使用Spring框架也可以整合测试
2)具体步骤
必须先有JUnit的环境(即已经导入了JUnit4的开发环境)
步骤一:在程序中引入:spring-test-4.2.4.RELEASE.jar
步骤二:在具体的测试类上添加注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo1 {
@Resource(name="userService")
private UserService userService;
@Test
public void demo2(){
userService.save();
}
}
五、AOP技术
1.AOP概述
在软件业,AOP为Aspect Oriented Programming的缩写上:面向切面编程。AOP是一种编程范式,隶属于软工范畴,指导开发者如何组织程序结构。AOP最早由AOP联盟的组织提出的,制定了一套规范。Spring将AOP思想引入到框架中,必须遵守AOP联盟的规范。通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术,AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视、事务管理、安全检查、缓存)。
2.AOP底层实现
1)Srping框架的AOP技术底层也是采用的代理技术,代理的方式提供了两种:
基于JDK的动态代理:必须是面向接口的,只有实现了具体接口的类才能生成代理对象
基于CGLIB动态代理:对于没有实现了接口的类,也可以产生代理,产生这个类的子类的方式
2)Spring传统AOP中根据类是否实现接口,来采用不同的代理方式
如果实现类接口,使用JDK动态代理完成AOP
如果没有实现接口,采用CGLIB动态代理完成AOP
3.JDK的动态代理
1)使用Proxy类来生成代理对象
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;
}
}
2)使用代理对象
public void run1(){
// 使用工具类,获取到代理对象
UserDao proxy = MyProxyUtils.getProxy(dao);
// 调用代理对象的方法 proxy.save(); proxy.update();
}
4.CGLIB的代理技术
1)引入CBLIB的开发包
如果想使用CGLIB的技术来生成代理对象,那么需要引入CGLIB的开发的jar包,在Spring框架核心包中已经引入了CGLIB的开发包了。所以直接引入Spring核心开发包即可!
2)编写相关的代码
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;
}
5.基于AspectJ的AOP的开发
1)AOP的相关术语
1. Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点
2. Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义
3. Advice(通知/增强) : 所谓通知是指拦截到Joinpoint之后所要做的事情就是通知。通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
4. Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field
5. Target(目标对象) :代理的目标对象
6. Weaving(织入) :是指把增强应用到目标对象来创建新的代理对象的过程
7. Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
8. Aspect(切面) :是切入点和通知的结合,以后咱们自己来编写和配置的
2)AspectJ的XML方式完成AOP的开发
1. 步骤一:创建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
2. 步骤二:创建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">
3. 步骤三:创建包结构,编写具体的接口和实现类
com.itheima.demo2
CustomerDao -- 接口
CustomerDaoImpl -- 实现类
4. 步骤四:将目标类配置到Spring中
<bean id="customerDao" class="com.itheima.demo3.CustomerDaoImpl"/>
5. 步骤五:定义切面类
public class MyAspectXml {
// 定义通知
public void log(){
System.out.println("记录日志...");
}
}
6. 步骤六:在配置文件中定义切面类
<bean id="myAspectXml" class="com.itheima.demo3.MyAspectXml"/>
7. 步骤七:在配置文件中完成aop的配置
<aop:config>
<aop:aspect ref="myAspectXml">
<!-- 定义通知类型:切面类的方法和切入点的表达式 -->
<aop:before method="log" pointcut="execution(public com.itheima.demo3.CustomerDaoImpl.save(..))"/>
</aop:aspect>
</aop:config>
8. 完成测试
@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();
}
}
6.切入点的表达式
在配置切入点的时候,需要定义表达式,重点的格式如下:execution(public *(..)),具体展开如下:
切入点表达式的格式如下:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
修饰符可以省略不写,不是必须要出现的,例如public。
返回值类型是不能省略不写的,根据你的方法来编写返回值。可以使用*代替。
包名例如:com.itheima.demo3.BookDaoImpl
首先com是不能省略不写的,但是可以使用*代替、中间的包名可以使用*号代替、如果想省略中间的包名可以使用 ..
类名也可以使用*号代替,也有类似的写法:*DaoImpl
方法也可以使用*号代替
参数如果是一个参数可以使用*号代替,如果想代表任意参数使用 ..
7.AOP的通知类型
1. 前置通知
在目标类的方法执行之前执行。
配置文件信息:<aop:after method="before" pointcut-ref="myPointcut3"/>
应用:可以对方法的参数来做校验
2. 最终通知
在目标类的方法执行之后执行,如果程序出现了异常,最终通知也会执行。
在配置文件中编写具体的配置:<aop:after method="after" pointcut-ref="myPointcut3"/>
应用:例如像释放资源
3. 后置通知
方法正常执行后的通知。
在配置文件中编写具体的配置:<aop:after-returning method="afterReturning" pointcut-ref="myPointcut2"/>
应用:可以修改方法的返回值
4. 异常抛出通知
在抛出异常后通知
在配置文件中编写具体的配置:<aop:after-throwing method="afterThorwing" pointcut-ref="myPointcut3"/>
应用:包装异常的信息
5. 环绕通知
方法的执行前后执行。
在配置文件中编写具体的配置:<aop:around method="around" pointcut-ref="myPointcut2"/>
要注意:目标的方法默认不执行,需要使用ProceedingJoinPoint对来让目标对象的方法执行。
public void around(ProceedingJoinPoint joinPoint){
System.out.println("环绕通知1...");
try {
// 手动让目标对象的方法去执行
joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("环绕通知2...");
}
8.AOP技术(注解方式)
1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包
先引入Spring框架开发的基本开发包,再引入Spring框架的AOP的开发包
2. 步骤二:创建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>
3. 步骤三:创建包结构,编写具体的接口和实现类
com.itheima.demo1
CustomerDao -- 接口
CustomerDaoImpl -- 实现类
4. 步骤四:将目标类配置到Spring中
<bean id="customerDao" class="com.itheima.demo1.CustomerDaoImpl"/>
5. 步骤五:定义切面类
添加切面和通知的注解
@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("记录日志...");
}
}
6. 步骤六:在配置文件中定义切面类
<bean id="myAspectAnno" class="com.itheima.demo1.MyAspectAnno"/>
7. 步骤七:在配置文件中开启自动代理
<aop:aspectj-autoproxy/>
8. 完成测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
@Resource(name="customerDao")
private CustomerDao customerDao;
@Test
public void run1(){
customerDao.save();
customerDao.update();
}
}
六、JDBC模板技术
1.JDBC模板技术概述
1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单
2. 提供了JDBC模板,Spring框架提供的 JdbcTemplate类
3. Spring框架可以整合Hibernate框架,也提供了模板类 HibernateTemplate类
2.JDBC的模板类
1.步骤一:创建数据库的表结构
create database spring_day03;
use spring_day03;
create table t_account(
id int primary key auto_increment,
name varchar(20),
money double
);
2.引入开发的jar包
先引入IOC基本的6个jar包、再引入Spring-aop的jar包、最后引入JDBC模板需要的jar包、MySQL数据库的驱动包、Spring-jdbc.jar、Spring-tx.jar
3.编写测试代码(自己来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);
}
3.Spring框架来管理模板类
1. 刚才编写的代码使用的是new的方式,应该把这些类交给Spring框架来管理。
2. 修改的步骤如下
步骤一: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);
}
}
4.管理开源的连接池
1. 管理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>
2. 管理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>
5.JDBC模板的简单操作
1. 增删改查的操作
@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);
jdbcTemplate.update("update account set name=?,money =? where id = ?", "思雨",10000d,5);
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;
}
}
6.事务管理
1. PlatformTransactionManager接口 -- 平台事务管理器(真正管理事务的类)。该接口有具体的实现类,根据不同的持久层框架,需要选择不同的实现类!
2. TransactionDefinition接口 -- 事务定义信息(事务的隔离级别,传播行为,超时,只读)
3. TransactionStatus接口 -- 事务的状态
4. 总结:上述对象之间的关系:平台事务管理器真正管理事务对象,根据事务定义的信息TransactionDefinition 进行事务管理,在管理事务中产生一些状态,将状态记录到TransactionStatus中。
PlatformTransactionManager接口中实现类和常用的方法
1)接口的实现类
如果使用的Spring的JDBC模板或者MyBatis框架,需要选择DataSourceTransactionManager实现类
如果使用的是Hibernate的框架,需要选择HibernateTransactionManager实现类
2)该接口的常用方法
void commit(TransactionStatus status)
TransactionStatus getTransaction(TransactionDefinition definition)
void rollback(TransactionStatus status)
TransactionDefinition
1)事务隔离级别的常量
static int ISOLATION_DEFAULT -- 采用数据库的默认隔离级别
static int ISOLATION_READ_UNCOMMITTED
static int ISOLATION_READ_COMMITTED
static int ISOLATION_REPEATABLE_READ
static int ISOLATION_SERIALIZABLE
2)事务的传播行为常量(不用设置,使用默认值)
先解释什么是事务的传播行为:解决的是业务层之间的方法调用!!
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出现异常,运行客户根据需求回滚(选择回滚到保存点或者是最初始状态)
7.搭建事务管理转账案例的环境
1. 步骤一:创建WEB工程,引入需要的jar包
IOC的6个包、AOP的4个包、C3P0的1个包、MySQL的驱动包、JDBC目标2个包、整合JUnit测试包
2. 步骤二:引入配置文件
引入配置文件:
引入log4j.properties
引入applicationContext.xml
<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>
3. 步骤三:创建对应的包结构和类
com.itheima.demo1
AccountService
AccountServlceImpl
AccountDao
AccountDaoImpl
4. 步骤四:引入Spring的配置文件,将类配置到Spring中
<bean id="accountService" class="com.itheima.demo1.AccountServiceImpl">
</bean>
<bean id="accountDao" class="com.itheima.demo1.AccountDaoImpl">
</bean>
5. 步骤五:在业务层注入DAO ,在DAO中注入JDBC模板(强调:简化开发,以后DAO可以继承JdbcDaoSupport类)
<bean id="accountService" class="com.itheima.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<bean id="accountDao" class="com.itheima.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
6. 步骤六:编写DAO和Service中的方法
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
public void outMoney(String out, double money) {
this.getJdbcTemplate().update("update t_account set money = money = ? where name = ?", money,out);
}
public void inMoney(String in, double money) {
this.getJdbcTemplate().update("update t_account set money = money + ? where name = ?", money,in);
}
}
7. 步骤七:编写测试程序.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {
@Resource(name="accountService")
private AccountService accountService;
@Test
public void run1(){
accountService.pay("冠希", "美美", 1000);
}
}
8.Spring框架的事务管理的分类
1)Spring的编程式事务管理(不推荐使用)
通过手动编写代码的方式完成事务的管理(不推荐)
2)Spring的声明式事务管理(底层采用AOP的技术)
通过一段配置的方式完成事务的管理(重点掌握注解的方式)
9.编程式的事务管理
1. 说明:Spring为了简化事务管理的代码,提供了模板类 TransactionTemplate,所以手动编程的方式来管理事务,只需要使用该模板类即可!!
2. 手动编程方式的具体步骤如下:
步骤一:配置一个事务管理器,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);
}
});
}
10.声明式事务管理
即通过配置文件来完成事务管理(AOP思想),分两种: 基于AspectJ的XML方式、基于AspectJ的注解方式
11.基于AspectJ的XML方式(重点掌握)
1. 步骤一:恢复转账开发环境
2. 步骤二:引入AOP的开发包
3. 步骤三:配置事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
4. 步骤四:配置事务增强
<!-- 配置事务增强 -->
<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>
5. 步骤五:配置AOP的切面
<!-- 配置AOP切面产生代理 -->
<aop:config>
<aop:advisor advice-ref="myAdvice" pointcut="execution(com.itheima.demo2.AccountServiceImpl.pay(..))"/>
</aop:config>
注意:如果是自己编写的切面,使用<aop:aspect>标签,如果是框架的,使用<aop:advisor>标签。
6. 步骤六:编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Demo2 {
@Resource(name="accountService")
private AccountService accountService;
@Test
public void run1(){
accountService.pay("冠希", "美美", 1000);
}
}
12.基于AspectJ的注解方式(重点掌握,最简单的方式)
1.步骤一:恢复转账的开发环境
2.步骤二:配置事务管理器
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
3.步骤三:开启注解事务
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
4.步骤四:在业务层的类上添加一个注解:@Transactional
5.编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext3.xml")
public class Demo3 {
@Resource(name="accountService")
private AccountService accountService;
@Test
public void run1(){
accountService.pay("冠希", "美美", 1000);
}
}