顾问包装通知

通知(advice)是Spring中的一种比较简单的切面,只能将切面织入到目标类的所有方法中,而无法对指定方法进行增强

顾问(advisor)是Spring提供的另外一种切面,可以织入到指定的方法中  接口PointcutAdvisor

第一步:定义接口以及实现类

package com.yjc.advisor;

public interface DoSomeService {
    void doSome();
    void doSome2();
}
---------------------------------------------------------------
package com.yjc.advisor;

public class DoSomeServiceImpl implements DoSomeService {
    @Override
    public void doSome() {
        System.out.println("doSome===============================");
    }

    @Override
    public void doSome2() {
        System.out.println("doSome2==========================================");
    }
}

第二步:定义增强类

package com.yjc.advisor;


import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforeAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("前置增强");
    }

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("后置增强");
    }
}

第三步,分别使用名称和正则表达式匹配方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dosome" class="com.yjc.advisor.DoSomeServiceImpl"></bean>

    <bean id="advice" class="com.yjc.advisor.BeforeAdvice"></bean>

    <!--基于方法名称的增强顾问-->
    <!--<bean id="advisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="advice"></property>
  <!--使用,逗号分隔-->
<property name="mappedNames" value="doSome,doSome2"></property>
</bean>--> <!--基于正则表达式的增强顾问--> <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="advice"></property>
  <!--.代表单个字符,*代表前一项出现0-n次,+号代表前一项出现1-n次--> <property name="pattern" value=".*do.*"></property> </bean>
  <!--使用的代理工厂-->   <!--<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="dosome"></property> <property name="interceptorNames" value="advisor"></property> &lt;!&ndash;value代表只使用cglib动态代理,默认值为false,真实对象为接口时使用jdk,没有接口时使用cglib &ndash;&gt; <property name="proxyTargetClass" value="true"></property> </bean>-->
  <!--自动顾问代理生成器--> <!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>-->   <!--名称顾问代理生成器--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames" value="dosome"/> <property name="interceptorNames" value="advisor"/> </bean> </beans>

第四步:测试

package com.yjc.advisor;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AdviceTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/yjc/advisor/application_advisor.xml");
        DoSomeService proxyFactory = applicationContext.getBean("dosome", DoSomeService.class);
        proxyFactory.doSome();
        proxyFactory.doSome2();
    }
}

 

posted @ 2019-11-01 16:55  天戈  阅读(150)  评论(0编辑  收藏  举报