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>
  • 相关阅读:
    SVM – 线性分类器
    解决mybatis generator无法覆盖XML
    windows下IDEA的terminal配置bash命令
    mysqldump定时备份数据库
    linux清理日志脚本
    MySQL主从同步配置
    mysql binlog日志自动清理及手动删除
    linux搭建FTP服务器并整合Nginx
    mysql解除死锁状态
    git取消跟踪已版本控制的文件
  • 原文地址:https://www.cnblogs.com/lowerma/p/11778547.html
Copyright © 2011-2022 走看看