栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring之AOP

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring之AOP

目录

一:AOP

1.AOP的介绍

2.AOP的关键性概念

二:使用aop解决以下问题

三:前置通知

四:环绕通知 

五:异常通知 

六:过滤通知 


一:AOP

1.AOP的介绍

介绍:面向切面编程

2.AOP的关键性概念

连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出.

目标(Target):被通知(被代理)的对象
注1:完成具体的业务逻辑

通知(Advice):在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现
注2:完成切面编程

代理(Proxy):将通知应用到目标对象后创建的对象(代理=目标+通知),
注3:只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的

切入点(Pointcut):多个连接点的集合,定义了通知应该应用到那些连接点。
                 (也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
    
适配器(Advisor):适配器=通知(Advice)+切入点(Pointcut)

过程:目标--->连接点--->通知--->代理

切入点(多个连接点就是切入点)

图解:

二:使用aop解决以下问题

 

问题:记录谁把数据进行了增删改的操作

第一步:导入相关资料

异常类(PriceException )

package com.lj.aop.exception;
 
public class PriceException extends RuntimeException {
 
	public PriceException() {
		super();
	}
 
	public PriceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
		super(message, cause, enableSuppression, writableStackTrace);
	}
 
	public PriceException(String message, Throwable cause) {
		super(message, cause);
	}
 
	public PriceException(String message) {
		super(message);
	}
 
	public PriceException(Throwable cause) {
		super(cause);
	}
	
}

Biz层(IBookBiz )

package com.lj.aop.biz;
 
public interface IBookBiz {
	// 购书
	public boolean buy(String userName, String bookName, Double price);
 
	// 发表书评
	public void comment(String userName, String comments);
}

 新建实现类(BookBizImpl )

package com.sg.aop.biz.impl;
 
import com.lj.aop.biz.IBookBiz;
import com.lj.aop.exception.PriceException;
 
public class BookBizImpl implements IBookBiz {
 
	public BookBizImpl() {
		super();
	}
 
	public boolean buy(String userName, String bookName, Double price) {
		// 通过控制台的输出方式模拟购书
		if (null == price || price <= 0) {
			throw new PriceException("book price exception");
		}
		System.out.println(userName + " buy " + bookName + ", spend " + price);
		return true;
	}
 
	public void comment(String userName, String comments) {
		// 通过控制台的输出方式模拟发表书评
		System.out.println(userName + " say:" + comments);
	}
 
}

 在spring-context.xml中做配置

 

写测试类

package com.lj.aop.test;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.lj.aop.biz.IBookBiz;
 
public class AopTest {
	public static void main(String[] args) {
//		获取到spring-Context.xml配置文件
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
//		根据bean的id得到实现类
//		IBookBiz bookBiz = (IBookBiz) applicationContext.getBean("bookBiz");
//		调用前置通知
		IBookBiz bookBiz = (IBookBiz) applicationContext.getBean("proxyFactoryBean");
//		买书
		bookBiz.buy("a", "《金瓶梅》", 33.3);
//		评论
		bookBiz.comment("a", "lllal");
	}
}

运行结果如下:

 

三:前置通知

 
package com.lj.aop.advice;
import java.util.Arrays;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {

	public void before(Method method, Object[] args, Object target) throws Throwable {
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		String params = Arrays.toString(args);
		String msg = "【系统日志】:正在调用->"+targetName+"."+methodName+",携带的参数:"+params;
		System.out.println(msg);
	}
}

配置spring-context.xml(注意是目标、前置通知、生成代理对象那一段,其他是上次博客的内容)

 

















打滴滴1
踩小溜1















		
		
		
		
			
				.*buy
			
		
	


		
		
		
		
			
				com.lj.aop.biz.IBookBiz
				
			
		
		
		
			
				myBefore
           
               
                 myFilterAdvice
                 myAfter2
                 myExceptionAdvice 
			
		
	

 

测试类;

package com.lj.aop.test;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.lj.aop.biz.IBookBiz;
 
public class AopTest {
	public static void main(String[] args) {
//		获取到spring-Context.xml配置文件
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
//		根据bean的id得到实现类
//		IBookBiz bookBiz = (IBookBiz) applicationContext.getBean("bookBiz");
//		调用前置通知
		IBookBiz bookBiz = (IBookBiz) applicationContext.getBean("proxyFactoryBean");
//		买书
		bookBiz.buy("晓哥", "《金瓶梅》", 33d);
//		评论
		bookBiz.comment("晓哥", "yyds");
	}
}

测试结果:

package com.lj.aop.advice;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.springframework.aop.AfterReturningAdvice;


public class MyAfterReturningAdvice implements AfterReturningAdvice {

	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		String params = Arrays.toString(args);
		String msg = "【返利通知:返利3元】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params + ";目标对象所调用的方法的返回值:"
				+ returnValue;
		System.out.println(msg);
	}

}

配置spring-context.xml

运行测试类,结果展示:

 

四:环绕通知 
package com.lj.aop.advice;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;


public class MyMethodInterceptor implements MethodInterceptor {

	public Object invoke(MethodInvocation invocation) throws Throwable {
		Object target = invocation.getThis();
		Method method = invocation.getMethod();
		Object[] args = invocation.getArguments();
		// a.jsp window.open(b.jsp)
		// b.jsp xxx->返回object->b.jsp window.close();window.getArguments;
		String targetName = target.getClass().getName();
		String methodName = method.getName();
		String params = Arrays.toString(args);
		String msg = "【环绕通知】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params;
		System.out.println(msg);
		//invocation可以执行被代理的目标对象的业务方法
		Object returnValue = invocation.proceed();
		String msg2 = "【环绕通知】:目标对象所调用的方法的返回值:" + returnValue;
		System.out.println(msg2);
		return returnValue;
	}

}

配置spring-context.xml



















打滴滴1
踩小溜1















		
		
		
		
			
				.*buy
			
		
	


		
		
		
		
			
				com.lj.aop.biz.IBookBiz
				
			
		
		
		
			
				myBefore
           
               
                 myFilterAdvice
                 myAfter2
                 myExceptionAdvice 
			
		
	

五:异常通知 
package com.lj.aop.advice;

import org.springframework.aop.ThrowsAdvice;

import com.lj.aop.exception.PriceException;




public class MyThrowsAdvice implements ThrowsAdvice {
	public void afterThrowing( PriceException ex ) {
		System.out.println("价格输入有误,购买失败,请重新输入!!!");
	}
}

配置spring-context.xml



















打滴滴1
踩小溜1















		
		
		
		
			
				.*buy
			
		
	


		
		
		
		
			
				com.lj.aop.biz.IBookBiz
				
			
		
		
		
			
				myBefore
           
               
                 myFilterAdvice
                 myAfter2
                 myExceptionAdvice 
			
		
	

 将测试类改一下(把价格改为负数)

package com.lj.aop.test;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.lj.aop.biz.IBookBiz;
 
public class AopTest {
	public static void main(String[] args) {
//		获取到spring-Context.xml配置文件
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
//		根据bean的id得到实现类
//		IBookBiz bookBiz = (IBookBiz) applicationContext.getBean("bookBiz");
//		调用前置通知
		IBookBiz bookBiz = (IBookBiz) applicationContext.getBean("proxyFactoryBean");
//		买书
		bookBiz.buy("晓哥", "《金瓶梅》", -33d);
//		评论
		bookBiz.comment("晓哥", "yyds");
	}
}

 结果展示:

六:过滤通知 

配置spring-context.xml

作用:过滤掉一些不需要使用的东西,从而进一步的优化 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/353409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号