1. spring概述

1.1 Spring是什么(理解)

Spring是分层的 Java SE/EE应用 full-stack(全栈的) 轻量级开源框架,以 IOC(Inverse Of Control:控制反转)和 AOP(Aspect Oriented Programming:面向切面编程)为内核。

提供了展现层 SpringMVC持久层 Spring JDBCTemplate以及业务层事务管理等众多的企业应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架
 

1.2 Spring发展历程 (了解)

1997年,IBM提出了EJB的思想
1998年,SUN制定开发标准规范EB1.0
1999年,EB1.1发布
2001年,EB2.0发布
2003年,EB2.1发布
2006年,EB3.0发布

RodJohnson(Spring 之父)

Expert One-to-OneJ2EEDesign and Development(2002)
阐述了J2EE使用EB开发设计的优点及解决方室
Expert One-to-OneJ2EE Developmentwithout EJB(2004)
阐述了J2EE开发不使用EJB的解决方式(Spring雏形)

2017 年 9 月份发布了 Spring 的最新版本 Spring5.0 通用版(GA)
 

1.3 Spring的优势(理解)

1.3.1 方便解耦,简化开发

通过Spring 提供的IOC容器,可以将对象间的依赖关系交由Spring进行控制,避免硬编码所造成的过度耦合用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用

1.3.2 AOP 编程的支持

通过 Spring的AOP功能,方便进行面向切面编程,许多不容易用传统OOP实现的功能可以通过AOP轻松实现

1.3.3 声明式事务的支持

可以将我们从单调烦闷的事务管理代码中解脱出来,通讨声明式方式灵活的进行事务管理,提高开发效率和质量

1.3.4 方便程序的测试

可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可做的事情

1.3.5 方便集成各种优秀框架

Spring对各种优秀框架(Struts、Hibemate、Hessian、Quartz等)的支持

1.3.6 隆低JavaEE API的使用难度

Spring对JaveEE API(如JDBC、JavaMail、远程调用等)进行了薄薄的封装层,使这些API的使用难度大为降低

1.3.7 Java 源码是经典学习范例

Spring的源代码设计精妙、结构清晰、匠心独用,处处体现着大师对Java 设计模式灵活运用以及对Java技术的高深造诣。它的源代码无意是Java技术的最佳实践的范例
 

1.4 Spring的体系结构(了解)


 

2. spring快速入门

2.1 Spring程序开发步骤

①导入Spring 坐标
②创建Bean
③创建String核心配置文件 applicationContext.xml
④在Spring配置文件中配置Bean
⑤创建ApplicationContext对象,通过getBean方法获得Bean实例

2.2 导入Spring开发的基本包坐标

<properties>
    <spring.version>5.0.5.RELEASE</spring.version>
</properties>

<!--导入spring的context坐标,context依赖core、beans、expression-->
<dependencies>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>${spring.version}</version>
     </dependency>
</dependencies>


2.3 编写Dao接口和实现类

public interface UserDao {
    public void save();
}

public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("UserDao save method running....");
    }
}


2.4 创建Spring核心配置文件

在类路径下(resources)创建applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework/schema/beans"
       xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework/schema/beans http://www.springframework/schema/beans/spring-beans.xsd">
</beans>


2.5 在Spring配置文件中配置UserDaoImpl

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework/schema/beans"
       xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework/schema/beans http://www.springframework/schema/beans/spring-beans.xsd">
    
    <bean id="userDao" class="com.terence.dao.impl.UserDaoImpl"></bean>
    
</beans>


2.6 使用Spring的API获得Bean实例

public class UserDaoDemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");
        userDao.save();
    }
}


3. Spring配置文件

3.1 Bean标签基本配置

用于配置对象交由Spring 来创建。
默认情况下它调用的是类中的无参构造函数,如果没有无参构造函数则不能创建成功。

基本属性:
id:Bean实例在Spring容器中的唯一标识
class:Bean的全限定名称
 

3.2 Bean标签范围配置

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

取值范围说明
singleton默认值,单例的
prototype多例的
requestWEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中
sessionWEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中
global sessionWEB 项目中,应用在 Portlet 环境,如果没有 Portlet 环境那么globalSession 相当于 session

3.2.1 当scope的取值为singleton时

Bean的实例化个数:1个
Bean的实例化时机:当Spring核心文件被加载时,实例化配置的Bean实例

Bean的生命周期:

  • 对象创建:当应用加载,创建容器时,对象就被创建了
  • 对象运行:只要容器在,对象一直活着
  • 对象销毁:当应用卸载,销毁容器时,对象就被销毁了

3.2.2 当scope的取值为prototype时

Bean的实例化个数:多个
Bean的实例化时机:当调用getBean()方法时实例化Bean

  • 对象创建:当使用对象时,创建新的对象实例
  • 对象运行:只要对象在使用中,就一直活着
  • 对象销毁:当对象长时间不用时,被 Java 的垃圾回收器回收了
     

3.3 Bean生命周期配置

init-method:指定类中的初始化方法名称

destroy-method:指定类中销毁方法名称
 

3.4 Bean实例化三种方式

3.4.1 使用无参构造方法实例化

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

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

3.4.2 工厂静态方法实例化

工厂的静态方法返回Bean实例

public class StaticFactory {
    public static UserDao getUserDao() {
        return new UserDaoImpl();
    }
}
<bean id="userDao" class="com.terence.factory.StaticFactory" factory-method="getUserDao" />

3.4.3  工厂实例方法实例化

工厂的非静态方法返回Bean实例

public class DynamicFactory {
    public UserDao getUserDao() {
        return new UserDaoImpl();
    }
}
<bean id="factoryBean" class="com.terence.factory.DynamicFactory"/>
<bean id="userDao" factory-bean="factoryBean" factory-method="getUserDao"/>


3.5 Bean的依赖注入概念

依赖注入(Dependency Injection):它是 Spring 框架核心 IOC 的具体实现。

在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。

IOC 解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。

那这种业务层和持久层的依赖关系,在使用 Spring 之后,就让 Spring 来维护了。

简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取

如下图:

3.6 Bean的依赖注入方式

3.6.1 构造方法

创建有参构造

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.terence.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.terence.service.impl.UserServiceImpl">
    <constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>

3.6.2 set方法

在UserServiceImpl中添加setUserDao方法

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="userService" class="com.terence.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"></property>
</bean>

set方法:P命名空间注入

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

首先,需要引入P命名空间:

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

其次,需要修改注入方式

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


3.7 Bean的依赖注入的数据类型

上面的操作,都是注入的引用Bean,处了对象的引用可以注入,普通数据类型,集合等都可以在容器中进行注入。

注入数据的三种数据类型

  • 普通数据类型
  • 引用数据类型
  • 集合数据类型

其中引用数据类型,此处就不再赘述了,之前的操作都是对UserDao对象的引用进行注入的,下面将以set方法注入为例,演示普通数据类型和集合数据类型的注入。

Bean的依赖注入的数据类型

3.7.1 普通数据类型的注入

public class UserDaoImpl implements UserDao {
    String username;
    String age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "UserDaoImpl{" +
                "username='" + username + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}
<bean id="userDao" class="com.terence.dao.impl.UserDaoImpl">
    <property name="username" value="传智播客"></property>
    <property name="age" value="15"></property>
</bean>

3.7.2 集合数据类型(List<String>)的注入

public class UserDaoImpl implements UserDao {

    List<String> strList;
    
    public List<String> getStrList() {
        return strList;
    }

    public void setStrList(List<String> strList) {
        this.strList = strList;
    }
}
<bean id="userDao" class="com.terence.dao.impl.UserDaoImpl">
    <property name="strList">
        <list>
            <value>aaa</value>
            <value>bbb</value>
            <value>ccc</value>
        </list>
     </property>
</bean>

3.7.3 集合数据类型(List<User>)的注入

public class UserDaoImpl implements UserDao {
    List<User> userList;

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
}
<bean id="u1" class="com.terence.domain.User"/>
<bean id="u2" class="com.terence.domain.User"/>
<bean id="userDao" class="com.terence.dao.impl.UserDaoImpl">
    <property name="userList">
        <list>
            <bean class="com.terence.domain.User"/>
            <bean class="com.terence.domain.User"/>
            <ref bean="u1"/>
            <ref bean="u2"/>
        </list>
    </property>
</bean>

3.7.4 集合数据类型( Map<String,User> )的注入

public class UserDaoImpl implements UserDao {
    Map<String, User> userMap;

    public Map<String, User> getUserMap() {
        return userMap;
    }

    public void setUserMap(Map<String, User> userMap) {
        this.userMap = userMap;
    }
}
<bean id="u1" class="com.terence.domain.User">
    <property name="name" value="tom"/>
    <property name="addr" value="beijing"/>
</bean>
<bean id="u2" class="com.terence.domain.User">
    <property name="name" value="lucy"/>
    <property name="addr" value="tianjin"/>
</bean>
<bean id="userDao" class="com.terence.dao.impl.UserDaoImpl">
    <property name="userMap">
        <map>
            <entry key="user1" value-ref="u1"></entry>
            <entry key="user2" value-ref="u2"></entry>
        </map>
    </property>
</bean>

3.7.5 集合数据类型(Properties)的注入

public class UserDaoImpl implements UserDao {
    Properties properties;

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
<bean id="userDao" class="com.terence.dao.impl.UserDaoImpl">
    <property name="properties">
        <props>
            <prop key="p1">aaa</prop>
            <prop key="p2">bbb</prop>
            <prop key="p3">ccc</prop>
        </props>
    </property>
</bean>


3.8 引入其他配置文件(分模块开发)

实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置拆解到其他配置文件中,而在Spring主配置文件通过import标签进行加载

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

4. spring相关API

4.1 ApplicationContext的继承体系

applicationContext:接口类型,代表应用上下文,可以通过其实例获得 Spring 容器中的 Bean 对象

4.2 ApplicationContext的实现类

4.2.1 ClassPathXmlApplicationContext

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

它是从类的根路径下加载配置文件 推荐使用这种

4.2.2 FileSystemXmlApplicationContext

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:\\140-Git\\spring\\spring_ioc\\src\\main\\resources\\applicationContext.xml");

它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。

4.2.3 AnnotationConfigApplicationContext

当使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。
 

4.3 getBean()方法使用

public Object getBean(String name) throws BeansException {  
	assertBeanFactoryActive();   
	return getBeanFactory().getBean(name);
}
public <T> T getBean(Class<T> requiredType) throws BeansException {   			    	assertBeanFactoryActive();
	return getBeanFactory().getBean(requiredType);
}

其中,当参数的数据类型是字符串时,表示根据Bean的id从容器中获得Bean实例,返回是Object,需要强转。

当参数的数据类型是Class类型时,表示根据类型从容器中匹配Bean实例,当容器中相同类型的Bean有多个时,则此方法会报错

getBean()方法使用

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService1 = (UserService) applicationContext.getBean("userService");
UserService userService2 = applicationContext.getBean(UserService.class);


5.Spring配置数据源

5.1 数据源(连接池)的作用

数据源(连接池)是提高程序性能如出现的

事先实例化数据源,初始化部分连接资源

使用连接资源时从数据源中获取

使用完毕后将连接资源归还给数据源

常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等

开发步骤

①导入数据源的坐标和数据库驱动坐标

②创建数据源对象

③设置数据源的基本连接数据

④使用数据源获取连接资源和归还连接资源

5.2 数据源的手动创建

①导入mysql数据库驱动坐标、导入c3p0和druid的坐标

<dependencies>
    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>
    <!-- C3P0连接池 -->
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <!-- Druid连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.10</version>
    </dependency>
    <!-- junit单元测试 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

②创建C3P0连接池

@Test
public void testC3P0() throws Exception {
    //创建数据源
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    //设置数据库连接参数
    dataSource.setDriverClass("com.mysql.jdbc.Driver");
    dataSource.setJdbcUrl("jdbc:mysql://192.168.1.55:3306/mysql");
    dataSource.setUser("root");
    dataSource.setPassword("646453");
    //获得连接对象
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    connection.close();
}
 

②创建Druid连接池

@Test
public void testDruid() throws Exception {
    //创建数据源
    DruidDataSource dataSource = new DruidDataSource();
    //设置数据库连接参数
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://192.168.1.55:3306/mysql");
    dataSource.setUsername("root");
    dataSource.setPassword("646453");
    //获得连接对象
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
}

③提取jdbc.properties配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.55:3306/mysql
jdbc.username=root
jdbc.password=646453

④读取jdbc.properties配置文件创建连接池

@Test
public void testC3P0ByProperties() throws Exception {
    //加载类路径下的jdbc.properties
    ResourceBundle rb = ResourceBundle.getBundle("jdbc");
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    dataSource.setDriverClass(rb.getString("jdbc.driver"));
    dataSource.setJdbcUrl(rb.getString("jdbc.url"));
    dataSource.setUser(rb.getString("jdbc.username"));
    dataSource.setPassword(rb.getString("jdbc.password"));
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
}

5.3 Spring配置数据源

可以将DataSource的创建权交由Spring容器去完成

DataSource有无参构造方法,而Spring默认就是通过无参构造方法实例化对象的

DataSource要想使用需要通过set方法设置数据库连接信息,而Spring可以通过set方法进行字符串注入

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://192.168.1.55:3306/mysql"/>
    <property name="user" value="root"/>
    <property name="password" value="646453"/>
</bean>

测试从容器当中获取数据源

@Test
public void testSpringDataSource() throws Exception{
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    DataSource dataSource = applicationContext.getBean(DataSource.class);
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    connection.close();
}

5.4 抽取jdbc配置文件

applicationContext.xml加载jdbc.properties配置文件获得连接信息。

首先,需要引入context命名空间和约束路径:

命名空间:

xmlns:xsi="http://www.w3/2001/XMLSchema-instance"

约束路径:

http://www.springframework/schema/context http://www.springframework/schema/context/spring-context.xsd
<context:property-placeholder location="classpath:jdbc.properties"/>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

6. Spring注解开发

6.1 Spring原始注解

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

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的销毁方法

注意:

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

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

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

//@Component("userDao")
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("save running...");
    }
}

使用@Compont或@Service标识UserServiceImpl需要Spring进行实例化

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

//@Component("userService")
@Service("userService")
public class UserServiceImpl implements UserService {
    /*@Autowired
    @Qualifier("userDao")*/
    @Resource(name="userDao")
    private UserDao userDao;

    @Override
    public void save() {
        userDao.save();
    }
}

使用@Value进行字符串的注入

@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Value("注入普通数据")
    private String str;
    @Value("${jdbc.driver}")
    private String driver;
    @Override
    public void save() {
        System.out.println(str);
        System.out.println(driver);
        System.out.println("save running... ...");
    }
}

使用@Scope标注Bean的范围

//@Scope("prototype")
@Scope("singleton")
public class UserDaoImpl implements UserDao {
   //此处省略代码
}

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

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

6.2 Spring新注解

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

非自定义的Bean的配置:<bean>

加载properties文件的配置:context:property-placeholder

组件扫描的配置:context:component-scan

引入其他文件:<import>

注解说明
@Configuration用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中的 <context:component-scan base-package="com.itheima"/>一样
@Bean使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource用于加载.properties 文件中的配置
@Import用于导入其他配置类
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {

    @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")  //Spring会将当前方法的返回值以指定名称存储到Spring容器中
    public DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }

}
//标志该类是Spring的核心配置类
@Configuration
@ComponentScan("com.terence")
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {

}

测试加载核心配置类创建Spring容器

@Test
public void testAnnoConfiguration() throws Exception {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(com.itheima.cofig.SpringConfiguration.class);
    UserService userService = (UserService)applicationContext.getBean("userService");
    userService.save();
    DataSource dataSource = (DataSource)applicationContext.getBean("dataSource");
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
}

7. Spring整合Junit

7.1 原始Junit测试Spring的问题

在测试类中,每个测试方法都有以下两行代码:

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = applicationContext.getBean(DataSource.class);

这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常。所以又不能轻易删掉。

7.2 上述问题解决思路

让SpringJunit负责创建Spring容器,但是需要将配置文件的名称告诉它

将需要进行测试Bean直接在测试类中进行注入

7.3 Spring集成Junit步骤

①导入spring集成Junit的坐标

②使用@Runwith注解替换原来的运行期

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

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

⑤创建测试方法进行测试

7.4 Spring集成Junit代码实现

①导入spring集成Junit的坐标

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

②使用@Runwith注解替换原来的运行期

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

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

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

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

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

⑤创建测试方法进行测试

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

    @Test
    public void testUserService(){
        userService.save();
    }
}

8.Spring 的 AOP 简介

8.1 什么是 AOP

AOP 为 Aspect Oriented Programming 的缩写,意思为面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

AOP 是 OOP 的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

8.2 AOP 的作用及其优势

作用:在程序运行期间,在不修改源码的情况下对方法进行功能增强

优势:减少重复代码,提高开发效率,并且便于维护

8.3 AOP 的底层实现

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

8.4 AOP 的动态代理技术

常用的动态代理技术

JDK 代理 : 基于接口的动态代理技术

cglib 代理:基于父类的动态代理技术

8.5 JDK 的动态代理

①目标类接口

public interface TargetInterface {
    public void save();
}

②目标类

public class Target implements TargetInterface{
    public void save() {
        System.out.println("save running...");
    }
}

③增强对象代码

public class Advice {
    public void before(){
        System.out.println("前置通知");
    }
    public void afterReturning(){
        System.out.println("后置增强");
    }
}

④动态代理代码

// 测试,当调用接口的任何方法时,代理对象的代码都无需修改
public class ProxyTest {
    public static void main(String[] args) {
        final Target target = new Target(); //创建目标对象
        final Advice advice = new Advice(); //创建增强对象
        //创建代理对象
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {
                        advice.before();
                        Object invoke = method.invoke(target, args);
                        advice.afterReturning();
                        return invoke;
                    }
                }
        );
        proxy.save();//执行测试方法
    }
}

8.6 cglib 的动态代理

①目标类

public class Target{
    public void save() {
        System.out.println("save running...");
    }
}

 ②增强对象代码

public class Advice {
    public void before(){
        System.out.println("前置通知");
    }
    public void afterReturning(){
        System.out.println("后置增强");
    }
}

③动态代理代码

//测试,当调用接口的任何方法时,代理对象的代码都无序修改
public class ProxyTest {
    public static void main(String[] args) {
        final Target target = new Target(); //创建目标对象
        final Advice advice = new Advice(); //创建增强对象
        Enhancer enhancer = new Enhancer();  //创建增强器
        enhancer.setSuperclass(Target.class); //设置父类
        enhancer.setCallback(new MethodInterceptor() { //设置回调
            @Override
            public Object intercept(Object o, Method method, Object[] objects,MethodProxy methodProxy) throws Throwable {
                advice.before();
                Object invoke = method.invoke(target, objects);
                advice.afterReturning();
                return invoke;
            }
        });
        Target proxy = (Target) enhancer.create(); //创建代理对象
        proxy.save();//执行测试方法
    }
}
 

8.7 AOP 相关概念

Spring 的 AOP 实现底层就是对上面的动态代理的代码进行了封装,封装后我们只需要对需要关注的部分进行代码编写,并通过配置的方式完成指定目标的方法增强。

在正式讲解 AOP 的操作之前,我们必须理解 AOP 的相关术语,常用的术语如下:

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

8.8 AOP 开发明确的事项

①需要编写的内容

  • 编写核心业务代码(目标类的目标方法)

  • 编写切面类,切面类中有通知(增强功能方法)

  • 在配置文件中,配置织入关系,即将哪些通知与哪些连接点进行结合

②AOP 技术实现的内容

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

③AOP 底层使用哪种代理方式

在 spring 中,框架会根据目标类是否实现了接口来决定采用哪种动态代理的方式。

8.9 知识要点

  • aop:面向切面编程

  • aop底层实现:基于JDK的动态代理 和 基于Cglib的动态代理

  • aop的重点概念:

    Pointcut(切入点):被增强的方法
    ​
    Advice(通知/ 增强):封装增强业务逻辑的方法
    ​
    Aspect(切面):切点+通知
    ​
    Weaving(织入):将切点与通知结合的过程
  • 开发明确事项:

    谁是切点(切点表达式配置)
    
    谁是通知(切面类中的增强方法)
    
    将切点和通知进行织入配置

8.10 基于 XML 的 AOP 开发

8.10.1 快速入门

①导入 AOP 相关坐标

<!--导入spring的context坐标,context依赖aop-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.5.RELEASE</version>
</dependency>
<!-- aspectj的织入 -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.13</version>
</dependency>

②创建目标接口和目标类(内部有切点)

public interface TargetInterface {
    public void save();
}
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running...");
    }
}

③创建切面类(内部有增强方法)

public class MyAspect{
    //前置增强方法
    public void before(){
        System.out.println("前置增强...");
    }
}

④将目标类和切面类的对象创建权交给 spring

<!--配置目标类-->
<bean id="target" class="com.terence.aop.Target"></bean>
<!--配置切面类-->
<bean id="myAspect" class="com.terence.aop.MyAspect"></bean>

⑤导入aop命名空间

xmlns:aop="http://www.springframework/schema/aop"

http://www.springframework/schema/aop http://www.springframework/schema/aop/spring-aop.xsd
 

⑥在 applicationContext.xml 中配置织入关系

配置切点表达式和前置增强的织入关系

<aop:config>
    <!--引用myAspect的Bean为切面对象-->
    <aop:aspect ref="myAspect">
        <!--配置Target的method方法执行时要进行myAspect的before方法前置增强-->
        <aop:before method="before" pointcut="execution(public void com.terence.aop.Target.save())"></aop:before>
    </aop:aspect>
</aop:config>

⑦测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
    @Autowired
    private TargetInterface target;
    @Test
    public void test1(){
        target.save();
    }
}

⑧测试结果

8.10.2 XML 配置 AOP 详解

①切点表达式的写法

表达式语法:

execution([修饰符] 返回值类型 包名.类名.方法名(参数))
  • 访问修饰符可以省略

  • 返回值类型、包名、类名、方法名可以使用星号* 代表任意

  • 包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类

  • 参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表

例如:

execution(public void com.itheima.aop.Target.method())	
execution(void com.itheima.aop.Target.*(..))
execution(* com.itheima.aop.*.*(..))
execution(* com.itheima.aop..*.*(..))
execution(* *..*.*(..))

②通知的类型

通知的配置语法:

<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式"></aop:通知类型>

public class MyAspect {

    public void before(){
        System.out.println("前置增强..........");
    }

    public void afterReturning(){
        System.out.println("后置增强..........");
    }

    //Proceeding JoinPoint:  正在执行的连接点===切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强....");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强....");
        return proceed;
    }

    public void afterThrowing(){
        System.out.println("异常抛出增强..........");
    }

    public void after(){
        System.out.println("最终增强..........");
    }

}
<aop:config>
    <!-- 声明切面 -->
    <aop:aspect ref="myAspect">
        <!-- 切面:切点+通知 -->
        <aop:before method="before" pointcut="execution(* com.terence.aop.*.*(..))"/>
        <aop:after-returning method="afterReturning" pointcut="execution(* com.terence.aop.*.*(..))"/>
        <aop:around method="around" pointcut="execution(* com.terence.aop.*.*(..))"/>
        <aop:after-throwing method="afterThrowing" pointcut="execution(* com.terence.aop.*.*(..))"/>
        <aop:after method="after" pointcut="execution(* com.terence.aop.*.*(..))"/>
    </aop:aspect>
</aop:config>

③切点表达式的抽取

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

<aop:config>
    <!-- 声明切面 -->
    <aop:aspect ref="myAspect">
        <!-- 抽取切点表达式 -->
        <aop:pointcut id="myPointcut" expression="execution(* com.terence.aop.*.*(..))"/>
        <!-- 切面:切点+通知 -->
       <aop:around method="around" pointcut-ref="myPointcut"/>
       <aop:after method="after" pointcut-ref="myPointcut"/>
    </aop:aspect>
</aop:config>

8.11 基于注解的 AOP 开发

8.11.1 快速入门

基于注解的aop开发步骤:

①创建目标接口和目标类(内部有切点)

public interface TargetInterface {
    public void save();
}
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running...");
    }
}

②创建切面类(内部有增强方法)

public class MyAspect {
    public void before(){
        System.out.println("前置增强..........");
    }
}

③将目标类和切面类的对象创建权交给 spring

@Component("target")
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running...");
    }
}
@Component("myAspect")
public class MyAspect {
    public void before(){
        System.out.println("前置增强..........");
    }

④在切面类中使用注解配置织入关系

@Component("myAspect")
@Aspect
public class MyAspect {
    @Before("execution(* com.terence.anno.*.*(..))")
    public void before(){
        System.out.println("前置增强..........");
    }

⑤在配置文件中开启组件扫描和 AOP 的自动代理

<!--组件扫描-->
<context:component-scan base-package="com.terence.anno"/>

<!--aop自动代理-->
<aop:aspectj-autoproxy/>

⑥测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AopTest {
    @Autowired
    private TargetInterface target;
    @Test
    public void test1(){
        target.save();
    }
}

⑦测试结果

8.11.2 注解配置 AOP 详解

①注解通知的类型

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

@Component("myAspect")
@Aspect //标注当前MyAspect是一个切面类
public class MyAspect {
    //配置前置通知
    //@Before("execution(* com.terence.anno.*.*(..))")
    public void before(){
        System.out.println("前置增强..........");
    }
    //配置后置通知
    //@AfterReturning("execution(* com.terence.anno.*.*(..))")
    public void afterReturning(){
        System.out.println("后置增强..........");
    }
    //配置环绕通知
    @Around("execution(* com.terence.anno.*.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前增强....");
        Object proceed = pjp.proceed();//切点方法
        System.out.println("环绕后增强....");
        return proceed;
    }
    //配置异常通知
    @AfterThrowing("execution(* com.terence.anno.*.*(..))")
    public void afterThrowing(){
        System.out.println("异常抛出增强..........");
    }
    //配置最终通知
    @After("execution(* com.terence.anno.*.*(..))")
    public void after(){
        System.out.println("最终增强..........");
    }
}

②切点表达式的抽取

同 xml配置 aop 一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。具体如下:

@Component("myAspect")
@Aspect
public class MyAspect {
    //定义切点表达式
    @Pointcut("execution(* com.terence.anno.*.*(..))")
    public void myPoint(){}

    @Before("MyAspect.myPoint()")
    public void before(){
        System.out.println("前置增强..........");
    }
}

9. JdbcTemplate基本使用

9.1 JdbcTemplate基本使用-概述(了解)

JdbcTemplate是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。

9.2 JdbcTemplate基本使用-开发步骤(理解)

①导入spring-jdbc和spring-tx坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.0.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.0.5.RELEASE</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
</dependency>
<!-- C3P0连接池 -->
<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>

②创建数据库表和实体

CREATE TABLE `account` (
  `name` varchar(50) NOT NULL COMMENT '名称',
  `money` double DEFAULT NULL COMMENT '存款',
  PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

package com.terence.domain;

public class Account {
    private String name;
    private String money;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMoney() {
        return money;
    }

    public void setMoney(String money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + '\'' +
                ", money='" + money + '\'' +
                '}';
    }
}

③创建JdbcTemplate对象

④执行数据库操作

public class JdbcTemplateTest {
    @Test
    //测试JdbcTemplate开发步骤
    public void test1() throws PropertyVetoException {
        //创建数据源对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://192.168.1.55:3306/terence_db");
        dataSource.setUser("root");
        dataSource.setPassword("646453");

        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //设置数据源对象  知道数据库在哪
        jdbcTemplate.setDataSource(dataSource);
        //执行操作
        int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
        System.out.println(row);
    }
}

9.3 JdbcTemplate基本使用-spring产生模板对象

我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将数据源DataSource注入到JdbcTemplate模版对象中,然后通过Spring容器获得JdbcTemplate对象来执行操作。

抽取jdbc.properties:

将数据库的连接信息抽取到外部配置文件中,和spring的配置文件分离开,有利于后期维护

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.55:3306/terence_db
jdbc.username=root
jdbc.password=646453

配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework/schema/beans"
       xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework/schema/context"
       xsi:schemaLocation="http://www.springframework/schema/beans http://www.springframework/schema/beans/spring-beans.xsd
        http://www.springframework/schema/context http://www.springframework/schema/context/spring-context.xsd">
    <!--加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

测试代码

@Test
//测试Spring产生jdbcTemplate对象
public void test2() throws PropertyVetoException {
    ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
    int row = jdbcTemplate.update("insert into account values(?,?)", "lisi", 5000);
    System.out.println(row);
}

9.7 JdbcTemplate基本使用-常用操作-更新操作(应用)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testUpdate(){
        jdbcTemplate.update("update account set money=? where name=?",10000,"tom");
    }

    @Test
    public void testDelete(){
        jdbcTemplate.update("delete from account where name=?","tom");
    }

}

9.8 JdbcTemplate基本使用-常用操作-查询操作(应用)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testQueryCount(){
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }

    @Test
    public void testQueryOne(){
        Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
        System.out.println(account);
    }

    @Test
    public void testQueryAll(){
        List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }

}

10. 编程式事务控制相关对象

10.1 PlatformTransactionManager

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

注意:

PlatformTransactionManager 是接口类型,不同的 Dao 层技术则有不同的实现类,

例如:

Dao 层技术是jdbc 或 mybatis 时:

org.springframework.jdbc.datasource.DataSourceTransactionManager

Dao 层技术是hibernate时:

org.springframework.orm.hibernate5.HibernateTransactionManager

10.2 TransactionDefinition

TransactionDefinition 是事务的定义信息对象,里面有如下方法:

10.2.1. 事务隔离级别

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

  • ISOLATION_DEFAULT 数据库的默认隔离级别

  • ISOLATION_READ_UNCOMMITTED 未提交读取,都不能解决

  • ISOLATION_READ_COMMITTED 已提交读取,可以解决脏读

  • ISOLATION_REPEATABLE_READ 可重复读,可以解决不可重复读

  • ISOLATION_SERIALIZABLE 序列化,全部可以解决,但是效率低相当于缩表

10.2.2. 事务传播行为

  • REQUIRED:如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。一般的选择(默认值)

  • SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行(没有事务)

  • MANDATORY:使用当前的事务,如果当前没有事务,就抛出异常

  • REQUERS_NEW:新建事务,如果当前在事务中,把当前事务挂起。

  • NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起

  • NEVER:以非事务方式运行,如果当前存在事务,抛出异常

  • NESTED:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行 REQUIRED 类似的操作

  • 超时时间:默认值是-1,没有超时限制。如果有,以秒为单位进行设置

  • 是否只读:建议查询时设置为只读

10.3 TransactionStatus

TransactionStatus 接口提供的是事务具体的运行状态,方法介绍如下。

11 基于 XML 的声明式事务控制

11.1 什么是声明式事务控制

Spring 的声明式事务顾名思义就是采用声明的方式来处理事务。这里所说的声明,就是指在配置文件中声明,用在 Spring 配置文件中声明式的处理事务来代替代码式的处理事务。

声明式事务处理的作用

  • 事务管理不侵入开发的组件。具体来说,业务逻辑对象就不会意识到正在事务管理之中,事实上也应该如此,因为事务管理是属于系统层面的服务,而不是业务逻辑的一部分,如果想要改变事务管理策划的话,也只需要在定义文件中重新配置即可

  • 在不需要事务管理的时候,只要在设定文件上修改一下,即可移去事务管理服务,无需改变代码重新编译,这样维护起来极其方便

注意:Spring 声明式事务控制底层就是AOP。

11.2 声明式事务控制的实现

声明式事务控制明确事项:

  • 谁是切点?        //被增强的方法

  • 谁是通知?        //事务

  • 配置切面?        //将切点和通知进行AOP的配置

①引入tx命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework/schema/beans"
       xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework/schema/aop"
       xmlns:tx="http://www.springframework/schema/tx"
       xsi:schemaLocation="
       http://www.springframework/schema/beans http://www.springframework/schema/beans/spring-beans.xsd
       http://www.springframework/schema/aop http://www.springframework/schema/aop/spring-aop.xsd
       http://www.springframework/schema/tx http://www.springframework/schema/tx/spring-tx.xsd">
</beans>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>

②配置事务增强

<!--平台事务管理器-->
<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>

③配置事务 AOP 织入

<!--事务的aop增强-->
<aop:config>
    <!--<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.terence.service.impl.*.*(..))"/>-->
    <aop:pointcut id="myPointcut" expression="execution(* com.terence.service.impl.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"></aop:advisor>
</aop:config>

④测试事务控制转账业务代码

@Override
public void transfer(String outMan, String inMan, double money) {
    accountDao.out(outMan,money);
    int i = 1/0;
    accountDao.in(inMan,money);
}
public class AccountController {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = app.getBean(AccountService.class);
        accountService.transfer("tom","lucy",500);
    }
}

11.3 切点方法的事务参数的配置

<!--事务增强配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--设置事务的属性信息-->
    <tx:attributes>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

其中,<tx:method> 代表切点方法的事务参数的配置,例如:

<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="*"/>
  • name:切点方法名称

  • isolation:事务的隔离级别

  • propogation:事务的传播行为

  • timeout:超时时间

  • read-only:是否只读

11.4 知识要点

声明式事务控制的配置要点

  • 平台事务管理器配置

  • 事务通知的配置

  • 事务aop织入的配置

12 基于注解的声明式事务控制

12.1 使用注解配置声明式事务控制

  1. 编写 AccoutDao

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void out(String outMan, double money) {
        jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
    }

    public void in(String inMan, double money) {
        jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
    }
}
  1. 编写 AccoutService

@Service("accountService")
@Transactional(isolation = Isolation.REPEATABLE_READ)
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan,money);
        int i = 1/0;
        accountDao.in(inMan,money);
    }

    //@Transactional(isolation = Isolation.DEFAULT)
    public void xxx(){}
}
  1. 编写 applicationContext.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework/schema/beans"
       xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework/schema/aop"
       xmlns:context="http://www.springframework/schema/context"
       xmlns:tx="http://www.springframework/schema/tx"
       xsi:schemaLocation="
       http://www.springframework/schema/beans http://www.springframework/schema/beans/spring-beans.xsd
       http://www.springframework/schema/aop http://www.springframework/schema/aop/spring-aop.xsd
       http://www.springframework/schema/tx http://www.springframework/schema/tx/spring-tx.xsd
       http://www.springframework/schema/context http://www.springframework/schema/context/spring-context.xsd">
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--组件扫描-->
    <context:component-scan base-package="com.terence"/>
    <!--事物的注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

12.2 注解配置声明式事务控制解析

①使用 @Transactional 在需要进行事务控制的类或是方法上修饰,注解可用的属性同 xml 配置方式,例如隔离级别、传播行为等。

②注解使用在类上,那么该类下的所有方法都使用同一套注解参数配置。

③使用在方法上,不同的方法可以采用不同的事务参数配置。

④Xml配置文件中要开启事务的注解驱动<tx:annotation-driven />

12.3 知识要点

注解声明式事务控制的配置要点

  • 平台事务管理器配置(xml方式)

  • 事务通知的配置(@Transactional注解配置)

  • 事务注解驱动的配置 <tx:annotation-driven />

更多推荐

学习Spring框架这一篇就够了