zoukankan      html  css  js  c++  java
  • day39-Spring 06-Spring的AOP:带有切点的切面

    环绕增强功能是最强的,它相当于前置增强和后置增强.

    这就是带有切点的切面


    package cn.itcast.spring3.demo4;
    
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    
    //import org.springframework.cglib.proxy.MethodInterceptor;
    
    /**
     * 增强的类:
     * 使用的是环绕增强
     * @author zhongzh
     *
     */
    /**
     * 环绕增强功能是最强的,它相当于前置增强和后置增强.
     * @author zhongzh
     *
     */
    public class MyAroundAdvice implements MethodInterceptor{
    
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            // TODO Auto-generated method stub
            System.out.println("环绕前增强.....");
            Object result = methodInvocation.proceed();//放行,相当于执行你目标对象的方法. 执行目标对象的方法
            
            
            System.out.println("环绕后增强.....");
            
            return result;
        }
    
    }
    package cn.itcast.spring3.demo4;
    /**
     * 目标对象
     * @author zhongzh
     *
     */
    public class OrderDao {
        public void add(){
            System.out.println("添加订单");    
        }
        public void update(){
            System.out.println("修改订单");    
        }
        public void delete(){
            System.out.println("删除订单");    
        }
        public void find(){
            System.out.println("查询订单");    
        }
    }
    package cn.itcast.spring3.demo4;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class SpringTest4 {
       @Autowired
       //@Qualifier("orderDao")
       @Qualifier("orderDaoProxy")
       private OrderDao orderDao;
       
       @Test
       public void demo1(){
           orderDao.add();
           orderDao.delete();
           orderDao.update();
           orderDao.find();
       }
    }
    <?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"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!-- 不带有切点的切面 -->
        
        <!-- 定义目标对象 -->
         <bean id="customerDao" class="cn.itcast.spring3.demo3.CustomerDaoImpl">
      
      </bean>
    <!-- 定义增强  增强对象-->
      <bean id="beforeAdvice" class="cn.itcast.spring3.demo3.MyBeforeAdvice">
      </bean>
      
      <!-- 手动生成代理太麻烦了.Spring支持配置生成代理: -->
      <!-- 代理对象 -->
      <bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
          <!-- 设置目标对象 -->
          <property name="target" ref="customerDao"></property>
          <!-- 设置实现的接口,value中写接口的全路径 -->
          <!-- 类实现的接口的全路径 -->
          <property name="proxyInterfaces" value="cn.itcast.spring3.demo3.CustomerDao"></property>
          <!-- 需要使用value:要的名称 使用beforeAdvice对它进行增强 -->
          <property name="interceptorNames" value="beforeAdvice"></property><!--interceptorNames要拦截的名称  -->
          <!-- 这个是针对类的所有方法进行拦截的,你还没到配置方法的那一步呢 -->
      </bean>
      
      <!-- 带有切点的切面 -->
      <!-- 定义目标对象 -->
      
     <bean id="orderDao" class="cn.itcast.spring3.demo4.OrderDao"></bean>
    
      <!-- 定义增强 -->
       <bean id="aroundAdvice" class="cn.itcast.spring3.demo4.MyAroundAdvice"></bean>
       
       <!-- 定义切点切面: --><!-- 正则的方法切点切面 -->
       <bean id="mypointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
       <!-- 要写一个正则规定它里面哪些方法吧 --><!--定义正则表达式,规定哪些方法执行拦截  -->
       <!-- .任意字符  *  任意个 -->
       <!--  
       <property name="pattern" value=".*"></property>
       -->
       <!--  
       <property name="pattern" value="cn.itcast.spring3.demo4.OrderDao.add.*"></property>
       -->
       <property name="patterns" value=".*add.*,.*find.*"></property>
       <!-- 应用增强 -->
       <property name="advice" ref="aroundAdvice"></property><!-- 把定义的增强应用上了 -->
       </bean>
       <!-- 定义生成代理对象 -->
       <bean id="orderDaoProxy"  class="org.springframework.aop.framework.ProxyFactoryBean">
                <!-- 配置目标 -->
            <property name="target" ref="orderDao"></property>
             <!-- 针对类的代理 -->
             <property name="proxyTargetClass" value="true"></property>
             <!-- 在目标上应用增强 -->
             <property name="interceptorNames" value="mypointcutAdvisor"></property>
       </bean>
           
       
    
    </beans>
  • 相关阅读:
    时间序列深度学习:seq2seq 模型预测太阳黑子
    时间序列分析工具箱——sweep
    WebApi 找到了与该请求匹配的多个操作
    EF(EntityFramework)与mysql使用,错误终极解决方案
    EF(EntityFramework) 插入或更新数据报错
    EF(EntityFramework)与mysql使用,取数据报错,linq实体映射错误
    EF(EntityFramework)与mysql使用,序列化问题[System.ObjectDisposedException]
    EF(EntityFramework)与mysql使用,乱码问题
    c# AutoMapper 使用方式
    c# json 序列化时遇到错误 error Self referencing loop detected for type
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/6782292.html
Copyright © 2011-2022 走看看