JAVA学习笔记—Spring框架基础

文章目录

一、控制反转

1. Spring概述

1.1 Spring是什么

Spring是分层的 Java SE/EE应用 full-stack(全栈式) 轻量级开源框架。
提供了表现层 SpringMVC和持久层 Spring JDBC Template以及 业务层 事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架。
两大核心:以 IOC(Inverse Of Control:控制反转)和 AOP(Aspect Oriented Programming:面向切面编程)为内核。

1.2 Spring优势

1)方便解耦,简化开发
Spring就是一个容器,可以将所有对象创建和关系维护交给Spring管理
什么是耦合度?对象之间的关系,通常说当一个模块(对象)更改时也需要更改其他模块(对象),这就是
耦合,耦合度过高会使代码的维护成本增加。要尽量解耦
2)AOP编程的支持
Spring提供面向切面编程,方便实现程序进行权限拦截,运行监控等功能。
3)声明式事务的支持
通过配置完成事务的管理,无需手动编程
4)方便测试,降低JavaEE API的使用
Spring对Junit4支持,可以使用注解测试
5)方便集成各种优秀框架
不排除各种优秀的开源框架,内部提供了对各种优秀框架的直接支持

1.3 Spring体系结构

image-20210118195224293

2. 初识IOC

2.1 概述

控制反转(Inverse Of Control)不是什么技术,而是一种设计思想。它的目的是指导我们设计出更加松耦合的程序。
控制:在java中指的是对象的控制权限(创建、销毁)
反转:指的是对象控制权由原来 由开发者在类中手动控制 反转到 由Spring容器控制
举个栗子
* 传统方式
之前我们需要一个userDao实例,需要开发者自己手动创建 new UserDao();
* IOC方式
现在我们需要一个userDao实例,直接从spring的IOC容器获得,对象的创建权交给了spring控制

3. Spring快速入门

3.1 介绍

需求:借助spring的IOC实现service层与dao层代码解耦合
步骤分析
\1. 创建java项目,导入spring开发基本坐标
\2. 编写Dao接口和实现类
\3. 创建spring核心配置文件
\4. 在spring配置文件中配置 UserDaoImpl
\5. 使用spring相关API获得Bean实例

3.2 实现

1)创建java项目,导入spring开发基本坐标

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

2)编写Dao接口和实现类

public interface UserDao {
	public void save();
}
public class UserDaoImpl implements UserDao {
	public void save() {
	System.out.println("保存成功了...");
	}
}

3)创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

4)在spring配置文件中配置 UserDaoImpl

<beans ...>
<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl"></bean>
</beans>

5)使用spring相关API获得Bean实例

public class UserTest {
    @Test
    public void testSave() throws Exception {
    ApplicationContext applicationContext = new
    ClassPathXmlApplicationContext("applicationContext.xml");
    UserDao userDao = (UserDao) applicationContext.getBean("userDao");
    userDao.save();
    }
}

image-20210118204258867

image-20210118204159076

image-20210118204315181

3.3 知识小结

Spring的开发步骤

1. 导入坐标
2. 创建Bean
3. 创建applicationContext.xml
4. 在配置文件中进行Bean配置
5. 创建ApplicationContext对象,执行getBean

4. Spring相关API

4.1 API继承体系介绍

Spring的API体系异常庞大,我们现在只关注两个BeanFactory和ApplicationContext

image-20210118195743094

4.2 BeanFactory

BeanFactory是 IOC 容器的核心接口,它定义了IOC的基本功能。
特点:在第一次调用getBean()方法时,创建指定对象的实例

BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

4.3 ApplicationContext

代表应用上下文对象,可以获得spring中IOC容器的Bean对象。
特点:在spring容器启动时,加载并创建所有对象的实例
常用实现类

1. ClassPathXmlApplicationContext
它是从类的根路径下加载配置文件 推荐使用这种。
2. FileSystemXmlApplicationContext
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
3. AnnotationConfigApplicationContext
当使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
常用方法
1. Object getBean(String name);
根据Bean的id从容器中获得Bean实例,返回是Object,需要强转。
2. <T> T getBean(Class<T> requiredType);
根据类型从容器中匹配Bean实例,当容器中相同类型的Bean有多个时,则此方法会报错。
3. <T> T getBean(String name,Class<T> requiredType);
根据Bean的id和类型获得Bean实例,解决容器中相同类型Bean有多个情况。

5. Spring配置文件

5.1 Bean标签基本配置

<bean id="" class=""></bean>
* 用于配置对象交由Spring来创建。
* 基本属性:
id:Bean实例在Spring容器中的唯一标识
class:Bean的全限定名
* 默认情况下它调用的是类中的 无参构造函数,如果没有无参构造函数则不能创建成功。

5.2 Bean标签范围配置

<bean id="" class="" scope=""></bean>

scope属性指对象的作用范围,取值如下:

取值范围说明
singleton默认值,单例的
prototype多例的
requestWEB项目中,Spring创建一个Bean的对象,将对象存入到request域中
sessionWEB项目中,Spring创建一个Bean的对象,将对象存入到session域中
global sessionWEB项目中,应用在Portlet环境,如果没有Portlet环境那么globalSession 相当 于 session
1. 当scope的取值为singleton时
Bean的实例化个数:1个
Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例
Bean的生命周期:
对象创建:当应用加载,创建容器时,对象就被创建了
对象运行:只要容器在,对象一直活着
对象销毁:当应用卸载,销毁容器时,对象就被销毁了
2. 当scope的取值为prototype时
Bean的实例化个数:多个
Bean的实例化时机:当调用getBean()方法时实例化Bean
Bean的生命周期:
对象创建:当使用对象时,创建新的对象实例
对象运行:只要对象在使用中,就一直活着
对象销毁:当对象长时间不用时,被 Java 的垃圾回收器回收了

5.3 Bean生命周期配置

<bean id="" class="" scope="" init-method="" destroy-method=""></bean>
* init-method:指定类中的初始化方法名称
* destroy-method:指定类中销毁方法名称

5.4 Bean实例化三种方式

无参构造方法实例化
工厂静态方法实例化
工厂普通方法实例化

5.4.1 无参构造方法实例化

它会根据默认无参构造方法来创建类对象,如果bean中没有默认无参构造函数,将会创建失败

<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl"/>
5.4.2 工厂静态方法实例化

应用场景
依赖的jar包中有个A类,A类中有个静态方法m1,m1方法的返回值是一个B对象。如果我们频繁使用B对象,此时我们可以将B对象的创建权交给spring的IOC容器,以后我们在使用B对象时,无需调用A类中的m1方法,直接从IOC容器获得

public class StaticFactoryBean {
    public static UserDao createUserDao(){
    return new UserDaoImpl();
    }
}
<bean id="userDao" class="com.lagou.factory.StaticFactoryBean"
factory-method="createUserDao" />
5.4.3 工厂普通方法实例化

应用场景
依赖的jar包中有个A类,A类中有个普通方法m1,m1方法的返回值是一个B对象。如果我们频繁使用B对象,
此时我们可以将B对象的创建权交给spring的IOC容器,以后我们在使用B对象时,无需调用A类中的m1方法,直接从IOC容器获得。

public class DynamicFactoryBean {
    public UserDao createUserDao(){
    return new UserDaoImpl();
    }
}
<bean id="dynamicFactoryBean" class="com.lagou.factory.DynamicFactoryBean"/>
<bean id="userDao" factory-bean="dynamicFactoryBean" factorymethod="createUserDao"/>

5.5 Bean依赖注入概述

依赖注入 DI(Dependency Injection):它是 Spring 框架核心 IOC 的具体实现。
在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。IOC 解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系,在使用 Spring 之后,就让 Spring 来维护了。简单的说,就是通过框架把持久层对象传入业务层,而不用我们自己去获取。

5.6 Bean依赖注入方式

5.6.1 构造方法

在UserServiceImpl中创建有参构造

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public UserServiceImpl(UserDao userDao) {
    this.userDao = userDao;
} 
@Override
public void save() {
    userDao.save();
    }
}

配置Spring容器调用有参构造时进行注入

<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.lagou.service.impl.UserServiceImpl">
<!--<constructor-arg index="0" type="com.lagou.dao.UserDao" ref="userDao"/> -->
<constructor-arg name="userDao" ref="userDao"/>
</bean>
5.6.2 set方法

在UserServiceImpl中创建set方法

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
} 
@Override
public void save() {
    userDao.save();
    }
}

配置Spring容器调用set方法进行注入

<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.lagou.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
5.6.3 P命名空间注入

P命名空间注入本质也是set方法注入,但比起上述的set方法注入更加方便,主要体现在配置文件中,如下:
首先,需要引入P命名空间:

xmlns:p="http://www.springframework.org/schema/p"

其次,需要修改注入方式:

<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.lagou.service.impl.UserServiceImpl"p:userDao-ref="userDao"/>

5.7 Bean依赖注入的数据类型

上面操作,都是注入Bean对象,除了对象的引用可以注入,普通数据类型和集合都可以在容器中进行注入。
注入数据的三种数据类型
\1. 普通数据类型
\2. 引用数据类型
\3. 集合数据类型
其中引用数据类型,此处就不再赘述了,之前的操作都是对UserDao对象的引用进行注入的。下面将以set方法注入为例,演示普通数据类型和集合数据类型的注入。

5.7.1 注入普通数据类型
public class User {
    private String username;
    private String age;
    public void setUsername(String username) {
    this.username = username;
} 
public void setAge(String age) {
    this.age = age;
    }
}
<bean id="user" class="com.lagou.domain.User">
<property name="username" value="jack"/>
<property name="age" value="18"/>
</bean>
5.7.2 注入集合数据类型

1)List集合注入

public class UserDaoImpl implements UserDao {
    private List<Object> list;
    public void save() {
    System.out.println(list);
    System.out.println("保存成功了...");
    }
}
<bean id="user" class="com.lagou.domain.User">
<property name="username" value="jack"/>
<property name="age" value="18"/>
</bean>
<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl">
<property name="list">
<list>
<value>aaa</value>
<ref bean="user"></ref>
</list>
</property>
</bean>

2)Set集合注入

public class UserDaoImpl implements UserDao {
    private Set<Object> set;
    public void setSet(Set<Object> set) {
    this.set = set;
} 
public void save() {
    System.out.println(set);
    System.out.println("保存成功了...");
    }
}
<bean id="user" class="com.lagou.domain.User">
<property name="username" value="jack"/>
<property name="age" value="18"/>
</bean>
<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl">
<property name="set">
<list>
<value>bbb</value>
<ref bean="user"></ref>
</list>
</property>
</bean>

3)Array数组注入

public class UserDaoImpl implements UserDao {
    private Object[] array;
    public void setArray(Object[] array) {
    this.array = array;
} 
public void save() {
    System.out.println(Arrays.toString(array));
    System.out.println("保存成功了...");
    }
}
<bean id="user" class="com.lagou.domain.User">
<property name="username" value="jack"/>
<property name="age" value="18"/>
</bean>
<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl">
<property name="array">
<array>
<value>ccc</value>
<ref bean="user"></ref>
</array>
</property>
</bean>

4)Map集合注入

public class UserDaoImpl implements UserDao {
    private Map<String, Object> map;
    public void setMap(Map<String, Object> map) {
    this.map = map;
} 
public void save() {
    System.out.println(map);
    System.out.println("保存成功了...");
    }
}
<bean id="user" class="com.lagou.domain.User">
<property name="username" value="jack"/>
<property name="age" value="18"/>
</bean>
<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl">
<property name="map">
<map>
<entry key="k1" value="ddd"/>
<entry key="k2" value-ref="user"></entry>
</map>
</property>
</bean>

5)Properties配置注入

public class UserDaoImpl implements UserDao {
    private Properties properties;
    public void setProperties(Properties properties) {
    this.properties = properties;
} 
public void save() {
    System.out.println(properties);
    System.out.println("保存成功了...");
    }
}
<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl">
<property name="properties">
<props>
<prop key="k1">v1</prop>
<prop key="k2">v2</prop>
<prop key="k3">v3</prop>
</props>
</property>
</bean>

5.8 配置文件模块化

实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置拆解到其他配置文件中,也就是所谓的配置文件模块化。
1)并列的多个配置文件

ApplicationContext act = new ClassPathXmlApplicationContext("beans1.xml","beans2.xml","...");

2)主从配置文件

<import resource="applicationContext-xxx.xml"/>

注意:
同一个xml中不能出现相同名称的bean,如果出现会报错
多个xml如果出现相同名称的bean,不会报错,但是后加载的会覆盖前加载的bean

5.9 知识小结

Spring的重点配置

<bean>标签:创建对象并放到spring的IOC容器
id属性:在容器中Bean实例的唯一标识,不允许重复
class属性:要实例化的Bean的全限定名
scope属性:Bean的作用范围,常用是Singleton(默认)和prototype
<constructor-arg>标签:属性注入
name属性:属性名称
value属性:注入的普通属性值
ref属性:注入的对象引用值
<property>标签:属性注入
name属性:属性名称
value属性:注入的普通属性值
ref属性:注入的对象引用值
<list>
<set>
<array>
<map>
<props>
<import>标签:导入其他的Spring的分文件

6. Spring注解开发

Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。

6.1 Spring常用注解

6.1.1 介绍

Spring常用注解主要是替代 bean的配置

注解说明
@Component使用在类上用于实例化Bean
@Controller使用在web层类上用于实例化Bean
@Service使用在service层类上用于实例化Bean
@Repository使用在dao层类上用于实例化Bean
@Autowired使用在字段上用于根据类型依赖注入
@Qualifier结合@Autowired一起使用,根据名称进行依赖注入
@Resource相当于@Autowired+@Qualifier,按照名称进行注入
@Value注入普通属性
@Scope标注Bean的作用范围
@PostConstruct使用在方法上标注该方法是Bean的初始化方法
@PreDestroy使用在方法上标注该方法是Bean的销毁方法

说明:
JDK11以后完全移除了javax扩展导致不能使用@resource注解

需要maven引入依赖
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>

注意
使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包
下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法。

<!--注解的组件扫描-->
<context:component-scan base-package="com.lagou"></context:component-scan>

6.1.2 实现

1)Bean实例化(IOC)

<bean id="userDao" class="com.lagou.dao.impl.UserDaoImpl"></bean>

使用@Compont或@Repository标识UserDaoImpl需要Spring进行实例化。

// @Component(value = "userDao")
@Repository // 如果没有写value属性值,Bean的id为:类名首字母小写
public class UserDaoImpl implements UserDao {
}

2)属性依赖注入(DI)

<bean id="userService" class="com.lagou.service.impl.UserServiceImpl">
<property name="userDao" ref="userDaoImpl"/>
</bean>

使用@Autowired或者@Autowired+@Qulifier或者@Resource进行userDao的注入

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    // <property name="userDao" ref="userDaoImpl"/>
    // @Autowired
    // @Qualifier("userDaoImpl")
    // @Resource(name = "userDaoImpl")
	public void setUserDao(UserDao userDao) {
    	this.userDao = userDao;
    }
}

3)@Value
使用@Value进行字符串的注入,结合SPEL表达式获得配置参数

@Service
public class UserServiceImpl implements UserService {
    @Value("注入普通数据")
    private String str;
    @Value("${jdbc.driver}")
    private String driver;
}

4)@Scope

<bean scope=""/>

使用@Scope标注Bean的范围

@Service
@Scope("singleton")
public class UserServiceImpl implements UserService {{
}

5)Bean生命周期

<bean init-method="init" destroy-method="destory" />

使用@PostConstruct标注初始化方法,使用@PreDestroy标注销毁方法

@PostConstruct
public void init(){
System.out.println("初始化方法....");
} 
@PreDestroy
public void destroy(){
System.out.println("销毁方法.....");
}

6.2 Spring常用注解整合DbUtils

步骤分析

1. 拷贝xml配置项目,改为注解配置项目
2. 修改AccountDaoImpl实现类
3. 修改AccountServiceImpl实现类
4. 修改spring核心配置文件
5. 编写测试代码

1)拷贝xml配置项目,改为常用注解配置项目
2)修改AccountDaoImpl实现类

@Repository
public class AccountDaoImpl implements AccountDao {
@Autowired
private QueryRunner queryRunner;
....
}

3)修改AccountServiceImpl实现类

@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
....
}

4)修改spring核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w1.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">
<!--注解的组件扫描-->
<context:component-scan base-package="com.lagou"></context:component-scan>
<!--加载jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties">
</context:property-placeholder>
<!--把数据库连接池交给IOC容器-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--把QueryRunner交给IOC容器-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
</beans>

5)编写测试代码

public class AccountServiceTest {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    AccountService accountService =
    applicationContext.getBean(AccountService.class);
    //测试查询
    @Test
    public void findByIdTest() {
    Account account = accountService.findById(3);
    System.out.println(account);
    }
}

6.3 Spring新注解

使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:

* 非自定义的Bean的配置:<bean>
* 加载properties文件的配置:<context:property-placeholder>
* 组件扫描的配置:<context:component-scan>
* 引入其他文件:<import>
注解说明
@Configuration用于指定当前类是一个Spring 配置类,当创建容器时会从该类上加载注解
@Bean使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource用于加载 properties 文件中的配置
@ComponentScan用于指定 Spring 在初始化容器时要扫描的包
@Import用于导入其他配置类

6.4 Spring纯注解整合DbUtils

步骤分析

1. 编写Spring核心配置类
2. 编写数据库配置信息类
3. 编写测试代码

1)编写Spring核心配置类

@Configuration
@ComponentScan("com.lagou")
@Import(DataSourceConfig.class)
public class SpringConfig {
    @Bean("queryRunner")
    public QueryRunner getQueryRunner(@Autowired DataSource dataSource) {
    return new QueryRunner(dataSource);
    }
}

2)编写数据库配置信息类

@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Bean("dataSource")
    public DataSource getDataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
    }
}

3)编写测试代码

public class AccountServiceTest {
ApplicationContext applicationContext =
 new AnnotationConfigApplicationContext(SpringConfig.class);
AccountService accountService = applicationContext.getBean(AccountService.class);
//测试查询
@Test
public void testFindById() {
Account account = accountService.findById(3);
    System.out.println(account);
    }
}

7. Spring整合Junit

7.1普通Junit测试问题

在普通的测试类中,需要开发者手动加载配置文件并创建Spring容器,然后通过Spring相关API获得Bean实例;如果不这么做,那么无法从容器中获得对象。

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);

我们可以让SpringJunit负责创建Spring容器来简化这个操作,开发者可以直接在测试类注入Bean实例;但是需要将配置文件的名称告诉它。

7.2 Spring整合Junit

步骤分析

1. 导入spring集成Junit的坐标
2. 使用@Runwith注解替换原来的运行器
3. 使用@ContextConfiguration指定配置文件或配置类
4. 使用@Autowired注入需要测试的对象
5. 创建测试方法进行测试

1)导入spring集成Junit的坐标

<!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

2)使用@Runwith注解替换原来的运行器

@RunWith(SpringJUnit4ClassRunner.class)
	public class SpringJunitTest {
}

3)使用@ContextConfiguration指定配置文件或配置类

@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(value = {"classpath:applicationContext.xml"}) 加载spring
核心配置文件
@ContextConfiguration(classes = {SpringConfig.class}) // 加载spring核心配置类
public class SpringJunitTest {
}

4)使用@Autowired注入需要测试的对象

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
public class SpringJunitTest {
@Autowired
private AccountService accountService;
}

5)创建测试方法进行测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringConfig.class})
public class SpringJunitTest {
@Autowired
private AccountService accountService;
//测试查询
@Test
public void testFindById() {
Account account = accountService.findById(3);
System.out.println(account);
}
}

二、AOP

1. 初识AOP

1.1 什么是AOP

AOP 为 Aspect Oriented Programming 的缩写,意思AOP 是 OOP(面向对象编程) 的延续,是软件开发中
容,利用AOP可以对业务逻辑的各个部分进行隔离,从而程序的可重用性,同时提高了开发的效率。
这样做的好处是:

\1. 在程序运行期间,在不修改源码的情况下对方法进行功能增强
\2. 逻辑清晰,开发核心业务的时候,不必关注增强业务的代码
\3. 减少重复代码,提高开发效率,便于后期维护

1.2 AOP底层实现

实际上,AOP 的底层是通过 Spring 提供的的动态代理技术实现的。在运行期间,Spring通过动态代理技术动态的生成代理对象,代理对象方法执行时进行增强功能的介入,在去调用目标对象的方法,从而完成功能的增强。

1.3 AOP相关术语

Spring 的 AOP 实现底层就是对上面的动态代理的代码进行了封装,封装后我们只需要对需要关注的
部分进行代码编写,并通过配置的方式完成指定目标的方法增强。
在正式讲解 AOP 的操作之前,我们必须理解 AOP 的相关术语,常用的术语如下:

* Target(目标对象):代理的目标对象
* Proxy (代理):一个类被 AOP 织入增强后,就产生一个结果代理类
* Joinpoint(连接点):所谓连接点是指那些可以被拦截到的点。在spring中,这些点指的是方法,因为
spring只支持方法类型的连接点
* Pointcut(切入点):所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义
* Advice(通知/ 增强):所谓通知是指拦截到 Joinpoint 之后所要做的事情就是通知
分类:前置通知、后置通知、异常通知、最终通知、环绕通知
* Aspect(切面):是切入点和通知(引介)的结合
* Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织
入,而AspectJ采用编译期织入和类装载期织入

1.4 AOP开发明确事项

1.4.1 开发阶段(我们做的)

\1. 编写核心业务代码(目标类的目标方法) 切入点
\2. 把公用代码抽取出来,制作成通知(增强功能方法) 通知
\3. 在配置文件中,声明切入点与通知间的关系,即切面

1.4.2 运行阶段(Spring框架完成的)

Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象,根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行

1.4.3 底层代理实现

在 Spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。
当bean实现接口时,会用JDK代理模式
当bean没有实现接口,用cglib实现( 可以强制使用cglib(在spring配置中加入<aop:aspectjautoproxy proxyt-target-class=”true”/>)

1.5 知识小结

* aop:面向切面编程
* aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理
* aop的重点概念:
Pointcut(切入点):真正被增强的方法
Advice(通知/ 增强):封装增强业务逻辑的方法
Aspect(切面):切点+通知
Weaving(织入):将切点与通知结合,产生代理对象的过程

2. 基于XML的AOP开发

2.1 快速入门

步骤分析

1. 创建java项目,导入AOP相关坐标
2. 创建目标接口和目标实现类(定义切入点)
3. 创建通知类及方法(定义通知)
4. 将目标类和通知类对象创建权交给spring
5. 在核心配置文件中配置织入关系,及切面
6. 编写测试代码

2.1.1 创建java项目,导入AOP相关坐标

<dependencies>
<!--导入spring的context坐标,context依赖aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- aspectj的织入(切点表达式需要用到该jar包) -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2.1.2 创建目标接口和目标实现类
public interface AccountService {
	public void transfer();
}
public class AccountServiceImpl implements AccountService {
    @Override
    public void transfer() {
    System.out.println("转账业务...");
    }
}
2.1.3 创建通知类
public class MyAdvice {
    public void before() {
    System.out.println("前置通知...");
    }
}
2.1.4 将目标类和通知类对象创建权交给spring
<!--目标类交给IOC容器-->
<bean id="accountService" class="com.lagou.service.impl.AccountServiceImpl">
</bean>
<!--通知类交给IOC容器-->
<bean id="myAdvice" class="com.lagou.advice.MyAdvice"></bean>
2.1.5 在核心配置文件中配置织入关系,及切面

导入aop命名空间

<?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: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">
<!--目标类交给IOC容器-->
<bean id="accountService" class="com.lagou.service.impl.AccountServiceImpl">
</bean>
<!--通知类交给IOC容器-->
<bean id="myAdvice" class="com.lagou.advice.MyAdvice"></bean>
<aop:config>
<!--引入通知类-->
<aop:aspect ref="myAdvice">
<!--配置目标类的transfer方法执行时,使用通知类的before方法进行前置增强-->
<aop:before method="before"
pointcut="execution(public void
com.lagou.service.impl.AccountServiceImpl.transfer())"></aop:before>
</aop:aspect>
</aop:config>
</beans>
2.1.6 编写测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
class AccountServiceTest {
@Autowired
private AccountService accountService;
@Test
public void testTransfer() throws Exception {
accountService.transfer();
}
}

2.2 XML配置AOP详解

2.2.1 切点表达式

表达式语法:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
访问修饰符可以省略
返回值类型、包名、类名、方法名可以使用星号 * 代替,代表任意
包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类
参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表  

例如:

execution(public void com.lagou.service.impl.AccountServiceImpl.transfer())
execution(void com.lagou.service.impl.AccountServiceImpl.*(..))
execution(* com.lagou.service.impl.*.*(..))
execution(* com.lagou.service..*.*(..))

切点表达式抽取
当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替
pointcut 属性来引用抽取后的切点表达式。

<aop:config>
<!--抽取的切点表达式-->
<aop:pointcut id="myPointcut" expression="execution(* com.lagou.service..*.*
(..))"> </aop:pointcut>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="myPointcut"></aop:before>
</aop:aspect>
</aop:config>
2.2.2 通知类型

通知的配置语法:

<aop:通知类型 method=“通知类中方法名” pointcut=“切点表达式"></aop:通知类型>
名称标签说明
前置通 知aop:before用于配置前置通知。指定增强的方法在切入点方法之前执 行
后置通 知aop:afterReturning用于配置后置通知。指定增强的方法在切入点方法之后执 行
异常通 知aop:afterThrowing用于配置异常通知。指定增强的方法出现异常后执行
最终通 知aop:after用于配置最终通知。无论切入点方法执行时是否有异常, 都会执行
环绕通 知aop:around用于配置环绕通知。开发者可以手动控制增强代码在什么 时候执行

注意:通常情况下,环绕通知都是独立使用的

2.3 知识小结

* aop织入的配置
<aop:config>
<aop:aspect ref=“通知类”>
<aop:before method=“通知方法名称” pointcut=“切点表达式"></aop:before>
</aop:aspect>
</aop:config>
* 通知的类型
前置通知、后置通知、异常通知、最终通知
环绕通知
* 切点表达式
execution([修饰符] 返回值类型 包名.类名.方法名(参数))

3. 基于注解的AOP开发

3.1 快速入门

步骤分析

1. 创建java项目,导入AOP相关坐标
2. 创建目标接口和目标实现类(定义切入点)
3. 创建通知类(定义通知)
4. 将目标类和通知类对象创建权交给spring
5. 在通知类中使用注解配置织入关系,升级为切面类
6. 在配置文件中开启组件扫描和 AOP 的自动代理
7. 编写测试代码
3.1.1 创建java项目,导入AOP相关坐标
<dependencies>
<!--导入spring的context坐标,context依赖aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- aspectj的织入 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
3.1.2 创建目标接口和目标实现类
public interface AccountService {
	public void transfer();
}
public class AccountServiceImpl implements AccountService {
    @Override
    public void transfer() {
    System.out.println("转账业务...");
    }
}
3.1.3 创建通知类
public class MyAdvice {
    public void before() {
    System.out.println("前置通知...");
    }
}
3.1.4 将目标类和通知类对象创建权交给spring
@Service
public class AccountServiceImpl implements AccountService {}
@Component
public class MyAdvice {}
3.1.5 在通知类中使用注解配置织入关系,升级为切面类
@Component
@Aspect
public class MyAdvice {
    @Before("execution(* com.lagou..*.*(..))")
    public void before() {
    System.out.println("前置通知...");
    }
}
3.1.6 在配置文件中开启组件扫描和 AOP 的自动代理
<!--组件扫描-->
<context:component-scan base-package="com.lagou"/>
<!--aop的自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
3.1.7 编写测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
class AccountServiceTest {
    @Autowired
    private AccountService accountService;
    @Test
    public void testTransfer() throws Exception {
    accountService.transfer();
    }
}

3.2 注解配置AOP详解

3.2.1 切点表达式

切点表达式的抽取

@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(* com.lagou..*.*(..))")
    public void myPoint(){}
    @Before("MyAdvice.myPoint()")
    public void before() {
    System.out.println("前置通知...");
}
3.2.2 通知类型

通知的配置语法:@通知注解(“切点表达式")

名称标签说明
前置通 知@Before用于配置前置通知。指定增强的方法在切入点方法之前执行
后置通 知@AfterReturning用于配置后置通知。指定增强的方法在切入点方法之后执行
异常通 知@AfterThrowing用于配置异常通知。指定增强的方法出现异常后执行
最终通 知@After用于配置最终通知。无论切入点方法执行时是否有异常,都会 执行
环绕通 知@Around用于配置环绕通知。开发者可以手动控制增强代码在什么时候 执行

注意:
当前四个通知组合在一起时,执行顺序如下:
@Before -> @After -> @AfterReturning(如果有异常:@AfterThrowing)

3.2.3 纯注解配置
@Configuration
@ComponentScan("com.lagou")
@EnableAspectJAutoProxy //替代 <aop:aspectj-autoproxy />
public class SpringConfig {
}

3.3 知识小结

* 使用@Aspect注解,标注切面类
* 使用@Before等注解,标注通知方法
* 使用@Pointcut注解,抽取切点表达式
* 配置aop自动代理 <aop:aspectj-autoproxy/> 或 @EnableAspectJAutoProxy

三、 Spring JDBCTemplate & 声明式事务

1. Spring的JdbcTemplate

1.1 JdbcTemplate是什么?

JdbcTemplate是spring框架中提供的一个模板对象,是对原始繁琐的Jdbc API对象的简单封装。

核心对象
JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSource dataSource);

核心方法
int update(); 执行增、删、改语句
List query(); 查询多个
T queryForObject(); 查询一个
new BeanPropertyRowMapper<>(); 实现ORM映射封装

查询数据库所有账户信息到Account实体中

public class JdbcTemplateTest {
@Test
public void testFindAll() throws Exception {
// 创建核心对象
JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());
// 编写sql
String sql = "select * from account";
// 执行sql
List<Account> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>
(Account.class));
}
}

1.2 Spring整合JdbcTemplate

需求
基于Spring的xml配置实现账户的CRUD案例
步骤分析

1. 创建java项目,导入坐标
2. 编写Account实体类
3. 编写AccountDao接口和实现类
4. 编写AccountService接口和实现类
5. 编写spring核心配置文件
6. 编写测试代码

1)创建java项目,导入坐标

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.15</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
    <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
</dependencies>

2)编写Account实体类

public class Account {
private Integer id;
private String name;
private Double money;
}

3)编写AccountDao接口和实现类

public interface AccountDao {
    public List<Account> findAll();
    public Account findById(Integer id);
    public void save(Account account);
    public void update(Account account);
    public void delete(Integer id);
}
@Repository
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<Account> findAll() {
// 编写sql
String sql = "select * from account";
// 执行sql
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>
(Account.class));
} 
@Override
public Account findById(Integer id) {
// 编写sql
String sql = "select * from account where id = ?";
// 执行sql
return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>
(Account.class),id);
} 
@Override
public void save(Account account) {
// 编写sql
String sql = "insert into account values(null,?,?)";
// 执行sql
jdbcTemplate.update(sql, account.getName(), account.getMoney());
} 
@Override
public void update(Account account) {
// 编写sql
String sql = "update account set name = ?,money = ? where id = ?";
// 执行sql
jdbcTemplate.update(sql, account.getName(),
account.getMoney(),account.getId());
} @
Override
public void delete(Integer id) {
// 编写sql
String sql = "delete from account where id = ?";
// 执行sql
jdbcTemplate.update(sql, id);
}
}

4)编写AccountService接口和实现类

public interface AccountService {
public List<Account> findAll();
public Account findById(Integer id);
public void save(Account account);
public void update(Account account);
public void delete(Integer id);
}
@
Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
public List<Account> findAll() {
return accountDao.findAll();
} 
@Override
public Account findById(Integer id) {
return accountDao.findById(id);
} 
@Override
public void save(Account account) {
accountDao.save(account);
} 
@Override
public void update(Account account) {
accountDao.update(account);
}
@Override
public void delete(Integer id) {
accountDao.delete(id);
}
}

5)编写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: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">
<context:component-scan base-package="com.lagou"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
</beans>

6)编写测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AccountServiceTest {
@Autowired
private AccountService accountService;
//测试保存
@Test
public void testSave() {
Account account = new Account();
account.setName("lucy");
account.setMoney(100d);
accountService.save(account);
} /
/测试查询
@Test
public void testFindById() {
Account account = accountService.findById(3);
System.out.println(account);
} /
/测试查询所有
@Test
public void testFindAll() {
List<Account> accountList = accountService.findAll();
for (Account account : accountList) {
System.out.println(account);
}
} /
/测试修改
@Test
public void testUpdate() {
Account account = new Account();
account.setId(3);
account.setName("rose");
account.setMoney(2000d);
accountService.update(account);
} /
/测试删除
@Test
public void testDelete() {
accountService.delete(3);
}
}

2. Spring的事务

2.1 Spring中的事务控制方式

Spring的事务控制可以分为编程式事务控制和声明式事务控制。
编程式
开发者直接把事务的代码和业务代码耦合到一起,在实际开发中不用。
声明式
开发者采用配置的方式来实现的事务控制,业务代码与事务代码实现解耦合,使用的AOP思想。

2.2 编程式事务控制相关对象【了解】

2.2.1 PlatformTransactionManager

PlatformTransactionManager接口,是spring的事务管理器,里面提供了我们常用的操作事务的方
法。

方法说明
TransactionStatus getTransaction(TransactionDefinition definition);获取事务的状态信息
void commit(TransactionStatus status);提交事务
void rollback(TransactionStatus status);回滚事务
* PlatformTransactionManager 是接口类型,不同的 Dao 层技术则有不同的实现类。
* Dao层技术是jdbcTemplate或mybatis时:
DataSourceTransactionManager
* Dao层技术是hibernate时:
HibernateTransactionManager
* Dao层技术是JPA时:
JpaTransactionManager

2.2.2 TransactionDefinition

TransactionDefinition接口提供事务的定义信息(事务隔离级别、事务传播行为等等)

方法说明
int getIsolationLevel()获得事务的隔离级别
int getPropogationBehavior()获得事务的传播行为
int getTimeout()获得超时时间
boolean isReadOnly()是否只读

1)事务隔离级别
设置隔离级别,可以解决事务并发产生的问题,如脏读、不可重复读和虚读(幻读)。

* ISOLATION_DEFAULT 使用数据库默认级别
* ISOLATION_READ_UNCOMMITTED 读未提交
* ISOLATION_READ_COMMITTED 读已提交
* ISOLATION_REPEATABLE_READ 可重复读
* ISOLATION_SERIALIZABLE 串行化

2)事务传播行为
事务传播行为指的就是当一个业务方法【被】另一个业务方法调用时,应该如何进行事务控制。

参数说明
REQUIRED如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到 这个事务中。一般的选择(默认值)
SUPPORTS支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)
MANDATORY使用当前的事务,如果当前没有事务,就抛出异常
REQUERS_NEW新建事务,如果当前在事务中,把当前事务挂起
NOT_SUPPORTED以非事务方式执行操作,如果当前存在事务,就把当前事务挂起
NEVER以非事务方式运行,如果当前存在事务,抛出异常
NESTED如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作
* read-only(是否只读):建议查询时设置为只读
* timeout(超时时间):默认值是-1,没有超时限制。如果有,以秒为单位进行设置
2.2.3 TransactionStatus

TransactionStatus 接口提供的是事务具体的运行状态。

方法说明
boolean isNewTransaction()是否是新事务
boolean hasSavepoint()是否是回滚点
boolean isRollbackOnly()事务是否回滚
boolean isCompleted()事务是否完成

可以简单的理解三者的关系:事务管理器通过读取事务定义参数进行事务管理,然后会产生一系列的事
务状态。

2.2.4 实现代码

1)配置文件

<!--事务管理器交给IOC-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

2)业务层代码

@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Autowired
private PlatformTransactionManager transactionManager;
@Override
public void transfer(String outUser, String inUser, Double money) {
// 创建事务定义对象
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
// 设置是否只读,false支持事务
def.setReadOnly(false);
// 设置事务隔离级别,可重复读mysql默认级别
def.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
// 设置事务传播行为,必须有事务
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
// 配置事务管理器
TransactionStatus status = transactionManager.getTransaction(def);
try {
// 转账
accountDao.out(outUser, money);
accountDao.in(inUser, money);
// 提交事务
transactionManager.commit(status);
} catch (Exception e) {
e.printStackTrace();
// 回滚事务
transactionManager.rollback(status);
}
}
}
2.2.5 知识小结

Spring中的事务控制主要就是通过这三个API实现的

* PlatformTransactionManager 负责事务的管理,它是个接口,其子类负责具体工作
* TransactionDefinition 定义了事务的一些相关参数
* TransactionStatus 代表事务运行的一个实时状态

2.3 基于XML的声明式事务控制【重点】

在 Spring 配置文件中声明式的处理事务来代替代码式的处理事务。底层采用AOP思想来实现的。

2.3.1 快速入门

需求
使用spring声明式事务控制转账业务。
步骤分析

1. 引入tx命名空间
2. 事务管理器通知配置
3. 事务管理器AOP配置
4. 测试事务控制转账业务代码

1)引入tx命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w2.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/s chema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>

2)事务管理器通知配置

<!--事务管理器-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--通知增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--定义事务的属性-->
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

3)事务管理器AOP配置

<!--aop配置-->
<aop:config>
<!--切面配置-->
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.lagou.serivce..*.*(..))">
</aop:advisor>
</aop:config>

4)测试事务控制转账业务代码

@Override
public void transfer(String outUser, String inUser, Double money) {
accountDao.out(outUser, money);
// 制造异常
int i = 1 / 0;
accountDao.in(inUser, money);
}
2.3.2 事务参数的配置详解
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED"
timeout="-1" read-only="false"/>
* name:切点方法名称
* isolation:事务的隔离级别
* propogation:事务的传播行为
* timeout:超时时间
* read-only:是否只读

CRUD常用配置

<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>

2.4 基于注解的声明式事务控制【重点】

2.4.1 常用注解

1)修改service层,增加事务注解

@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Transactional(propagation = Propagation.REQUIRED, isolation =
Isolation.REPEATABLE_READ, timeout = -1, readOnly = false)
@Override
public void transfer(String outUser, String inUser, Double money) {
accountDao.out(outUser, money);
int i = 1 / 0;
accountDao.in(inUser, money);
}
}

2)修改spring核心配置文件,开启事务注解支持

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w2.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--省略之前datsSource、jdbcTemplate、组件扫描配置-->
<!--事务管理器-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务的注解支持-->
<tx:annotation-driven/>
</beans>
2.4.2 纯注解

核心配置类

@Configuration // 声明为spring配置类
@ComponentScan("com.lagou") // 扫描包
@Import(DataSourceConfig.class) // 导入其他配置类
@EnableTransactionManagement // 事务的注解驱动
public class SpringConfig {
@Bean
public JdbcTemplate getJdbcTemplate(@Autowired DataSource dataSource) {
return new JdbcTemplate(dataSource);
} @
Bean("transactionManager")
public PlatformTransactionManager getPlatformTransactionManager(@Autowired
DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}

数据源配置类

@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
2.4.3 知识小结
* 平台事务管理器配置(xml、注解方式)
* 事务通知的配置(@Transactional注解配置)
* 事务注解驱动的配置 <tx:annotation-driven/>、@EnableTransactionManagement

3. Spring集成web环

3.1 ApplicationContex

应用上下文对象是通过 new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写 new ClasspathXmlApplicationContext(spring配置文件) ,
这样的弊端是配置文件加载多次,应用上下文对象创建多次。

解决思路分析:
在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

3.2 Spring提供获取应用上下文的工具

上面的分析不用手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:
\1. 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
\2. 使用WebApplicationContextUtils获得应用上下文对象ApplicationContext

3.3 实现

1)导入Spring集成web的坐标

<dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

2)配置ContextLoaderListener监听器

<!--全局参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value></context-param>
<!--Spring的监听器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

3)通过工具获得应用上下文对象

ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
Object obj = applicationContext.getBean("id");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值