• 顾问包装通知




    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>
  • 相关阅读:
    (转载)SAPI 包含sphelper.h编译错误解决方案
    C++11标准的智能指针、野指针、内存泄露的理解(日后还会补充,先浅谈自己的理解)
    504. Base 7(LeetCode)
    242. Valid Anagram(LeetCode)
    169. Majority Element(LeetCode)
    100. Same Tree(LeetCode)
    171. Excel Sheet Column Number(LeetCode)
    168. Excel Sheet Column Title(LeetCode)
    122.Best Time to Buy and Sell Stock II(LeetCode)
    404. Sum of Left Leaves(LeetCode)
  • 原文地址:https://www.cnblogs.com/lowerma/p/11778547.html
Copyright © 2020-2023  润新知