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

这就是带有切点的切面


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. light oj 1037 状压dp

    #include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...

  2. 爱上一门语言不需要理由——我的js之路

    开始记录js学习:~~~~分享一下你的js学习途径吧 决定学习前端之后,开始接触JavaScript 1995年,网景公司的Brendan Eich用10天完成了JavaScript的设计,他被称为J ...

  3. java的堆栈通俗理解

    java内存模型有堆内存和栈内存, 初学者可能看官方解释很模糊 堆:new 出来的对象或者数组都存放在堆中: List <String> list =new ArrayList<St ...

  4. 【One by one系列】一步步部署.net core应用

    我们的目标: CentOS系统 nginx服务器 asp.net core应用 mysql服务器 腾讯云服务器 工具准备 [Xshell]--使用windwos下的工具Xshell,原理就是使用SHH ...

  5. kafka理论

    一.消息队列,简称MQ,message queue 生产者:生存数据写到kafka,持久化到硬盘.对同一个Topic来讲,生产者通常只有‘一个’(可以多并发)数据保存时常可以配置,默认保存七天. 消费 ...

  6. TZ_16_Vue_入门案例

    1.新建一个html文件导入vue.js <script src="node_modules/vue/dist/vue.js"></script> 2.创建 ...

  7. Wamp Apache 启动失败检测方法

    一般情况下,看错误日志就可以解决.如果遇到错误日志看不到的情况,不放试试下面的方法 //无错误日志解决办法cmd命令行切换到C:\wamp\bin\apache\apache2.4.9\bin目录 输 ...

  8. jeecms系统使用介绍——jeecms中的内容、栏目、模型之间的关系

    转载:https://blog.csdn.net/dongdong9223/article/details/76578120 jeecms是一款很不错的cms产品,之前在文章<基于Java的门户 ...

  9. grpc入门2

    rpc-gateway使用(同时提供rpc和http接口) 介绍第三方库 https://github.com/grpc-ecosystem/grpc-gateway 在grpc之上加一层代理并转发, ...

  10. flexget安装

    首先新建一个文件夹给flexget下载种子 Flexgetdownloads 注意:如果先安装flexget再创建文件夹则需要手动去分配权限 然后通过勾选我要试用beta版本,来获取flexget 找 ...