http://blog.youkuaiyun.com/qq_34120041/article/details/53817252
常用的AOP两种方式:
1、Scheme-AOP-配置文件的切面
2、@Aspectj -基于注解
案例:配置文件的切面
1、接口IPerson和实现类XiaoBaiImpl
package com.aop.schema;
public interface IPerson {
public void addPerson();
public void addNum(int num,String name);
public void updatePerson();
}
package com.aop.schema;
import org.springframework.stereotype.Component;
@Component
public class XiaoBaiImpl implements IPerson {
@Override
public void addPerson() {
System.out.println("add小白!");
}
@Override
public void updatePerson() {
System.out.println("update小白!");
}
@Override
public void addNum(int num, String name) {
System.out.println("添加Num");
}
public void del() {
try {
XiaoBaiImpl xiaoBaiImpl = null;
xiaoBaiImpl.addNum(1, "fyl");
} catch (RuntimeException e) {
throw new CustomException(XiaoBaiImpl.class, "del", e.getMessage());
}
}
}
2、前置通知、环绕通知
package com.aop.schema;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
@Component
public class AroundMethod {
public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("事务开启了......");
joinPoint.proceed();
System.out.println("事务结束了.......");
}
public void beforeAdvice() {
System.out.println("我是前置通知!!");
}
}
3、自定义异常类CustomException、异常通知AroundThrowingMethod
package com.aop.schema;
public class CustomException extends RuntimeException {
private static final long serialVersionUID = 8308193234255898346L;
private Class clz;
private String method;
public CustomException() {
super();
}
public Class getClz() {
return clz;
}
public void setClz(Class clz) {
this.clz = clz;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public CustomException(Class clz, String method, String message) {
super(message);
this.clz = clz;
this.method = method;
}
public CustomException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public CustomException(String message, Throwable cause) {
super(message, cause);
}
public CustomException(String message) {
super(message);
}
public CustomException(Throwable cause) {
super(cause);
}
}
package com.aop.schema;
import org.springframework.stereotype.Component;
@Component
public class AroundThrowingMethod {
public void aroundThrowAdvice(CustomException ce) throws Throwable{
System.out.println("抛异常的类是:"+ce.getClz().getName());
System.out.println("抛异常的方法是:"+ce.getMethod());
System.out.println("事務回滾..."+ce.getMessage());
}
}
4、spring 2.5 通知类MethodInterceptorAdvice
package com.aop.schema;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;
@Component
public class MethodInterceptorAdvice implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
//执行的类
System.out.println("执行的类:"+invocation.getThis().getClass().getName());
//获取方法名
Method method = invocation.getMethod();
System.out.println("执行的方法:"+method.getName());
//获取参数
Object[] params = invocation.getArguments();
if (params != null) {
for (int i = 0; i < params.length; i++) {
System.out.println("参数"+i+"值为:"+String.valueOf(params[i]));
}
}
System.out.println("MethodInterceptor开启...");
long btime = System.currentTimeMillis();
//执行具体的方法
Object obj = invocation.proceed();
System.out.println("MethodInterceptor结束...");
long etime = System.currentTimeMillis() - btime;
System.out.println("=========共耗时:"+etime+"ms");
return obj;
}
}
5、bean.xml
<!-- 扫包 -->
<context:component-scan base-package="com.aop.schema"></context:component-scan>
<!--
1:Scheme-AOP-配置文件的切面
2:@Aspectj aop
-->
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* add*(..))" id="before"/>
<!-- 通知类 :前置通知,后置通知,环绕通知,异常通知,引介通知(针对(对象))-->
<aop:aspect ref="aroundMethod">
<!-- 环绕通知-方法aroundAdvice -->
<aop:around method="aroundAdvice" pointcut="execution(* update*(..))"/>
<!-- 前置通知-方法beforeAdvice -->
<aop:before method="beforeAdvice" pointcut-ref="before"/>
</aop:aspect>
<aop:aspect ref="aroundThrowingMethod">
<!-- 异常通知-方法beforeAdvice -->
<aop:after-throwing method="aroundThrowAdvice" pointcut="execution(* del*(..))" throwing="ce"/>
</aop:aspect>
</aop:config>
<!-- schemeaop + springaop2.5x 环绕型通知 -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="methodInterceptorAdvice" pointcut="execution(* com.aop.schema.XiaoBaiImpl.addN*(..))"/>
</aop:config>
6、测试类
package com.aop.schema;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:com/aop/schema/bean.xml");
XiaoBaiImpl xiaoBaiImpl = (XiaoBaiImpl) context.getBean("xiaoBaiImpl");
xiaoBaiImpl.addPerson();
System.out.println("------------------------------");
xiaoBaiImpl.updatePerson();
System.out.println("------------------------------");
xiaoBaiImpl.addNum(12, "fyl");
System.out.println("------------------------------");
xiaoBaiImpl.del();
}
}
7、结果
一月 05, 2017 3:08:38 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ccabbaa: startup date [Thu Jan 05 15:08:38 CST 2017]; root of context hierarchy
一月 05, 2017 3:08:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/aop/schema/bean.xml]
我是前置通知!! --前置通知
add小白!
------------------------------
事务开启了...... --环绕通知
update小白!
事务结束了.......
------------------------------
我是前置通知!! --前置通知
执行的类:com.aop.schema.XiaoBaiImpl --MethodInterceptor
执行的方法:addNum
参数0值为:12
参数1值为:fyl
MethodInterceptor开启...
添加Num
MethodInterceptor结束...
=========共耗时:1ms
------------------------------
抛异常的类是:com.aop.schema.XiaoBaiImpl
抛异常的方法是:del
事務回滾...null
Exception in thread "main" com.aop.schema.CustomException
at com.aop.schema.XiaoBaiImpl.del(XiaoBaiImpl.java:28)
at com.aop.schema.XiaoBaiImpl$$FastClassBySpringCGLIB$$cf1f1ef7.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
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.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.aop.schema.XiaoBaiImpl$$EnhancerBySpringCGLIB$$2d1b53f8.del(<generated>)
at com.aop.schema.Test.main(Test.java:17)