Spring 学习笔记

182 阅读9分钟

概述

简介

分层(表现层、Service 层和 Dao 层)的全栈、轻量级开源框架

优势

  • IOC:方便解耦,简化开发
  • AOP:解决 OOP 无法解决的问题
  • 声明式事务:简化事务控制
  • 测试:用非容器依赖的编程方式进行测试
  • 集成:支持各种优秀框架
  • API:简化 Java EE API(JDBC、JavaMail 和远程调用等)
  • 源码:可以学习和借鉴其中的设计模式

核心思想

IOC

定义

控制:对象创建的权利

反转:控制权交给外部环境了

作用

解决了对象之间的耦合问题

和 DI 的区别

同一件事的不同角度描述

AOP

定义

解决OOP不能处理的问题,例如去除顶层父类的多个方法相同位置的重复代码

切:横切逻辑(多个顺序流程中出现的相同子流程)

面:横切逻辑代码影响多个方法,每个方法是一个点,点动成面

手写实现IOC和AOP

银行转账案例数据库表

name money cardNo
李大雷 10000 6029621011000
韩梅梅 10000 6029621011001

问题分析

  1. 对象之间耦合
  2. Service 层代码没有进行事务控制

解决思路

  1. 反射 + 工厂模式
  2. 将数据库连接和当前线程绑定 + Service 层进行事务的开始、提交和回滚

利用反射实现 IOC

POJO

package com.lagou.edu.pojo;

public class Account {

    private String cardNo;
    private String name;
    private int money;

    public String getName() {
        return name;
    }

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

    public int getMoney() {
        return money;
    }

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

    public String getCardNo() { return cardNo; }

    public void setCardNo(String cardNo) { this.cardNo = cardNo;}

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

配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!--根标签beans,里面配置一个又一个的bean子标签,每一个bean子标签都代表一个类的配置-->
<beans>
    <!--id标识对象,class是类的全限定类名-->
    <bean id="accountDao" class="com.lagou.edu.dao.JdbcAccountDaoImpl">
        <property name="ConnectionUtils" ref="connectionUtils"/>
    </bean>
    <bean id="transferService" class="com.lagou.edu.service.TransferServiceImpl">
        <!--set+ name 之后锁定到传值的set方法了,通过反射技术可以调用该方法传入对应的值-->
        <property name="AccountDao" ref="accountDao"></property>
    </bean>

    <!--与当前线程绑定的数据库连接-->
    <bean id="connectionUtils" class="com.lagou.edu.utils.ConnectionUtils"></bean>
</beans>

工厂类

package com.lagou.edu.factory;

public class BeanFactory {
    /**
     * 任务一:读取解析xml,通过反射技术实例化对象并且存储待用(map集合)
     * 任务二:对外提供获取实例对象的接口(根据id获取)
     */
    private static Map<String,Object> map = new HashMap<>();  // 存储对象

    static {
        // 任务一:读取解析xml,通过反射技术实例化对象并且存储待用(map集合)
        // 加载xml
        InputStream resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("beans2.xml");
        // 解析xml
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(resourceAsStream);
            Element rootElement = document.getRootElement();
            List<Element> beanList = rootElement.selectNodes("//bean");
            for (int i = 0; i < beanList.size(); i++) {
                Element element =  beanList.get(i);
                // 处理每个bean元素,获取到该元素的id 和 class 属性
                String id = element.attributeValue("id");        // accountDao
                String clazz = element.attributeValue("class");  // com.lagou.edu.dao.JdbcAccountDaoImpl
                // 通过反射技术实例化对象
                Class<?> aClass = Class.forName(clazz);
                Object o = aClass.newInstance();  // 实例化之后的对象

                // 存储到map中待用
                map.put(id,o);
            }

            // 实例化完成之后维护对象的依赖关系,检查哪些对象需要传值进入,根据它的配置,我们传入相应的值
            // 有property子元素的bean就有传值需求
            List<Element> propertyList = rootElement.selectNodes("//property");
            // 解析property,获取父元素
            for (int i = 0; i < propertyList.size(); i++) {
                Element element =  propertyList.get(i);   //<property name="AccountDao" ref="accountDao"></property>
                String name = element.attributeValue("name");
                String ref = element.attributeValue("ref");

                // 找到当前需要被处理依赖关系的bean <bean id="transferService" class="com.lagou.edu.service.TransferServiceImpl">
                Element parent = element.getParent();

                // 调用父元素对象的反射功能
                String parentId = parent.attributeValue("id");
                Object parentObject = map.get(parentId);
                // 遍历父对象中的所有方法,找到"set" + name
                Method[] methods = parentObject.getClass().getMethods();
                for (int j = 0; j < methods.length; j++) {
                    Method method = methods[j];
                    if(method.getName().equalsIgnoreCase("set" + name)) {  // 该方法就是 setAccountDao(AccountDao accountDao)
                        method.invoke(parentObject,map.get(ref));
                    }
                }
                // 把处理之后的parentObject重新放到map中
                map.put(parentId,parentObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 任务二:对外提供获取实例对象的接口(根据id获取)
    public static  Object getBean(String id) {
        return map.get(id);
    }
}

与当前线程绑定的数据库连接

package com.lagou.edu.utils;

/**
 * 数据库连接池
 */
public class DruidUtils {

    private DruidUtils(){
    }

    private static DruidDataSource druidDataSource = new DruidDataSource();

    static {
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3307/bank");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
    }

    public static DruidDataSource getInstance() {
        return druidDataSource;
    }
}
package com.lagou.edu.utils;

/**
 * 与当前线程绑定的数据库连接
 */
public class ConnectionUtils {

    private ThreadLocal<Connection> threadLocal = new ThreadLocal<>(); // 存储当前线程的连接

    /**
     * 从当前线程获取连接
     */
    public Connection getCurrentThreadConn() throws SQLException {
        /**
         * 判断当前线程中是否已经绑定连接,如果没有绑定,需要从连接池获取一个连接绑定到当前线程
          */
        Connection connection = threadLocal.get();
        if(connection == null) {
            // 从连接池拿连接并绑定到线程
            connection = DruidUtils.getInstance().getConnection();
            // 绑定到当前线程
            threadLocal.set(connection);
        }
        return connection;
    }
}

DAO 层

package com.lagou.edu.dao;

public interface AccountDao {

    Account queryAccountByCardNo(String cardNo) throws Exception;

    int updateAccountByCardNo(Account account) throws Exception;
}
package com.lagou.edu.dao;

public class JdbcAccountDaoImpl implements AccountDao {

    private ConnectionUtils connectionUtils;
    
    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }

    @Override
    public Account queryAccountByCardNo(String cardNo) throws Exception {
        // 从连接池获取连接
        Connection con = connectionUtils.getCurrentThreadConn();
        String sql = "select * from account where cardNo=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, cardNo);
        ResultSet resultSet = preparedStatement.executeQuery();

        Account account = new Account();
        while (resultSet.next()) {
            account.setCardNo(resultSet.getString("cardNo"));
            account.setName(resultSet.getString("name"));
            account.setMoney(resultSet.getInt("money"));
        }

        resultSet.close();
        preparedStatement.close();
        // 不能关闭连接
        return account;
    }

    @Override
    public int updateAccountByCardNo(Account account) throws Exception {

        // 从连接池获取连接
        // 改造为:从当前线程当中获取绑定的connection连接
        Connection con = connectionUtils.getCurrentThreadConn();
        String sql = "update account set money=? where cardNo=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, account.getMoney());
        preparedStatement.setString(2, account.getCardNo());
        int i = preparedStatement.executeUpdate();

        preparedStatement.close();
        // 不能关闭连接
        return i;
    }
}

Service 层

package com.lagou.edu.service;

public interface TransferService {

    void transfer(String fromCardNo, String toCardNo, int money) throws Exception;
}
package com.lagou.edu.service;

public class TransferServiceImpl implements TransferService {

    private AccountDao accountDao;

    // set方法传值
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        try{
            // 开启事务(关闭事务的自动提交)
            TransactionManager.getInstance().beginTransaction();*/

            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            accountDao.updateAccountByCardNo(to);
            int c = 1/0; // 触发回滚
            accountDao.updateAccountByCardNo(from);
            // 提交事务
            TransactionManager.getInstance().commit();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚事务
            TransactionManager.getInstance().rollback();
            // 抛出异常便于上层servlet捕获
            throw e;
        }
    }
}

测试

package com.lagou.edu.test;

public class Test {
    @Test
    public void transfer() {
        TransferService service = (TransferService) BeanFactory.getBean("transferService");
        service.transfer("6029621011000", "6029621011001", 100);
    }
}

使用动态代理实现 AOP

配置文件

<beans>
    ...
    <!--事务管理器-->
    <bean id="transactionManager" class="com.lagou.edu.utils.TransactionManager">
        <property name="ConnectionUtils" ref="connectionUtils"/>
    </bean>

    <!--代理对象工厂-->
    <bean id="proxyFactory" class="com.lagou.edu.factory.ProxyFactory">
        <property name="TransactionManager" ref="transactionManager"/>
    </bean>
</beans>

事务管理器

package com.lagou.edu.utils;

/**
 * 事务管理器类:负责手动事务的开启、提交、回滚
 */
public class TransactionManager {

    @Autowired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }
    
    // 开启手动事务控制
    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    // 提交事务
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }


    // 回滚事务
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }
}

代理对象工厂

package com.lagou.edu.factory;

/**
 * 代理对象工厂:负责生成代理对象
 */
public class ProxyFactory {

    private TransactionManager transactionManager;

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    /**
     * Jdk动态代理
     * @param obj  委托对象
     * @return   代理对象
     */
    public Object getJdkProxy(Object obj) {

        // 获取代理对象
        return  Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Object result = null;

                        try{
                            // 开启事务(关闭事务的自动提交)
                            transactionManager.beginTransaction();
                            // 方法调用
                            result = method.invoke(obj,args);
                            // 提交事务
                            transactionManager.commit();
                        } catch (Exception e) {
                            e.printStackTrace();
                            // 回滚事务
                            transactionManager.rollback();
                            // 抛出异常便于上层servlet捕获
                            throw e;
                        }
                        return result;
                    }
                });
    }

    /**
     * 使用cglib动态代理生成代理对象
     * @param obj 委托对象
     * @return
     */
    public Object getCglibProxy(Object obj) {
        return Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                Object result = null;
                try{
                    // 开启事务(关闭事务的自动提交)
                    transactionManager.beginTransaction();
                    // 方法调用
                    result = method.invoke(obj,objects);
                    // 提交事务
                    transactionManager.commit();
                }catch (Exception e) {
                    e.printStackTrace();
                    // 回滚事务
                    transactionManager.rollback();
                    // 抛出异常便于上层servlet捕获
                    throw e;
                }
                return result;
            }
        });
    }
}

去掉 Service 层 transfer 方法的事务控制代码

@Override
public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
    Account from = accountDao.queryAccountByCardNo(fromCardNo);
    Account to = accountDao.queryAccountByCardNo(toCardNo);

    from.setMoney(from.getMoney()-money);
    to.setMoney(to.getMoney()+money);

    accountDao.updateAccountByCardNo(to);
    int c = 1/0;
    accountDao.updateAccountByCardNo(from);
}

测试

package com.lagou.edu.test;

public class Test {

    @Test
    public void transfer() {
        // 首先从BeanFactory获取到proxyFactory代理工厂的实例化对象
        ProxyFactory proxyFactory = (ProxyFactory) BeanFactory.getBean("proxyFactory");
        // 获取被代理对象
        TransferService transferService = (TransferService) BeanFactory.getBean("transferService");
        // 根据被代理对象生成代理对象
        TransferService proxyService = (TransferService) proxyFactory.getJdkProxy(transferService);
        proxyService.transfer("6029621011000", "6029621011001", 100);
    }
}

IOC的应用

在理解的基础上把代码敲出来

基础应用

BeanFactory vs. ApplicationContext

Java SE 环境下启动 IOC 容器

ClassPathXmlApplicationContext:从类的根路径下加载配置文件(相对路径,推荐使用)

FileSystemXmlApplicationContext:从磁盘路径下加载配置文件(绝对路径,不推荐)

AnnotationConfigApplicationContext:纯注解模式下启动 Spring 容器

Java EE 环境下启动 IOC 容器

使用监听器 ContextLoaderListener 启动 IOC 容器

纯 xml

xml 文件头

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

实例化 bean 的三种方式

使用无参构造器创建

使用静态方法创建

使用实例化方法创建

bean 的生命周期

单例模式:singleton

生命周期与容器相同

多例模式:prototype

Spring 框架只负责创建,不负责销毁

bean 标签属性

id

class

name

factory-bean

factory-method

scope

init-method

destroy-method

分类

按照注入方式分类

构造器注入(强制属性注入):当没有无参构造时,必须提供构造器注入

set方法注入(可选属性注入,使用最多)

按照注入的数据类型分类

基本类型和 String

其他类型:对象类型

复杂类型:Array, List, Set, Map, Properties

xml + 注解

部分 bean 使用 xml 定义,另一部分使用注解定义。仍然从加载 xml 开始。

使用 xml 注入:第三方定义的定义的 bean, 如 Druid 数据库连接池

使用注解注入:自己开发定义的 bean

搞清楚注解的本质(标记),扫描

xml 与 注解的对应关系

xml 注解
标签 @Component, @Repository, @Service, @Controller
标签的 scope 属性 @Scope
标签的 init-method 属性 @PostConstruct
标签的 destroy-method 属性 @PreDestroy

依赖注入的注解实现

自动装配

@Autowired 通常采用按照类型注入

public class TransferServiceImpl {
    @Autowired
    private AccountDao accountDao;
}
手动装配

@Qualifier 告诉 Spring 具体去装配哪个对象

public class TransferServiceImpl {
    @Autowired
    @Qualifier(name="jdbcAccountDaoImpl")
    private AccountDao accountDao;
}

纯注解

改造 xml + 注解模式,将 xml 中遗留的内容全部以注解的形式迁移出去,最终删除 xml 文件,从 java 配置类启动

对应注解

@Configuration:表明当前类是一个配置类

@ComponentScan:替代 context:component-scan

@PropertySource:注入外部属性配置文件

@Value:对变量赋值

@Bean:将方法返回对象加入到 IOC 容器

高级特性

延迟加载

ApplicationContext 容器的默认行为是在启动服务器时将所有单例 bean 提前实例化(不延迟加载)

lazy-init

<bean id="testBean" class="com.lagou.LazyBean" lazy-init="true"/>

早期硬件资源受限制使用

可以将不常用的 bean 设置成延迟加载

FactoryBean vs. BeanFactory

工厂bean

public class CompanyFactoryBean implements FactoryBean<Company> {

    private String companyInfo;

    public void setCompanyInfo(String companyInfo) {
        this.companyInfo = companyInfo;
    }

    @Override
    public Company getObject() throws Exception {
        // 模拟复杂bean的创建
        Company company = new Company();
        String[] strings = companyInfo.split(",");
        company.setName(strings[0]);
        company.setAddress(strings[1]);
        company.setScale(Integer.parseInt(strings[2]));
        return company;
    }

    @Override
    public Class<?> getObjectType() {
        return Company.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
public class App {
    public static void main( String[] args ) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Object companyBean = context.getBean("companyBean");
        System.out.println(companyBean);

        Object factoryBean = context.getBean("&companyBean");
        System.out.println(factoryBean );
    }
}

后置处理器

从原始对象到代理对象

BeanPostProcessor

在 bean 对象实例化之后进行处理

BeanFactoryPostProcessor

在 BeanFactory 初始化之后进行处理

IOC 源码

源码不要都搞清楚,抓主线脉络

方法:反调(find usages)

循环依赖问题

AOP的应用

AOP 源码

工作经验:问题定位、分析的能力