顾问包装通知



Interfaceadvrsor接口


public interface Interfaceadvrsor {

    public void doSome();


    public void say();



}
Springadvrsor实现类
public class Springadvrsor implements Interfaceadvrsor
{

    @Override
    public void doSome() {
        System.out.println("正式业务");
    }

    @Override
    public void say() {
        System.out.println("无聊");
    }
}
SpringAopAdvice增强类
public class SpringAopAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("前者");
    }
    @Override
    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节点-->
<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"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="springadvrsor" class="cn.spring.Springadvrsor"></bean>
    <bean id="springAopAdvice" class="cn.spring.SpringAopAdvice"></bean>
    <bean id="PointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice" ref="springAopAdvice"></property>
    </bean>
    <bean id="ProxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="springadvrsor"></property>
        <property name="interceptorNames" value="PointcutAdvisor"></property>
    </bean>

</bean>

自动顾问代理生成器

applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--根节点是我们的beans节点-->
<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"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="springadvrsor" class="cn.spring.Springadvrsor"></bean>
    <bean id="springAopAdvice" class="cn.spring.SpringAopAdvice"></bean>
    <bean id="PointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice" ref="springAopAdvice"></property>

        <!--
        .  代表单个字符
        *   代表前一项出现0-n次
        +   代表前一项出现1-n次
        至少包含两个字符
        -->
    <property name="patterns">
        <list>
            <value>.*do.*</value>
        </list>
    </property>
    </bean>
    <!--默认顾问自动代理生成器-->
<!--<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>-->
</beans>

名称顾问代理生成器

<?xml version="1.0" encoding="UTF-8"?>
<!--根节点是我们的beans节点-->
<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"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="springadvrsor" class="cn.spring.Springadvrsor"></bean>
    <bean id="springAopAdvice" class="cn.spring.SpringAopAdvice"></bean>
    <bean id="PointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    <property name="advice" ref="springAopAdvice"></property>
 
    <!--BeanName自动代理生成器-->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames" value="springadvrsor"></property>
        <property name="interceptorNames" value="PointcutAdvisor"></property>
    </bean>

</beans>

猜你喜欢

转载自www.cnblogs.com/lowerma/p/11778547.html
今日推荐