您好,登录后才能下订单哦!
本篇内容介绍了“Sping aop面向切面编程通知的方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
一、创建两个接口:
接口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/)
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 配置被代理的对象 --> <bean id="test1Service" class="com.hubin.aop.Test1Service"> <property name="name" value="王大锤" /> </bean> <!-- 配置前置通知 --> <bean id="myMethodBeforeAdvice" class="com.hubin.aop.MyMethodBeforeAdvice" /> <!-- 配置后置通知 --> <bean id="myMethodAfterAdvice" class="com.hubin.aop.MyMethodAfterAdvice" /> <!-- 配置环绕通知 --> <bean id="myMethodAroundAdvice" class="com.hubin.aop.MyMethodAroundAdvice"/> <!-- 配置异常通知 --> <bean id="myMethodThrowsAdvice" class="com.hubin.aop.MyMethodThrowsAdvice"/> <!-- 配置代理对象(代理对象不需要我们自己写,已经有现成的ProxyFactoryBean类存在了) --> <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 代理接口集 --> <property name="proxyInterfaces"> <list> <!--ProxyFactoryBean类会实现下列接口(必须是接口的全路径) --> <value>com.hubin.aop.TestServiceInter</value> <value>com.hubin.aop.TestServiceInter2</value> </list> </property> <!-- 把通知织入到代理对象相当于将通知和代理对象关联 --> <property name="interceptorNames"> <list> <!-- 必须和配置通知的ben的id对应值是相同的 --> <value>myMethodBeforeAdvice</value> <value>myMethodAfterAdvice</value> <value>myMethodAroundAdvice</value> <value>myMethodThrowsAdvice</value> </list> </property> <!-- 配置被代理对象,ref必须和配置代理对象的id对应值相同--> <property name="target" ref="test1Service"/> </bean> </beans>
八、随便创建一个类用来做测试
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面向切面编程通知的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。