zoukankan      html  css  js  c++  java
  • 顾问包装通知



    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>
  • 相关阅读:
    Java之时间处理(当前年的上一年、上一季度、当月、当季)
    Nginx代理之大文件下载失败问题
    PageHelper之排序
    MySQL之集群配置
    Java之判断字符串是否为数字(包含浮点型数据)
    /bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file:
    Error Code: 1153
    413 Request Entity Too Large
    MyBatis分页插件失效问题之解决
    HikariConfig 连接池属性详解
  • 原文地址:https://www.cnblogs.com/lowerma/p/11778547.html
Copyright © 2011-2022 走看看