Spring学习(六)
一、AOP概念的引入(JDK动态代理)
为什么需要JDK动态代理?
在传统的业务开发中,我们经常会遇到这样的场景:需要在多个业务方法中添加相同逻辑,比如事务管理、日志记录、权限校验等。
使用JDK动态代理技术,将这些横切关注点(事务、日志等)从业务逻辑中抽取出来。
1.创建maven的项目,引入开发的坐标
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <!-- 有单元测试的环境,Spring5版本,Junit4.12版本 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- Spring整合Junit测试的jar包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> <scope>test</scope> </dependency> </dependencies>2.QcbyUtils类,进行事务管理
package com.qcby.Utils; import com.alibaba.druid.pool.DruidDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; /** * 事务的工具类 */ public class QcbyUtils { private static DruidDataSource ds = null; // 使用ThreadLocal存储当前线程中的Connection对象 private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(); // 在静态代码块中创建数据库连接池 static { try { // 通过代码创建C3P0数据库连接池 ds = new DruidDataSource(); ds.setDriverClassName("com.mysql.cj.jdbc.Driver"); ds.setUrl("jdbc:mysql:///spring_db"); ds.setUsername("root"); ds.setPassword("Xuhaoyu666!"); } catch (Exception e) { throw new ExceptionInInitializerError(e); } } /** * @Method: getConnection * @Description: 从数据源中获取数据库连接 * @Anthor: * @return Connection * @throws SQLException */ public static Connection getConnection() throws SQLException { // 从当前线程中获取Connection Connection conn = threadLocal.get(); if (conn == null) { // 从数据源中获取数据库连接 conn = getDataSource().getConnection(); // 将conn绑定到当前线程 threadLocal.set(conn); } return conn; } /** * @Method: startTransaction * @Description: 开启事务 * @Anthor: * */ public static void startTransaction() { try { Connection conn = threadLocal.get(); if (conn == null) { conn = getConnection(); // 把 conn绑定到当前线程上 threadLocal.set(conn); } // 开启事务 conn.setAutoCommit(false); } catch (Exception e) { throw new RuntimeException(e); } } /** * @Method: rollback * @Description:回滚事务 * @Anthor: */ public static void rollback() { try { // 从当前线程中获取Connection Connection conn = threadLocal.get(); if (conn != null) { // 回滚事务 conn.rollback(); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Method: commit * @Description:提交事务 * @Anthor: */ public static void commit() { try { // 从当前线程中获取Connection Connection conn = threadLocal.get(); if (conn != null) { // 提交事务 conn.commit(); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Method: close * @Description:关闭数据库连接(注意,并不是真的关闭,而是把连接还给数据库连接池) * @Anthor: * */ public static void close() { try { // 从当前线程中获取Connection Connection conn = threadLocal.get(); if (conn != null) { conn.close(); // 解除当前线程上绑定conn threadLocal.remove(); } } catch (Exception e) { throw new RuntimeException(e); } } /** * @Method: getDataSource * @Description: 获取数据源 * @Anthor: * @return DataSource */ public static DataSource getDataSource() { // 从数据源中获取数据库连接 return ds; } }3.AccountService的接口和实现类
package com.qcby.service; import com.qcby.domain.Account; import java.sql.SQLException; public interface AccountService { public void save(Account account1, Account account2) throws SQLException; }package com.qcby.service; import com.qcby.dao.AccountDao; import com.qcby.domain.Account; import java.sql.SQLException; public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void save(Account account1, Account account2) throws SQLException{ // try{ // 保存1账号 accountDao.save(account1); // 模拟异常 // int a = 1 / 0; // 保存2账号 accountDao.save(account2); // } catch (ArithmeticException e) { // e.printStackTrace(); // throw new SQLException("事务执行失败,发生除零错误", e); // } } }4.AccountDao的接口和实现类
package com.qcby.dao; import com.qcby.domain.Account; public interface AccountDao { public void save(Account account); }package com.qcby.dao; import com.qcby.Utils.QcbyUtils; import com.qcby.domain.Account; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class AccountDaoImpl implements AccountDao { public void save(Account account) { try{ Connection conn = QcbyUtils.getConnection(); String sql = "insert into account values(null,?,?)"; PreparedStatement stmt = conn.prepareStatement((sql)); stmt.setString(1,account.getName()); stmt.setDouble(2,account.getMoney()); stmt.executeUpdate(); stmt.close(); } catch (SQLException e){ e.printStackTrace(); } } }5.生成代理对象
package com.qcby.JDKProxy; import com.qcby.Utils.QcbyUtils; import com.qcby.service.AccountService; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class JdkProxy { public static Object getProxy(final AccountService accountService){ Object proxy = Proxy.newProxyInstance(JdkProxy.class.getClassLoader(), accountService.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null; try{ QcbyUtils.startTransaction(); result = method.invoke(accountService,args); QcbyUtils.commit(); } catch (Exception e){ e.printStackTrace(); QcbyUtils.rollback(); } return result; } }); return proxy; } }6.测试方法
import com.qcby.JDKProxy.CglibProxy; import com.qcby.JDKProxy.JdkProxy; import com.qcby.dao.AccountDao; import com.qcby.dao.AccountDaoImpl; import com.qcby.domain.Account; import com.qcby.service.AccountService; import com.qcby.service.AccountServiceImpl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.sql.SQLException; /** * 达内教育--腾讯课程认证机构 * 史招阳 */ public class DemoTest { @Test public void run1() throws SQLException { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取service对象 AccountService accountService = (AccountService) ac.getBean("accountService"); Account account1 = new Account(null,"熊大",10000.00); Account account2 = new Account(null,"美羊羊",11000.00); // accountService.save(account1,account2); Object proxyobj = JdkProxy.getProxy(accountService); AccountService proxy = (AccountService) proxyobj; proxy.save(account1,account2); } }二、AOP相关的概念
1.AOP的概述
什么是AOP的技术?
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程
AOP是一种编程范式,隶属于软工范畴,指导开发者如何组织程序结构
AOP最早由AOP联盟的组织提出的,制定了一套规范.Spring将AOP思想引入到框架中,必须遵守AOP联盟的规范
通过预编译方式或者运行期动态代理实现程序功能的统一维护的一种技术
AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型
利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率
AOP:面向切面编程(思想,解决OOP遇到的一些问题)
AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监视、事务管理、安全检查、缓存)
为什么要学习AOP?
可以在不修改源代码的前提下,对程序进行增强!!
2. AOP的优势
运行期间,不修改源代码的情况下对已有的方法进行增强
AOP优势:
1. 减少重复代码
2. 提高开发效率
3. 维护方便
3. AOP的底层原理
JDK 的动态代理技术
1. 为接口创建代理类的字节码文件
2. 使用 ClassLoader 将字节码文件加载到 JVM
3. 创建代理类实例对象,执行对象的目标方法
CGLIB 代理技术
