千家信息网

Sping aop面向切面编程通知的方法是什么

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,本篇内容介绍了"Sping aop面向切面编程通知的方法是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学
千家信息网最后更新 2025年02月02日Sping aop面向切面编程通知的方法是什么

本篇内容介绍了"Sping aop面向切面编程通知的方法是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

建议先看看:java静态代理和动态代理

一、创建两个接口:

接口TestServiceInter:

package com.hubin.aop;public interface TestServiceInter {    public void sayHello();}

接口TestServiceInter2:

package com.hubin.aop;public interface TestServiceInter2 {    public void sayBye();}

二、创建被代理类Test1Service 该类实现了上面的两个接口

package com.hubin.aop;/** *  *  * 被代理类,相当于我自己干事情 * @author Administrator * */public class Test1Service implements TestServiceInter,TestServiceInter2 {    private String name;        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public void sayHello() {        // TODO Auto-generated method stub        System.out.println("hi "+name);    }    public void sayBye() {        // TODO Auto-generated method stub        System.out.println("bye "+name);    }}

三、创建前置通知MyMethodBeforeAdvice类

package com.hubin.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;/** * 前置通知类,类似于中介公司实现我交代的事情之前做的一些事情 * @author Administrator * */public class MyMethodBeforeAdvice implements MethodBeforeAdvice{    /**     * method: 被调用方法名字     * args: 给method传递的参数     * target: 目标对象     */    public void before(Method method, Object[] args, Object target)            throws Throwable {        // TODO Auto-generated method stub        System.out.println("前置通知被调用");    }}

四、创建后置通知类MyMethodAfterAdvice :

package com.hubin.aop;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;/** * 后置通知 * @author Administrator * */public class MyMethodAfterAdvice implements AfterReturningAdvice{    @Override    public void afterReturning(Object returnValue, Method method,            Object[] args, Object target) throws Throwable {        // TODO Auto-generated method stub        System.out.println("后置通知被调用");            }}

五、创建环绕通知类MyMethodAroundAdvice:

package com.hubin.aop;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MyMethodAroundAdvice implements MethodInterceptor {    @Override    public Object invoke(MethodInvocation arg0) throws Throwable {        // TODO Auto-generated method stub        System.out.println("调用环绕通知前");        arg0.proceed();//这句话不调用的话,被代理类的方法不会运行        System.out.println("调用环绕通知后");        return null;    }}

六、创建异常通知MyMethodThrowsAdvice:

package com.hubin.aop;import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;/** * 异常对象 * @author Administrator * */public class MyMethodThrowsAdvice implements ThrowsAdvice {    public void afterThrowing(Method m,Object []obj,Object target,Exception e){        System.out.println("出事了"+e.getMessage());    }}

七、创建配置文件beans.xml(文件位置在包com/hubin/aop/)

                 com.hubin.aop.TestServiceInter        com.hubin.aop.TestServiceInter2            myMethodBeforeAdvice    myMethodAfterAdvice    myMethodAroundAdvice    myMethodThrowsAdvice

八、随便创建一个类用来做测试

package com.hubin.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AopTest {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        ApplicationContext ac=new ClassPathXmlApplicationContext("com/hubin/aop/beans.xml");        TestServiceInter ts=(TestServiceInter) ac.getBean("proxyFactoryBean");        ts.sayHello();        //((TestServiceInter2)ts).sayBye();            }}

九、运行结果:

前置通知被调用调用环绕通知前hi 王大锤调用环绕通知后后置通知被调用前置通知被调用调用环绕通知前bye 王大锤调用环绕通知后后置通知被调用

"Sping aop面向切面编程通知的方法是什么"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0