Spring AOP: 多切面的顺序,性能及异常监控

news/2024/12/22 16:44:19/

目录标题

  • 一、Sprig的AOP操作
    • JDK动态代理
    • CGLib动态代理
    • 基于xml开发Spring AOP
    • 基于注解开发Spring AOP
  • 二、多切面的顺序
    • 基于注解的配置
    • 基于Ordered接口配置
    • 基于XML配置
  • 三、性能及异常监控
    • 性能监控
    • 异常监控
  • 四、工程目录及运行结果图

一、Sprig的AOP操作

JDK动态代理

//接口
package com.qfedu.demo01;
/*** @author 123*/ //LoginService接口
public interface LoginService {public void login();
}
//实现类
package com.qfedu.demo01;/*** @author 123*/
public class LoginServiceImpl implements LoginService{public void login() {System.out.println("执行login方法");}
}
package com.qfedu.demo01;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;public class PerformHandler implements InvocationHandler {
//    目标对象private Object target;public PerformHandler(Object target){this.target = target;}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        增强的方法System.out.println("方法开始执行");
//        执行被代理类的原方法Object invoke = method.invoke(target, args);
//        增强的方法System.out.println("方法执行完毕");return invoke;}
}
//测试类
package com.qfedu.demo01;import java.lang.reflect.Proxy;public class TestPerformHandler {public static void main(String[] args) {LoginService loginService = new LoginServiceImpl();PerformHandler performHandler = new PerformHandler(loginService);
//        创建代理对象loginService = (LoginService)Proxy.newProxyInstance(loginService.getClass().getClassLoader(),loginService.getClass().getInterfaces(), performHandler);loginService.login();}
}

CGLib动态代理

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>chapter08</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>aopalliance</groupId><artifactId>aopalliance</artifactId><version>1.0</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.7.4</version></dependency><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>5.0.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.0.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.0.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.0.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>5.0.8.RELEASE</version></dependency>
</dependencies></project>
package com.qfedu.demo01;import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.Method;public class CglibPoxy implements MethodInterceptor {private Enhancer  enhancer = new Enhancer();
//    生成代理对象的方法public Object getProxy(Class clazz){enhancer.setSuperclass(clazz);enhancer.setCallback(this);return enhancer.create();}public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("CGLig代理之前");Object invoke = methodProxy.invokeSuper(o, objects);System.out.println("CGLig代理之后");return null;}
}
package com.qfedu.demo01;public class TestCGLlib {public static void main(String[] args) {CglibPoxy cglibPoxy = new CglibPoxy();
//        创建代理对象LoginServiceImpl userService = (LoginServiceImpl) cglibPoxy.getProxy(LoginServiceImpl.class);userService.login();}
}

基于xml开发Spring AOP

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:contxt="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd ">
<!--    注册bean--><bean name="userService" class="com.qfedu.demo02.UserServiceImpl"></bean><bean name="xmlAdvice" class="com.qfedu.demo02.XmlAdvice"></bean>
<!--配置Spring AOP--><aop:config>
<!--        指定切点--><aop:pointcut id="pointcut" expression="execution( * com.qfedu.demo02.UserServiceImpl.*(..))"/>
<!--        制定切面--><aop:aspect ref="xmlAdvice"><aop:before method="before" pointcut-ref="pointcut"></aop:before><aop:after-returning method="afterReturning" pointcut-ref="pointcut"></aop:after-returning><aop:around method="around" pointcut-ref="pointcut"></aop:around><aop:after-throwing method="afterException" pointcut-ref="pointcut"></aop:after-throwing><aop:after method="after" pointcut-ref="pointcut"></aop:after></aop:aspect></aop:config>
</beans>
package com.qfedu.demo02;public interface UserService {void insert();void delete();void update();void select();
}
package com.qfedu.demo02;public class UserServiceImpl implements UserService{public void insert() {System.out.println("添加用户信息");}public void delete() {System.out.println("删除用户信息");}public void update() {System.out.println("更新用户信息");}public void select() {System.out.println("查询用户信息");}
}
package com.qfedu.demo02;import org.aspectj.lang.ProceedingJoinPoint;public class XmlAdvice {//    前置通知public void before(){System.out.println("此为前置通知");}//    后置通知public void afterReturning(){System.out.println("此为后置通知(无异常)");}//    环绕通知public Object around(ProceedingJoinPoint point) throws Throwable{System.out.println("此为环绕通知之前的部分");Object object = point.proceed();System.out.println("此为环绕通知之后的部分");return object;}//    异常通知public void afterException(){System.out.println("此为异常通知");}//    后置通知public void after(){System.out.println("此为后置通知");}
}
package com.qfedu.demo02;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestXml {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = context.getBean("userService", UserService.class);userService.delete();}
}

基于注解开发Spring AOP

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:contxt="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd ">
<!--    注册bean--><bean name="userService" class="com.qfedu.demo02.UserServiceImpl"></bean><bean name="xmlAdvice" class="com.qfedu.demo02.XmlAdvice"></bean>
<!--配置Spring AOP--><aop:config>
<!--        指定切点--><aop:pointcut id="pointcut" expression="execution( * com.qfedu.demo02.UserServiceImpl.*(..))"/>
<!--        制定切面--><aop:aspect ref="xmlAdvice"><aop:before method="before" pointcut-ref="pointcut"></aop:before><aop:after-returning method="afterReturning" pointcut-ref="pointcut"></aop:after-returning><aop:around method="around" pointcut-ref="pointcut"></aop:around><aop:after-throwing method="afterException" pointcut-ref="pointcut"></aop:after-throwing><aop:after method="after" pointcut-ref="pointcut"></aop:after></aop:aspect></aop:config><!--    注册bean-->
<!--    <bean name="userService" class="com.qfedu.demo02.UserServiceImpl"/>--><bean name="annoAdvice" class="com.qfedu.demo02.AnnoAdvice"/>
<!--    开启@aspectj的自动代理支持--><aop:aspectj-autoproxy/>
</beans>
package com.qfedu.demo02;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;public class AnnoAdvice {//切点@Pointcut("execution( * com.qfedu.demo02.UserServiceImpl. * (..))")public void pointcut(){}//    前置通知@Before("pointcut()")public void before(){System.out.println("此为前置通知");}//    后置通知@AfterReturning("pointcut()")public void afterReturning(){System.out.println("此为后置通知(无异常)");}//    环绕通知@Around("pointcut()")public Object around(ProceedingJoinPoint point) throws Throwable{System.out.println("此为环绕通知之前的部分");Object object = point.proceed();System.out.println("此为环绕通知之后的部分");return object;}//    异常通知@AfterThrowing("pointcut()")public void afterException(){System.out.println("此为异常通知");}//    后置通知@After("pointcut()")public void after(){System.out.println("此为后置通知");}}
package com.qfedu.demo02;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAnnotation {public static void main(String[] args) {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = applicationContext.getBean("userService", UserService.class);userService.insert();}
}

二、多切面的顺序

基于注解的配置

@Order(0):数字越小,优先级越高

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:contxt="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd ">
<!--    注册bean--><bean name="userService" class="com.qfedu.demo02.UserServiceImpl"/>
<!--    <bean name="xmlAdvice" class="com.qfedu.demo02.XmlAdvice"/>-->
<!--    <bean name="annoAdvice" class="com.qfedu.demo02.AnnoAdvice"/>--><bean name="aspect01" class="com.qfedu.demo02.Aspect01"/><bean name="aspect02" class="com.qfedu.demo02.Aspect02"/><!--    开启@aspectj的自动代理支持/扫描包设置--><aop:aspectj-autoproxy/>
<!--&lt;!&ndash;配置Spring AOP&ndash;&gt;-->
<!--    <aop:config>-->
<!--&lt;!&ndash;        指定切点&ndash;&gt;-->
<!--        <aop:pointcut id="pointcut" expression="execution( * com.qfedu.demo02.UserServiceImpl.*(..))"/>-->
<!--&lt;!&ndash;        制定切面&ndash;&gt;-->
<!--        <aop:aspect ref="xmlAdvice">-->
<!--            <aop:before method="before" pointcut-ref="pointcut"></aop:before>-->
<!--            <aop:after-returning method="afterReturning" pointcut-ref="pointcut"></aop:after-returning>-->
<!--            <aop:around method="around" pointcut-ref="pointcut"></aop:around>-->
<!--            <aop:after-throwing method="afterException" pointcut-ref="pointcut"></aop:after-throwing>-->
<!--            <aop:after method="after" pointcut-ref="pointcut"></aop:after>-->
<!--        </aop:aspect>-->
<!--    </aop:config>-->
</beans>
package com.qfedu.demo02;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
@Aspect
@Order(1)
public class Aspect01 {
//    切点@Pointcut("execution(* com.qfedu.demo02.UserServiceImpl.*(..))")public void  pointcut(){}
//    前置通知@Before("pointcut()")public void before(){System.out.println("这是Aspect01前置通知");}
//    后置通知@After("pointcut()")public void after(){System.out.println("这是Aspect01后置通知");}
}
package com.qfedu.demo02;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;@Aspect
@Order(0)
public class Aspect02 {//    切点@Pointcut("execution(* com.qfedu.demo02.UserServiceImpl.*(..))")public void  pointcut(){}//    前置通知@Before("pointcut()")public void before(){System.out.println("这是Aspect02前置通知");}//    后置通知@After("pointcut()")public void after(){System.out.println("这是Aspect02后置通知");}
}
package com.qfedu.demo02;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAspect {public static void main(String[] args) {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = applicationContext.getBean("userService", UserService.class);userService.select();}
}

基于Ordered接口配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:contxt="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd ">
<!--    注册bean--><bean name="userService" class="com.qfedu.demo02.UserServiceImpl"/>
<!--    <bean name="xmlAdvice" class="com.qfedu.demo02.XmlAdvice"/>-->
<!--    <bean name="annoAdvice" class="com.qfedu.demo02.AnnoAdvice"/>-->
<!--    <bean name="aspect01" class="com.qfedu.demo02.Aspect01"/>-->
<!--    <bean name="aspect02" class="com.qfedu.demo02.Aspect02"/>--><bean name="aspect03" class="com.qfedu.demo02.Aspect03"/><bean name="aspect04" class="com.qfedu.demo02.Aspect04"/><!--    开启@aspectj的自动代理支持/扫描包设置--><aop:aspectj-autoproxy/>
<!--&lt;!&ndash;配置Spring AOP&ndash;&gt;-->
<!--    <aop:config>-->
<!--&lt;!&ndash;        指定切点&ndash;&gt;-->
<!--        <aop:pointcut id="pointcut" expression="execution( * com.qfedu.demo02.UserServiceImpl.*(..))"/>-->
<!--&lt;!&ndash;        制定切面&ndash;&gt;-->
<!--        <aop:aspect ref="xmlAdvice">-->
<!--            <aop:before method="before" pointcut-ref="pointcut"></aop:before>-->
<!--            <aop:after-returning method="afterReturning" pointcut-ref="pointcut"></aop:after-returning>-->
<!--            <aop:around method="around" pointcut-ref="pointcut"></aop:around>-->
<!--            <aop:after-throwing method="afterException" pointcut-ref="pointcut"></aop:after-throwing>-->
<!--            <aop:after method="after" pointcut-ref="pointcut"></aop:after>-->
<!--        </aop:aspect>-->
<!--    </aop:config>-->
</beans>
package com.qfedu.demo02;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;@Aspect
public class Aspect03 implements Ordered {@Pointcut("execution(* com.qfedu.demo02.UserServiceImpl.*(..))")public void pointcut(){}//    前置通知@Before("pointcut()")public void before(){System.out.println("这是Aspect03前置通知");}//    后置通知@After("pointcut()")public void after(){System.out.println("这是Aspect03后置通知");}
//返回指定切面的优先级public int getOrder() {return 1;}
}
package com.qfedu.demo02;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;
@Aspect
public class Aspect04 implements Ordered {@Pointcut("execution(* com.qfedu.demo02.UserServiceImpl.*(..))")public void pointcut(){}//    前置通知@Before("pointcut()")public void before(){System.out.println("这是Aspect04前置通知");}//    后置通知@After("pointcut()")public void after(){System.out.println("这是Aspect04后置通知");}//返回指定切面的优先级public int getOrder() {return 2;}
}

基于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"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:contxt="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd "><!--    注册bean--><bean name="userService" class="com.qfedu.demo02.UserServiceImpl"/><bean name="aspect05" class="com.qfedu.demo02.Aspect05"/><bean name="aspect06" class="com.qfedu.demo02.Aspect06"/><!--配置Spring AOP--><aop:config><!--        指定切点--><aop:pointcut id="pointcut" expression="execution( * com.qfedu.demo02.UserServiceImpl.*(..))"/><!--        制定切面--><aop:aspect ref="aspect05" order="1"><aop:before method="before" pointcut-ref="pointcut"></aop:before><aop:after method="after" pointcut-ref="pointcut"></aop:after></aop:aspect><aop:aspect ref="aspect06" order="2"><aop:before method="before" pointcut-ref="pointcut"></aop:before><aop:after method="after" pointcut-ref="pointcut"></aop:after></aop:aspect></aop:config>
</beans>
package com.qfedu.demo02;public class Aspect05 {public  void before(){System.out.println("这是Aspect05的前置通知");}public  void after(){System.out.println("这是Aspect05的后置通知");}
}
package com.qfedu.demo02;public class Aspect06 {public  void before(){System.out.println("这是Aspect06的前置通知");}public  void after(){System.out.println("这是Aspect06的后置通知");}
}

三、性能及异常监控

性能监控

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:contxt="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd "><!--    注册bean--><bean name="service01" class="com.qfedu.demo03.Service01"/><bean name="recordAspect" class="com.qfedu.demo03.RecordAspect"/><aop:aspectj-autoproxy/>
</beans>
package com.qfedu.demo03;public class Service01 {public void service() throws InterruptedException {System.out.println("执行service()方法");Thread.sleep(1000);}
}
package com.qfedu.demo03;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;import java.util.Date;@Aspect
public class RecordAspect {//切点@Pointcut("execution( * com.qfedu.demo03.Service*.*(..))")public void record(){}@Around("record()")public Object recordTimer(ProceedingJoinPoint thisJoinPoint) throws Throwable{
//        获取目标对象类名称String className = thisJoinPoint.getTarget().getClass().getName();
//        方法名称String methodaName = thisJoinPoint.getSignature().getName();
//        消耗时间long startTime = System.currentTimeMillis();Object result = thisJoinPoint.proceed();long time = System.currentTimeMillis()-startTime;Record record = new Record();record.setExpendTime(time);record.setClassName(className);record.setMethodName(methodaName);record.setRecordTime(new Date());System.out.println(record.toString());return result;}}
package com.qfedu.demo03;import java.util.Date;public class Record {
//    类名称private String className;
//    方法名称private String methodName;
//    记录时间private Date recordTime;
//    程序执行耗费时间private Long expendTime;public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getMethodName() {return methodName;}public void setMethodName(String methodName) {this.methodName = methodName;}public Date getRecordTime() {return recordTime;}public void setRecordTime(Date recordTime) {this.recordTime = recordTime;}public Long getExpendTime() {return expendTime;}public void setExpendTime(Long expendTime) {this.expendTime = expendTime;}@Overridepublic String toString() {return "Record{" +"className='" + className + '\'' +", methodName='" + methodName + '\'' +", recordTime=" + recordTime +", expendTime=" + expendTime +'}';}
}
package com.qfedu.demo03;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestRecord {public static void main(String[] args) throws InterruptedException {ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext_record.xml");Service01 service01 = context.getBean("service01", Service01.class);service01.service();}
}

异常监控

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:contxt="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd "><!--    注册bean--><bean name="service02" class="com.qfedu.demo03.Service02"/><bean name="messageAspect" class="com.qfedu.demo03.MessageAspect"/><aop:aspectj-autoproxy/>
</beans>
package com.qfedu.demo03;import java.util.Date;public class Message {private String className;private String methodName;private Date recordTime;private String exceptionMsg;public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public String getMethodName() {return methodName;}public void setMethodName(String methodName) {this.methodName = methodName;}public Date getRecordTime() {return recordTime;}public void setRecordTime(Date recordTime) {this.recordTime = recordTime;}public String getExceptionMsg() {return exceptionMsg;}public void setExceptionMsg(String exceptionMsg) {this.exceptionMsg = exceptionMsg;}@Overridepublic String toString() {return "Message{" +"className='" + className + '\'' +", methodName='" + methodName + '\'' +", recordTime=" + recordTime +", exceptionMsg='" + exceptionMsg + '\'' +'}';}
}
package com.qfedu.demo03;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;import java.util.Date;@Aspect
public class MessageAspect {//切点@Pointcut("execution( * com.qfedu.demo03.Service*.*(..))")public void exectionMsg(){}@Around("exectionMsg()")public Object msgMethod(ProceedingJoinPoint thisJoinPoint) throws Throwable{
//        获取目标对象类名称String className = thisJoinPoint.getTarget().getClass().getName();
//        方法名称String methodaName = thisJoinPoint.getSignature().getName();try{return thisJoinPoint.proceed();}catch (MyException e){Message msg = new Message();msg.setClassName(thisJoinPoint.getTarget().getClass().getName());msg.setMethodName(thisJoinPoint.getSignature().getName());msg.setRecordTime(new Date());msg.setExceptionMsg(e.getMsg());System.out.println(msg.toString());return null;}}
}
package com.qfedu.demo03;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestMessage {public static void main(String[] args) throws Exception {ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext_msg.xml");Service02 service02 = context.getBean("service02", Service02.class);service02.service();}
}
package com.qfedu.demo03;public class Service02 {public void service() throws Exception{System.out.println("执行Service()方法");int num = 105;if(num>100||num<0){throw new MyException("您输入的不正确,请输入0~100之间的数字");}else {System.out.println("您输入的数字是"+num);}}
}
package com.qfedu.demo03;public class MyException extends Exception{private static final long serialVersionUID = 1L;private String msg;MyException(String msg){super();this.msg = msg;}public String getMsg(){return msg;}
}

四、工程目录及运行结果图

demo1和demo2的目录
在这里插入图片描述

JDK动态代理
在这里插入图片描述

CGLib动态代理

在这里插入图片描述

基于xml开发Spring AOP

在这里插入图片描述

基于注解开发Spring AOP

在这里插入图片描述

基于注解的配置

在这里插入图片描述
在这里插入图片描述

基于Ordered接口配置
在这里插入图片描述

在这里插入图片描述

基于XML配置

在这里插入图片描述
在这里插入图片描述

demo3目录
在这里插入图片描述

性能监控

在这里插入图片描述

异常监控

在这里插入图片描述


http://www.ppmy.cn/news/48030.html

相关文章

【STL】map容器

1、map容 map中所有元素都是pair组成pair中第一个元素为key&#xff08;键值&#xff09;&#xff0c;起到索引作用&#xff0c;第二个元素为value&#xff08;实值&#xff09;所有元素都会根据元素的键值自动 排序 map/multimap属于关联式容器&#xff0c;底层结构由二叉树…

8万字智慧旅游景区信息化建设方案word

本资料来源公开网络&#xff0c;仅供个人学习&#xff0c;请勿商用&#xff0c;如有侵权请联系删除。 1.1. 整体建设框架 XXXXXX智慧景区旅游建设对于全面整合景区旅游资源&#xff0c;提升景区旅游产业发展能级&#xff0c;进一步增强景区旅游业的核心竞争力具有十分重要的支…

《Vue3实战》 第五章 计算、监听属性

《Vue3实战》篇章整体栏目 ————————————————————————————— 【第一章】node.js/npm安装、配置 【第二章】创建项目和目录结构 【第三章】基础语法 【第四章】条件语句、循环语句 【第五章】计算、监听属性 【第六章】样式绑定和事件处理 【第七章】…

Ae:摄像机设置

Ae菜单&#xff1a;图层/摄像机设置 Camera Settings 快捷键&#xff1a;Ctrl Shift Y 新建摄像机图层时&#xff0c;首先会弹出摄像机设置 Camera Settings对话框。 经典 3D 渲染器时的摄像机设置 Cinema 4D 渲染器时的摄像机设置 类型 Type 有两种类型的摄像机供选择。 提…

【计算机视觉 | 图像分割】Segment Anything论文讲解

文章目录 一、前言二、论文出发点三、创新思路四、方法4.1 Segment Anything Task4.2 Segment Anything Model4.3 Segment Anything Data Engine4.4 Segment Anything Dataset 五、结果 一、前言 论文&#xff1a;https://arxiv.org/pdf/2304.02643.pdf 项目&#xff1a;https…

数据库基础篇 《2. MySQL环境搭建》

目录 1. MySQL的卸载 步骤1&#xff1a;停止MySQL服务 步骤2&#xff1a;软件的卸载 步骤3&#xff1a;残余文件的清理 ​编辑2. MySQL的下载、安装、配置 2.1 MySQL的4大版本 2.2 软件的下载 2.3 MySQL8.0 版本的安装 2.4 配置MySQL8.0 2.5 配置MySQL8.0 环境变量 …

Python办公自动化之PostgreSQL篇2——利用Python连接PostgreSQL并读取一张表

在上一篇我们已经安装好了最新的PostgreSQL&#xff0c;以及最方便的可视化工具&#xff0c;Navicat 如果错过的小伙伴&#xff0c;可以去上一篇查看&#xff1a;点我查看 今天我们来用Python连接一下PostgreSQL&#xff0c;然后准备一张测试表&#xff0c;导入PostgreSQL&am…

数据爬虫爬数据时常遇到的问题

第一&#xff0c;可能不能直接通过pyppeteer简单的直接page.querySelectorAll()获取所有元素然后直接 str(await (await element_songer_name.getProperty("title")).jsonValue()) 得到数据值&#xff0c;主要原因可能是#document问题&#xff0c; 所以遇到这个问题…