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

这就是带有切点的切面


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>

day39-Spring 06-Spring的AOP:带有切点的切面的更多相关文章

  1. day39-Spring 05-Spring的AOP:不带有切点的切面

    Spring底层的代理的实现: 不带切点的切面是对类里面的所有的方法都进行拦截. 做Spring AOP的开发需要两个包:一个是AOP的包,一个是AOP联盟的包(因为规范是由AOP联盟提出来的). 用 ...

  2. Spring AOP(通知、连接点、切点、切面)

    一.AOP术语 通知(Advice)  切面的工作被称为通知.通知定义了切面是什么以及何时使用.除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题.5种通知类型: 前置通知(Before): ...

  3. Spring的IOC和AOP之深剖

    今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...

  4. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  5. Spring基础学习(四)—AOP

    一.AOP基础 1.基本需求      需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...

  6. Spring MVC 中使用AOP 进行统一日志管理--注解实现

    1.AOP简介 AOP称为面向切面编程 AOP的基本概念 (1)Aspect(切面):通常是一个类,里面可以定义切入点和通知 (2)JointPoint(连接点):程序执行过程中明确的点,一般是方法的 ...

  7. Spring学习总结(7)-AOP

    参考资料:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#aop 1 ...

  8. Java Spring的IoC和AOP的知识点速记

    Spring简介 Spring解决的最核心的问题就是把对象之间的依赖关系转为用配置文件来管理,这个是通过Spring的依赖注入机制实现的. Spring Bean装配 1. IOC的概念以及在Spri ...

  9. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

随机推荐

  1. Hdu 2376

    题目链接 题意:给出一颗含有n个结点的树,树上每条边都有一个长度,求树上所有路径的平均长度. 考虑树上每条边对所有路径长度和的贡献,对于每条偶 就是边的两个短点u和v,只需要记录以u为根的子树的结点的 ...

  2. 【arc072f】AtCoder Regular Contest 072 F - Dam

    题意 有一个体积为L的水池,有N天 每天早上进水Vi体积的Ti温度的水. 每天晚上可以放掉任意体积的水. 问每天中午,水池满的情况下,水温最高多少. 水的温度只受新加进的谁的影响,对于水\(W1(T1 ...

  3. [code] if (x<0)x=0;else if (x>255)x=255;

    //颜色范围0-255: // 1.原始: )tem_b=;)tem_b=; )tem_g=;)tem_g=; )tem_r=;)tem_r=; //2.使用条件状态值生成掩码来移除条件分支 tem_ ...

  4. Composer安装Yii2以及相关扩展

    1.安装redis扩展 命令:composer require yiisoft/yii2-redis Git地址:https://github.com/yiisoft/yii2-redis/blob/ ...

  5. hadoop面试题及答案解析

    1.(Datanode)程序负责HDFS数据存储. 2.HDFS中的block默认保存(3份). 3.(TaskTracker)程序通常与NameNode在一个节点启动. 分析:hadoop集群是基于 ...

  6. 常见问题:MongoDB基础知识

    常见问题:MongoDB基础知识 ·MongoDB支持哪些平台? ·MongoDB作为托管服务提供吗? ·集合(collection)与表(table)有何不同? ·如何创建数据库(database) ...

  7. webpack打包js,css和图片

    1.webpack打包默认配置文件webpack.config.js 2.打包js文件:有这个文件并配置可以直接在cmd上webpack打包,没有这个文件要在cmd上输入 webpack main.j ...

  8. ES6学习笔记之解构赋值

    1.数组的解构赋值 简单用法 { // 旧 let a=1,b=3; //新 let [a,b]=[1,3]; console.log(a,b);// 1 3 } 只要等号两边的模式相同,左边的变量就 ...

  9. 006-使用python编写一个猜数字的程序

    题目:随机生成一个数字,共有三次机会对该数字进行猜测. #功能点# 1.猜错的时候给出提示,告诉用户输入的值是大了还是小了# 2.最多提供三次机会# 3.随机生成需要猜的数字答案 编写思路: 1.刚开 ...

  10. 如何在TypeScript中使用JS类库

    使用流程 1.首先要清除类库是什么类型,不同的类库有不同的使用方式 2.寻找声明文件 JS类库一般有三类:全局类库.模块类库.UMD库.例如,jQuery是一种UMD库,既可以通过全局方式来引用,也可 ...