UserDao: 实体接口类
package com.student.jdk;
public interface UserDao {
public void addUser();
public void deleteUser();
}
实体类
package com.student.jdk;
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
System.out.println("添加用户!!!!");
}
@Override
public void deleteUser() {
System.out.println("删除用户!!!!");
}
}
切面类:
package com.student.aspectj.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
//前置通知
public void myBefore(JoinPoint joinPoint)
{
System.out.print("正在执行前置通知!!!,权限检查中....");
System.out.print("目标类是:"+joinPoint.getTarget());
System.out.println(",被植入的目标方法是:"+joinPoint.getSignature().getName());
}
//后置通知
public void myAfterReturning(JoinPoint joinPoint)
{
System.out.print("正在执行后置通知!!!,记录日志中....");
System.out.println(",被植入的目标方法是:"+joinPoint.getSignature().getName());
}
//环绕通知
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
{
System.out.println("正在执行环绕通知!!!,模拟开启事务中...");
Object obj = proceedingJoinPoint.proceed();
System.out.println("环绕结束,模拟关闭事物中...");
return obj;
}
//异常通知
public void myAfterThrowing(JoinPoint joinPoint,Throwable e)
{
System.out.println("异常通知:"+e.getMessage());
}
//最终通知
public void myAfter()
{
System.out.println("最终通知:释放资源...");
}
}
配置文件:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userDao" class="com.student.jdk.UserDaoImpl"></bean>
<bean id="myAspect" class="com.student.aspectj.xml.MyAspect"></bean>
<aop:config>
<aop:aspect ref="myAspect">
<aop:pointcut expression="execution(* com.student.jdk.*.*(..))" id="myPointCut"/>
<aop:before method="myBefore" pointcut-ref="myPointCut"/>
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/>
<aop:around method="myAround" pointcut-ref="myPointCut"/>
<aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/>
<aop:after method="myAfter" pointcut-ref="myPointCut"/>
</aop:aspect>
</aop:config>
</beans>
测试类:
package com.student.aspectj.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.student.jdk.UserDao;
public class TestXmlAspectj {
public static void main(String[] args) {
String xmlPath = "com/student/aspectj/xml/applictionContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.addUser();
}
}
运行结果:
十月 16, 2019 11:36:30 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@69d0a921: startup date [Wed Oct 16 11:36:30 CST 2019]; root of context hierarchy
十月 16, 2019 11:36:30 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/student/aspectj/xml/applictionContext.xml]
正在执行前置通知!!!,权限检查中....目标类是:com.student.jdk.UserDaoImpl@2aa5fe93,被植入的目标方法是:addUser
正在执行环绕通知!!!,模拟开启事务中...
添加用户!!!!
最终通知:释放资源...
环绕结束,模拟关闭事物中...
正在执行后置通知!!!,记录日志中....,被植入的目标方法是:addUser
修改最终通知:
//最终通知
public void myAfter()
{
System.out.println("最终通知:释放资源...");
int b=10/0;
}
运行结果:
十月 16, 2019 11:43:56 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@69d0a921: startup date [Wed Oct 16 11:43:56 CST 2019]; root of context hierarchy
十月 16, 2019 11:43:56 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/student/aspectj/xml/applictionContext.xml]
正在执行前置通知!!!,权限检查中....目标类是:com.student.jdk.UserDaoImpl@2aa5fe93,被植入的目标方法是:addUser
正在执行环绕通知!!!,模拟开启事务中...
添加用户!!!!
最终通知:释放资源...
异常通知:/ by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.student.aspectj.xml.MyAspect.myAfter(MyAspect.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:611)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:50)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85)
at com.student.aspectj.xml.MyAspect.myAround(MyAspect.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy2.addUser(Unknown Source)
at com.student.aspectj.xml.TestXmlAspectj.main(TestXmlAspectj.java:14)