(14)Spring AOP Advisor

来源:互联网 发布:mac cs6破解文件下载 编辑:程序博客网 时间:2024/06/05 10:40

只能有一个通知,但是感觉很强大

被拦截类的接口

package shuai.spring.study.service;public interface IHelloService {    public void sayHello();    public String returnHello(String param0, String param1);}

被拦截类

package shuai.spring.study.service.impl;import shuai.spring.study.Info;import shuai.spring.study.service.IHelloService;public class HelloServiceImpl implements IHelloService {    @Override    public void sayHello() {        System.out.println("============Hello World!");    }    @Override    public String returnHello(String param0, String param1) {        System.out.println("返回前输出,两个参数:" + param0 + "和" + param1);        return Info.info(param0, param1);    }}

Info类

package shuai.spring.study;public class Info {    public static String info() {        System.out.println("返回时输出:hello world 1");        return "返回值:hello world 1";    }    public static String info(String param0, String param1) {        System.out.println("返回时输出:" + param0 + "," + param1);        return "返回值为:" + param0 + "," + param1;    }}

配置文件

<?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="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/>                <bean id="helloAdvisorBefore" class="shuai.spring.study.aop.HelloAdvisorBefore"/>        <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->        <aop:config>        <aop:pointcut expression="execution(* shuai.spring.study.service..*.*(..))" id="pointcut"/>        <aop:advisor         advice-ref="helloAdvisorBefore"        pointcut-ref="pointcut"/>        </aop:config></beans>

注意:advice-ref引用的bean必须要实现某个通知的接口,比如这里的HelloAdvisorBefore要实现前置通知的接口MethodBeforeAdvice,就是一个通知类,目标方法被拦截了会到这里执行。

定义HelloAdvisorBefore类

package shuai.spring.study.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class HelloAdvisorBefore implements MethodBeforeAdvice {    @Override    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {        System.out.println("所在方法的名字为:" + arg0.getName());        for (int i = 0; i < arg1.length; i++) {            System.out.println("第" + (i + 1) + "个参数的值为:" + arg1[i]);        }        System.out.println("所在类的toString方法为:" + arg2);    }}

测试类

package shuai.spring.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import shuai.spring.study.service.IHelloService;public class HelloTest {    @Test    public void testHelloWorld() {        @SuppressWarnings("resource")        ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml");        IHelloService iHelloService = context.getBean("iHelloService", IHelloService.class);        iHelloService.returnHello("hello", "world");    }}

测试结果:

所在方法的名字为:returnHello
第1个参数的值为:hello
第2个参数的值为:world
所在类的toString方法为:shuai.spring.study.service.impl.HelloServiceImpl@7ff95560
返回前输出,两个参数:hello和world
返回时输出:hello,world


原创粉丝点击