Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容
© 版权声明:本文为博主原创文章,转载请注明出处
实例:
1.项目结构
2.pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.spring</groupId>
- <artifactId>Spring-AOP-API</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>Spring-AOP-API Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <properties>
- <spring.version>4.3.8.RELEASE</spring.version>
- </properties>
- <dependencies>
- <!-- junit -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- <scope>test</scope>
- </dependency>
- <!-- Spring Core -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>Spring-AOP-API</finalName>
- </build>
- </project>
3.BizLogic.java
- package org.spring.aop.api;
- public interface BizLogic {
- String save();
- String saveEx();
- }
4.BizLogicImpl.java
- package org.spring.aop.api;
- public class BizLogicImpl implements BizLogic {
- public String save() {
- System.out.println("BizLogicImpl:BizLogicImpl save.");
- return "BizLogicImpl save";
- }
- public String saveEx() {
- System.out.println("BizLogicImpl:BizLogicImpl save.");
- throw new RuntimeException();
- }
- }
5.CustomBeforeAdvice
- package org.spring.aop.api;
- import java.lang.reflect.Method;
- import org.springframework.aop.MethodBeforeAdvice;
- public class CustomBeforeAdvice implements MethodBeforeAdvice {
- public void before(Method method, Object[] args, Object target) throws Throwable {
- System.out.println("CustomBeforeAdvice:" + method.getName() + " " +
- target.getClass().getName());
- }
- }
6.CustomAfterReturnAdvice.java
- package org.spring.aop.api;
- import java.lang.reflect.Method;
- import org.springframework.aop.AfterReturningAdvice;
- public class CustomAfterReturnAdvice implements AfterReturningAdvice {
- public void afterReturning(Object returnValue, Method method, Object[] args, Object target)
- throws Throwable {
- System.out.println("CustomAfterReturnAdvice:" + method.getName() + " "
- + target.getClass().getName() + " " + returnValue);
- }
- }
7.CustomThrowsAdvice.java
- package org.spring.aop.api;
- import java.lang.reflect.Method;
- import org.springframework.aop.ThrowsAdvice;
- public class CustomThrowsAdvice implements ThrowsAdvice {
- public void afterThrowing(Exception ex) throws Throwable {
- System.out.println("CustomThrowsAdvice afterThrowing 1");
- }
- public void afterThrowing(Method method, Object[] args, Object target, Exception ex)
- throws Throwable {
- System.out.println("CustomThrowsAdvice afterThrowing 2:" + method.getName() + " "
- + target.getClass().getName());
- }
- }
8.CustomMethodInterceptor.java
- package org.spring.aop.api;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- public class CustomMethodInterceptor implements MethodInterceptor {
- public Object invoke(MethodInvocation invocation) throws Throwable {
- System.out.println("CustomMethodInterceptor 1:" + invocation.getMethod().getName()
- + " " + invocation.getStaticPart().getClass().getName());
- Object obj = invocation.proceed();
- System.out.println("CustomMethodInterceptor 2:" + obj);
- return obj;
- }
- }
9.Lockable.java
- package org.spring.aop.api.introduction;
- /**
- * 接口
- *
- */
- public interface Lockable {
- void lock();
- void unlock();
- boolean locked();
- }
10.LockMixin.java
- package org.spring.aop.api.introduction;
- import org.aopalliance.intercept.MethodInvocation;
- import org.springframework.aop.support.DelegatingIntroductionInterceptor;
- /**
- * 实现类
- *
- */
- public class LockMixin extends DelegatingIntroductionInterceptor implements Lockable {
- private static final long serialVersionUID = 1L;
- private boolean locked;
- public void lock() {
- this.locked = true;
- }
- public void unlock() {
- this.locked = false;
- }
- public boolean locked() {
- return this.locked;
- }
- /**
- * 被锁定后不能使用setter方法改变值
- */
- @Override
- public Object invoke(MethodInvocation invocation) throws Throwable {
- if(locked && invocation.getMethod().getName().indexOf("set") == 0){
- throw new RuntimeException();
- }
- return super.invoke(invocation);
- }
- }
11.LockMixinAdvisor.java
- package org.spring.aop.api.introduction;
- import org.springframework.aop.support.DefaultIntroductionAdvisor;
- /**
- * Introduction,在不改变代码的情况下,添加一个父类
- *
- */
- public class LockMixinAdvisor extends DefaultIntroductionAdvisor {
- private static final long serialVersionUID = 1L;
- public LockMixinAdvisor() {
- super(new LockMixin(), Lockable.class);
- }
- }
12.Spring-aop-api.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <!-- Before advice
- -一个简单的通知类型
- -只是进方法之前被调用,不需要MethodInvocation对象
- -前置通知可以在连接点执行之前插入自定义行为,但不能改变返回值
- -->
- <bean id="customBeforeAdvice" class="org.spring.aop.api.CustomBeforeAdvice"/>
- <!-- Throws advice
- -如果连接点抛出异常,throws advice在连接点返回后被调用
- -如果throws-advice的方法抛出异常,那么它将覆盖原有异常
- -接口org.springframework.aop.ThrowsAdvice不包含任何方法,仅仅是一个声明,实现类需要实现类似下面的方法
- -void afterThrowing([Method, args, target], ThrowableSubclass)
- -->
- <bean id="customThrowsAdvice" class="org.spring.aop.api.CustomThrowsAdvice"/>
- <!-- After Returning advice
- -后置通知必须实现org.springframework.aop.AfterReturningAdvice几口
- -可以访问返回值(但不能进行修改)、被调用的方法、方法的参数和目标
- -如果抛出异常,将会抛出拦截器链,替代返回值
- -->
- <bean id="customAfterReturnAdvice" class="org.spring.aop.api.CustomAfterReturnAdvice"/>
- <!-- Interception around advice
- -Spring的切入点模型使得切入点可以独立与advice重用,以针对不同的advice可以使用相同的切入点
- -->
- <bean id="customMethodInterceptor" class="org.spring.aop.api.CustomMethodInterceptor"/>
- <!-- Introduction advice
- -Spring把引入通知作为一种特殊的拦截通知
- -需要IntroductionAdvisor和IntroductionInterceptor
- -仅适用于类,不能和任何切入点一起使用
- -实例:如果调用lock()方法,希望所有的setter方法抛出LockedException异常。
- -代码:Lockalbe.java LockMixin.java LockMixinAdvisor.java
- -->
- <!-- Advisor API
- -Advisor是仅包含一个切入点表达式关联的单个通知的方面
- -除了introductions,advisor可以用于任何通知
- -org.springframework.aop.support.DefaultIntroductionAdvisor是最常用的advisor类,
- 它可以与MethodInterceptor,BeforeAdvice或者ThrowsAdvice一起使用
- -它可以混合在Spring同一个AOP代理的advisor和advice
- -->
- <!-- 当使用方式三时,屏蔽该bean的定义 -->
- <!-- <bean id="bizLogicImplTarget" class="org.spring.aop.api.BizLogicImpl"/> -->
- <!-- ###########################方式一:使用pointcutBean开始############################# -->
- <!-- 根据方法名匹配切入点pointcut -->
- <!-- <bean id="pointcutBean" class="org.springframework.aop.support.NameMatchMethodPointcut">
- <property name="mappedNames">成员变量,匹配的方法名集合
- <list>
- <value>sa*</value>
- </list>
- </property>
- </bean>
- <bean id="defaultAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
- <property name="advice" ref="customBeforeAdvice"/>接入点通知,若非接入点,则该通知不生效
- <property name="pointcut" ref="pointcutBean"/>接入点
- </bean> -->
- <!-- ProxyFactoryBean
- -创建Spring AOP代理的基本方法是使用org.springframework.aop.framework.ProxyFactoryBean
- -这可以完全控制切入点和通知(advice)以及它们的顺序
- -ProxyFactoryBean实现里getObject()方法创建一个AOP代理包装一个目标对象
- -使用ProxyFactoryBean或者其它IOC相关类来创建AOP代理的最重要的好处是通知和切入点也可以由IOC来管理
- -被代理类没有实现任何接口,使用CGLIB代理,否则JDK代理
- -通过设置proxyTargetClass为true,可强制使用CGLIB
- -如果目标类实现了一个(或者多个)接口,那么创建代理的类型将依赖ProxyFactoryBean的配置
- -如果ProxyFactoryBean的proxyInterfaces属性设置为一个或多个全限定接口名,基于JDK的代理将被创建
- -如果ProxyFactoryBean的proxyInterfaces属性没有被设置,但是目标类实现了一个(或者更多)接口,那么proxyInterfaces
- 将自动检测到这个目标类已经实现了至少一个接口,创建一个基于JDK的代理
- -->
- <!-- <bean id="bizLogicImpl" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target">为该类创建代理,由ProxyFactoryBean自己去判断是否实现了接口
- <ref bean="bizLogicImplTarget"/>
- </property>
- <property name="interceptorNames">执行该代理的时候执行的interceptor
- <list>
- <value>defaultAdvisor</value>
- <value>customAfterReturnAdvice</value>
- <value>customMethodInterceptor</value>
- <value>customThrowsAdvice</value>
- </list>
- </property>
- </bean> -->
- <!-- ###############################方式一结束######################################### -->
- <!-- ############################方式二:使用pointcutBean开始############################ -->
- <!-- <bean id="bizLogicImpl" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces">直接指定接口
- <value>org.spring.aop.api.BizLogic</value>
- </property>
- <property name="target">为该类创建代理,因为直接指定了接口,所以肯定是使用JDK代理
- <ref bean="bizLogicImplTarget"/>
- </property>
- <property name="interceptorNames">执行该代理的时候执行的interceptor
- <list>
- <value>customBeforeAdvice</value>
- <value>customAfterReturnAdvice</value>
- <value>customMethodInterceptor</value>
- <value>customThrowsAdvice</value>
- </list>
- </property>
- </bean> -->
- <!-- ###############################方式二结束######################################### -->
- <!-- ##############################方式三:使用匿名内部Bean############################### -->
- <bean id="bizLogicImpl" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces"><!-- 直接指定接口 -->
- <value>org.spring.aop.api.BizLogic</value>
- </property>
- <property name="target"><!-- 上面可以直接使用bean id获取bean对象,此时该bean对象没有被代理,配置的advice不会被执行 -->
- <bean class="org.spring.aop.api.BizLogicImpl"/>
- </property>
- <property name="interceptorNames"><!-- 执行该代理的时候执行的interceptor -->
- <list>
- <value>customBeforeAdvice</value>
- <value>customAfterReturnAdvice</value>
- <value>customMethodInterceptor</value>
- <value>customThrowsAdvice</value>
- </list>
- <!-- 使用global advisors
- -用*做通配,匹配所有拦截器加入通知链
- 此处只会执行customMethodInterceptor,因为只有该advice实现了Interceptor接口
- -->
- <!-- <list>
- <value>custom*</value>
- </list> -->
- </property>
- </bean>
- <!-- ###############################方式三结束######################################### -->
- <!-- ##############################方式四:简化的proxy定义############################### -->
- <!-- 简化的proxy定义
- -使用父子bean定义以及内部bean定义,可能会带来更清洁和更简洁的代理定义
- -->
- <!-- <bean id="baseProxyBean" class="org.springframework.aop.framework.ProxyFactoryBean"
- lazy-init="true" abstract="true"/>
- <bean id="bizLogicImpl" parent="baseProxyBean">
- <property name="proxyInterfaces">直接指定接口
- <value>org.spring.aop.api.BizLogic</value>
- </property>
- <property name="target">上面可以直接使用bean id获取bean对象,此时该bean对象没有被代理,配置的advice不会被执行
- <bean class="org.spring.aop.api.BizLogicImpl"/>
- </property>
- <property name="interceptorNames">执行该代理的时候执行的interceptor
- <list>
- <value>customBeforeAdvice</value>
- <value>customAfterReturnAdvice</value>
- <value>customMethodInterceptor</value>
- <value>customThrowsAdvice</value>
- </list>
- </property>
- </bean> -->
- <!-- ###############################方式四结束######################################### -->
- </beans>
13.TestBase.java
- package org.spring.aop.api.test;
- import org.junit.After;
- import org.junit.Before;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.util.StringUtils;
- public class TestBase {
- private ClassPathXmlApplicationContext context;
- private String springXmlPath;
- /**
- * 无参构造器
- */
- public TestBase() {
- }
- /**
- * 含参构造器,初始化spring配置文件路径
- *
- * @param springXmlPath
- * spring配置文件路径
- */
- public TestBase(String springXmlPath) {
- this.springXmlPath = springXmlPath;
- }
- /**
- * 初始化spring配置文件并加载到IOC容器中
- */
- @Before
- public void before() {
- if(StringUtils.isEmpty(springXmlPath)) {
- springXmlPath = "classpath:spring-*.xml";
- }
- context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
- context.start();
- }
- /**
- * 销毁IOC容器
- */
- @After
- public void after() {
- if(context != null){
- context.destroy();
- }
- }
- /**
- * 根据bean ID获取bean对象
- *
- * @param beanId
- * bean ID
- * @return
- */
- public Object getBean(String beanId) {
- return context.getBean(beanId);
- }
- }
14.TestSpringAOPAPI.java
- package org.spring.aop.api.test;
- import org.junit.Test;
- import org.spring.aop.api.BizLogic;
- public class TestSpringAOPAPI extends TestBase {
- /**
- * 通过构造器初始化spring配置文件
- */
- public TestSpringAOPAPI() {
- super("classpath:spring-aop-api.xml");
- }
- /**
- * 测试正常方法
- */
- @Test
- public void testSave() {
- BizLogic logic = (BizLogic) super.getBean("bizLogicImpl");
- logic.save();
- }
- /**
- * 测试存在异常的方法
- */
- @Test
- public void testsaveEx() {
- BizLogic logic = (BizLogic) super.getBean("bizLogicImpl");
- logic.saveEx();
- }
- }
15.效果预览
15.1 执行testSave方法
15.2 执行testsaveEx方法
说明:其他几种方式已在spring-aop-api.xml中注明,可自行测试。
参考:http://www.imooc.com/video/4597
http://www.imooc.com/video/4598
http://www.imooc.com/video/4599
Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容的更多相关文章
- Spring学习(十五)----- Spring AOP通知实例 – Advice
Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...
- spring学习 十五 spring的自动注入
一 :在 Spring 配置文件中对象名和 ref=”id” ,id 名相同使用自动注入,可以不配置<property/>,对应的注解@Autowired的作用 二: 两种配置办法 (1 ...
- Spring学习十四----------Spring AOP实例
© 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- spring boot(十五)spring boot+thymeleaf+jpa增删改查示例
快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...
- Spring 学习十五 AOP
http://www.hongyanliren.com/2014m12/22797.html 1: 通知(advice): 就是你想要的功能,也就是安全.事物.日子等.先定义好,在想用的地方用一下.包 ...
- Spring学习(十九)----- Spring的五种事务配置详解
前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...
- spring学习 十四 注解AOP 通知传递参数
我们在对切点进行增强时,不建议对切点进行任何修改,因此不加以使用@PointCut注解打在切点上,尽量只在Advice上打注解(Before,After等),如果要在通知中接受切点的参数,可以使用Jo ...
- Spring学习(十八)----- Spring AOP+AspectJ注解实例
我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...
- Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)
在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...
随机推荐
- java读取文件的基本操作
import java.io.FileInputStream; /** * 使用FileInputStream读取文件 */ public class FileRead { /** * @param ...
- js setInterval 启用&停止
以下面例子为说明: <title></title> <script src="Scripts/jquery-1.4.1-vsdoc.js" type= ...
- 利用Docker搭建本地https环境的完整步骤
利用Docker搭建本地https环境的完整步骤 这篇文章主要给大家介绍了关于如何利用Docker搭建本地https环境的完整步骤,文中通过示例代码将实现的步骤介绍的非常详细,对大家的学习或者工作具有 ...
- Educational Codeforces Round 37 A B C D E F
A. water the garden Code #include <bits/stdc++.h> #define maxn 210 using namespace std; typede ...
- 《手把手教你学C语言》学习笔记(2)---学习C语言的目标和方法
一.学习C语言的目标主要是: 熟练掌握C语言的关键字,语法规则,程序控制等: 掌握基本的数据结构,数组.链表.栈和队列等: 掌握C语言中指针和内存.数组与指针.函数与指针.变量和指针.结构体和指针.硬 ...
- jquery 中的post和get方法同步问题
解决方法: 在需要同步的js代码前修改ajax的async属性. 有两种设置方法: 1: $.ajaxSettings.async = false; 2: $.ajaxSetup({ async : ...
- cocoapods集成三方库遇到的坑
什么都不想说直接上图 这是最近在管理三方库时遇到头疼的问题,刚开始一直怀疑是cocoapods或者ruby的版本问题但是升级到最新版还是同样的错误,后来又怀疑是资源文件的问题但是在同一时间不同地点集成 ...
- Hotspot JVM下,parallel与concurrent的区别
转载于知乎 作者:Ted Mosby链接:https://www.zhihu.com/question/21535747/answer/144884632来源:知乎著作权归作者所有.商业转载请联系作者 ...
- 2018年东北农业大学春季校赛 K wyh的数列【数论/斐波那契数列大数取模/循环节】
链接:https://www.nowcoder.com/acm/contest/93/K来源:牛客网 题目描述 wyh学长特别喜欢斐波那契数列,F(0)=0,F(1)=1,F(n)=F(n-1)+F( ...
- Codeforces Round #450 (Div. 2) C. Remove Extra One【*模拟链表/一个数比前面所有数大就是个record。删掉一个数,让record的个数尽量多。】
C. Remove Extra One time limit per test 2 seconds memory limit per test 256 megabytes input standard ...