Spring Advisor

SpringAdvisor 顾问:在通知的基础之上,在细入我们的切面AOP

通知和顾问都是切面的实现方式

通知是顾问的一个属性

顾问会通过我们的设置,将不同的通知,在不同的时间点把切面织入不同的切入点。

PointCutAdvisor接口!

比较常用的两个实现类

1 根据切入点(主业务方法)名称  织入切面NameMatchMethodPointCutAdvisor

2 根据自定义的正则 表达式织入切面 RegexpMethodPointoutAdvisor                           

接口

public interface UserService {
    public void addUser();
    public  String eat();
}

实现类

public class UserServiceImpl implements UserService {
    public void addUser() {
        System.out.println("添加用户");
    }

    public String eat() {
        System.out.println("eat  a little apple");
        return "apple";
    }
}

前置增强

public class BeforeAdvice implements MethodBeforeAdvice {
    /**
     *
     * @param method 目标方法
     * @param objects  目标方法的参数列表
     * @param o  目标对象
     * @throws Throwable
     */
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("前置增强---------------");
    }
}

后置增强

public class AfterAdvice implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("后置增强");
    }
}

ApplicationContext.xml

<?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 ="userservice" class="cn.kitty.service.UserServiceImpl"></bean>
    <!--配置前置通知-->
    <bean id="beforeAdvice" class="cn.kitty.service.BeforeAdvice"></bean>
    <!--配置后置通知-->
    <!--<bean id="afterAdvice" class="cn.kitty.service.AfterAdvice"></bean>-->
     <!--配置顾问   根据名称-->
    <!--<bean id="Myadvisors" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="mappedNames" value="addUser"></property>
配置切入点
单个方法使用mappedName

多个方法使用mappedNames
 
</bean>-->  <!--配置顾问 自定义的配置根据正则表达式织入切面-->  <bean id="Myadvisors" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="beforeAdvice"></property> <property name="pattern" value=".*a.*"></property> </bean>

配置切入点
单个表达式使用pattern
多个表达式使用patterns
<!-- 配置代理工厂bean 生成代理类 把通知织入到目标对象--> <bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--注册目标对象--> <property name="target" ref="userservice"></property> <!--注册顾问 --> <property name= "interceptorNames" value="Myadvisors"></property>

</bean> </beans>

test 测试类

public class Test1011 {
    @Test
    public void aadvisor(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService service= (UserService) context.getBean("userProxy");
        service.addUser();
    }
}

 

posted @ 2017-10-16 21:41  春眠不觉笑  阅读(1091)  评论(0编辑  收藏  举报