哈喽铁子们今天分享Spring之aop 先看目录
目录
1,AOP的概念(即面向切面编程) 1.1 目标(Target)
被通知(被代理)的对象
在某个特定的连接点上执行的动作,同时Advice也是程序代码的具体实现,例如一个实现日志记录的代码(通知有些书上也称为处理)
将通知应用到目标对象后创建的对象(代理=目标+通知),只有代理对象才有AOP功能,而AOP的代码是写在通知的方法里面的
多个连接点的集合,定义了通知应该应用到那些连接点。 (也将Pointcut理解成一个条件 ,此条件决定了容器在什么情况下将通知和目标组合成代理返回给外部程序)
适配器=通知(Advice)+切入点(Pointcut)
程序执行过程中明确的点,如方法的调用,或者异常的抛出.
BookBiz.java:
- package com.tsq.aop.biz;
-
- public interface BookBiz {
- // 购书
- public boolean buy(String userName, String bookName, Double price);
-
- // 发表书评
- public void comment(String userName, String comments);
- }
BookBizImpl.java:
- package com.tsq.aop.biz.impl;
-
- import com.tsq.aop.biz.BookBiz;
- import com.tsq.aop.exception.PriceException;
-
- public class BookBizImpl implements BookBiz {
-
- public BookBizImpl() {
- super();
- }
-
- public boolean buy(String userName, String bookName, Double price) {
- // 通过控制台的输出方式模拟购书
- if (null == price || price <= 0) {
- throw new PriceException("book price exception");
- }
- //logDao.add->sout("买书相关日志")
- System.out.println(userName + " buy " + bookName + ", spend " + price);
- return true;
- }
-
- public void comment(String userName, String comments) {
- // 通过控制台的输出方式模拟发表书评
- System.out.println(userName + " say:" + comments);
- //System.out.println("买书返利");
- }
-
- }
PriceException.java:
- package com.tsq.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);
- }
-
- }
(org.springframework.aop.MethodBeforeAdvice):在连接点之前执行的通知()
案例:在购书系统当中使用AOP方式实现日志系统
MyMethodBeforeAdvice.java
- package com.tsq.aop.advice;
-
- import java.lang.reflect.Method;
- import java.util.Arrays;
-
- import org.springframework.aop.MethodBeforeAdvice;
-
- public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
-
- @Override
- public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
-
- //目标对象的类名
- String clzname = arg2.getClass().getName();
- //当前调用的方法是
- String methodName = arg0.getName();
- //当前调用方法所传递参数
- String args = Arrays.toString(arg1);
-
- System.out.println("【系统日志】:"+clzname+"."+methodName+"被调用,传递的参数"+args);
-
- }
-
- }
spring-context.xml
-
"bookBiz" class="com.tsq.biz.impl.BookBizImpl"> -
-
"MyBefore" class="com.cxy.aop.advice.MyMethodBeforeAdvice">
测试类
- package com.tsq.aop.demo;
-
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import com.tsq.biz.BookBiz;
-
- public class Demo1 {
-
- public static void main(String[] args) {
-
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
-
- BookBiz bean = (BookBiz)context.getBean("bookBiz");
- bean.buy("aa", "tsq", 0.1d);
- bean.comment("aa", "cc");
- }
-
- }
(org.springframework.aop.AfterReturningAdvice):在连接点正常完成后执行的通知
案例:在线购书系统中,要求不修改BookBizImpl代码的情况下增加如下功能:对买书的用户进行返利:每买本书返利3元。(后置通知)
MyAfterReturningAdvice .java
- package com.tsq.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
- <bean name="MyAfter" class="com.tsq.aop.advice.MyAfterReturningAdvice">bean>
(org.aopalliance.intercept.MethodInterceptor):包围一个连接点的通知,最大特点是可以修改返回值,由于它在方法前后都加入了自己的逻辑代码,因此功能异常强大。
它通过MethodInvocation.proceed()来调用目标方法(甚至可以不调用,这样目标方法就不会执行)
案例:修改日志系统不光要输出参数,还要输出返回值(环绕通知)
MyMethodInterceptor.
- package com.tsq.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();
-
- String targetName = target.getClass().getName();
- String methodName = method.getName();
- String params = Arrays.toString(args);
- String msg = "【环绕通知】:正在调用->" + targetName + "." + methodName + ",携带的参数:" + params;
- System.out.println(msg);
- Object returnValue = invocation.proceed();
- String msg2 = "【环绕通知】:目标对象所调用的方法的返回值:" + returnValue;
- System.out.println(msg2);
-
- return returnValue;
- }
-
-
- }
org.springframework.aop.ThrowsAdvice:这个通知会在方法抛出异常退出时执行
案例: 书本价格为负数时抛出一个异常,通过异常通知取消此订单
- package com.tsq.aop.advice;
-
- import org.springframework.aop.ThrowsAdvice;
-
- import com.tsq.aop.exception.PriceException;
-
- public class MyThrowsAdvice implements ThrowsAdvice {
- public void afterThrowing(PriceException ex ) {
- System.out.println("价格输入错误,购买失败,请重新购买");
- }
- }
spring-context.xml
- <bean name="MyException" class="com.tsq.aop.advice.MyThrowsAdvice">bean>
org.springframework.aop.support.RegexpMethodPointcutAdvisor: 处理买书返利的bug
适配器=通知(Advice)+切入点(Pointcut)
案例:通过适配器解决发书评时也返利的问题
-
"org.springframework.aop.support.RegexpMethodPointcutAdvisor" - id="MyAfter2">
-
"advice" ref="MyAfter"> -
-
"patterns"> -
-
.*buy -
-
-
代理工厂
-
"org.springframework.aop.framework.ProxyFactoryBean" - id="proxyFactoryBean">
-
"target" ref="bookBiz"> -
-
"proxyInterfaces"> -
-
com.tsq.aop.biz.IBookBiz -
-
-
"interceptorNames"> -
-
MyBefore -
MyAfter -
MyFilterAdvice -
MyException -
-
-
好了铁子们今天Spring之aop分享结束了