11.1-springAOP名词解析--spring006

mac2025-12-17  5

Joinpoint(连接点): 所谓连接点是指那些被拦截到的点。在 spring 中,这些点指的是方法,因为 spring 只支持方法类型的 连接点。

业务层接口的方法 List findAllAccount(); Account findAccountById(Integer accountId);

Pointcut(切入点): 所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义。 切入点一定是连接点 被增强的业务接口的方法

此处test就不是切入点 但接口的其他方法就是切入点,因为test没有被增强

public IAccountService getAccountService() { return (IAccountService)Proxy.newProxyInstance(accountService.getClass().getClassLoader(), accountService.getClass().getInterfaces(), new InvocationHandler() { /** * 添加事务的支持 * * @param proxy * @param method * @param args * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("test".equals(method.getName())){ return method.invoke(accountService,args); } Object rtValue = null; try { //1.开启事务 txManager.beginTransaction(); //2.执行操作 rtValue = method.invoke(accountService, args); //3.提交事务 txManager.commit(); //4.返回结果 return rtValue; } catch (Exception e) { //5.回滚操作 txManager.rollback(); throw new RuntimeException(e); } finally { //6.释放连接 txManager.release(); } } });

Advice(通知/增强): 所谓通知是指拦截invoke()到 Joinpoint 之后所要做的事情就是通知。 通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。

即invoke后进行对事物的支持

private TransactionManager txManager; 即事物工具类 /** * 和事务管理相关的工具类,它包含了,开启事务,提交事务,回滚事务和释放连接 */ public class TransactionManager { private ConnectionUtils connectionUtils; public void setConnectionUtils(ConnectionUtils connectionUtils) { this.connectionUtils = connectionUtils; } //开启事物 public void beginTransaction(){ try { connectionUtils.getThreadConnection().setAutoCommit(false);//自动提交 /** * 提交事务 */ public void commit(){ try { connectionUtils.getThreadConnection().commit(); /** * 回滚事务 */ public void rollback(){ try { connectionUtils.getThreadConnection().rollback(); /** * 释放连接 */ public void release(){ try { connectionUtils.getThreadConnection().close();//还回连接池中 connectionUtils.removeConnection();

Introduction(引介): 引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方 法或 Field。

Target(目标对象): 代理的目标对象。被代理类,制造商。

Weaving(织入): 是指把增强应用到目标对象来创建新的代理对象的过程。 spring 采用动态代理织入,而 AspectJ 采用编译期织入和类装载期织入。

//加入对某一功能的支持,例如对事物支持

Proxy(代理): 一个类被 AOP 织入增强后,就产生一个结果代理类。

public IAccountService getAccountService() { return (IAccountService)Proxy.newProxyInstance(accountService.getClass().getClassLoader(), accountService.getClass().getInterfaces(), new InvocationHandler() { return (IAccountService)Proxy.newProxyInstance...就是代理对象

Aspect(切面): 是切入点和通知(引介)的结合。 切入点方法和通知方法在执行调用的对应关系以及配置

切入点:接口方法 通知:提供公共代码的类,例如事物类TransactionManager
最新回复(0)