课程链接:

1    解析

1.1  类的方式实现各种通知需要实现的接口

1.2  创建Spring aop代理的优点及方法

1.3  代理控制切入点和通知的顺序的代码实现(具体完全实现,见代码2.1)

1.4  代理方式选择

2    代码演练

2.1  类的方式实现各种通知

2.2  类的方式实现各种通知(和2.1对比)

1    解析

1.1  类的方式实现各种通知需要实现的接口

前置通知:MethodBeforeAdvice

后置通知:AfterReturningAdvice

异常通知:ThrowsAdvice

环绕通知:MethodInterceptor

1.2  创建Spring aop代理的优点及方法

优点:可以完全控制切入点和通知(advice)以及他们的顺序

方法:基本方法是使用org.springframework.aop.framework.ProxyFactoryBean

1.3  代理控制切入点和通知的顺序的代码实现(具体完全实现,见代码2.1)

<!-- 从这里开始,引入多个通知   --><!-- 引入的业务类不是ProxyFactoryBean ,而是getObject返回的,即 target==》bizLogicImplTarget -->

<bean id="bizLogicImpl" class="org.springframework.aop.framework.ProxyFactoryBean">

<!--     具体业务类 -->

    <property name="target">

        <ref bean="bizLogicImplTarget"/>

    </property>

    <!--     list通知不是按照顺序执行的,而是按照通知的先后顺序执行的,先执行前置通知,然后执行环绕通知,最后执行后置通知 -->

    <property name="interceptorNames">

        <list>

            <value>defaultAdvisor</value>

<value>moocAfterReturningAdvice</value> <value>moocMethodInterceptor</value> <value>moocThrowsAdvice</value> </list> </property> </bean>
<!--     使用前置通知,切入点为pointcutBean -->
<bean id="defaultAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">

    <property name="advice" ref="moocBeforeAdvice"></property>

    <property name="pointcut" ref="pointcutBean"></property>

</bean>
 

1.4  代理方式选择

1.4.1  使用ProxyFactoryBean或者其它loC相关类来创建AOP代理的最重要好处是通知和切入点也可以由loC来管理

1.4.2  被代理类没有实现任何接口,使用CGLIB代理,否则JDK代理

1.4.3  通过设置proxyTargetClass为true,可强制使用CGLIB

1.4.4  如果目标类实现了一个(或者多个)接口,那么创建代理的类型将依赖ProxyFactoryBean的配置

1.4.5  如果ProxyFactoryBean的proxyInterfaces属性被设置为一个或者多个全限定接口名,基于JDK的代理将被创建b

1.4.6  如果ProxyFactoryBean的proxyInterfaces属性没有被设置,但是目标类实现了一个(或者更多)接口,那么ProxyFactoryBean将自动检测到这个目标类已经实现了至少一个接口,创建一个基于JDK的代理。

2    代码演练

2.1  类的方式实现各种通知

测试类:

package com.imooc.test.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.aop.api.BizLogic;
import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPAPI extends UnitTestBase{ public TestAOPAPI() {
super("classpath:spring-aop-api.xml");
// TODO Auto-generated constructor stub
} @Test
public void testSave(){
BizLogic bLogic = super.getbean("bizLogicImpl"
);
bLogic.save();
}
}

配置文件:(可以细细看这篇详细内容)

<?xml version="1.0" encoding="UTF-8"?>
<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: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-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
> <bean id="moocBeforeAdvice" class="com.imooc.aop.api.MoocBeforeAdvice"></bean> <bean id="moocAfterReturningAdvice" class="com.imooc.aop.api.MoocAfterReturningAdvice"></bean> <bean id="moocMethodInterceptor" class="com.imooc.aop.api.MoocMethodInterceptor"></bean> <bean id="moocThrowsAdvice" class="com.imooc.aop.api.MoocThrowsAdvice"></bean> <bean id="bizLogicImplTarget" class="com.imooc.aop.api.BizLogicImpl"></bean>

<!--  执行所有sa打头的方法,都会引入切面 --><!-- 实现之一:NameMatchMethodPointcut,根据方法名字进行匹配 ,自带方法 -->

<!-- 更加灵活的配置切入点 -->

 <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="moocBeforeAdvice"></property>

    <property name="pointcut" ref="pointcutBean"></property>

</bean>

<!-- 从这里开始,引入多个通知   --><!-- 引入的业务类不是ProxyFactoryBean ,而是getObject返回的,即 target==》bizLogicImplTarget -->

<bean id="bizLogicImpl" class="org.springframework.aop.framework.ProxyFactoryBean">

<!--     具体业务类 -->

    <property name="target">

        <ref bean="bizLogicImplTarget"/>

    </property>

    <!--     list通知不是按照顺序执行的,而是按照通知的先后顺序执行的,先执行前置通知,然后执行环绕通知,最后执行后置通知 -->

    <property name="interceptorNames">

        <list>

            <value>defaultAdvisor</value>

            <value>moocAfterReturningAdvice</value>

            <value>moocMethodInterceptor</value>

            <value>moocThrowsAdvice</value>

        </list>

    </property>

</bean>

</beans>

实体类:

package com.imooc.aop.api;

public class BizLogicImpl implements BizLogic{

    @Override
public String save() {
System.out.println("syso save");
return "BizLogicImpl save";
} }

接口类:

package com.imooc.aop.api;

public interface BizLogic {
String save();
}

前置通知:

package com.imooc.aop.api;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MoocBeforeAdvice implements MethodBeforeAdvice{

    /**
* 实现MethodBeforeAdvice接口,可以在连接点之前执行
*
*
* 作用是什么?
* 将前一章中的xml配置方式改为了实现接口的实现方式 具体细节和使用参考开发文档
*
*/
@Override
public void before(Method method, Object[] aobj, Object obj)
throws Throwable {
System.out.println("MoocBeforeAdvice:"+method.getName()+" "+
obj.getClass().getName());
} }

后置通知:

package com.imooc.aop.api;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

/**
* 实现AfterReturningAdvice接口,可以在连接点之后执行
* @author weijingli
*
*/
public class MoocAfterReturningAdvice implements AfterReturningAdvice{ /**
* obj 作为输出
*/
@Override
public void afterReturning(Object obj, Method method, Object[] aobj,
Object obj1) throws Throwable {
System.out.println("MoocAfterReturningAdvice:"+method.getName()+
" 类的名称:"+obj1.getClass().getName()+" "+obj);
} }

环绕通知:

package com.imooc.aop.api;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; /**
* 引入环绕通知
*
* 实现 MethodInterceptor 接口
* @author weijingli
*
*/
public class MoocMethodInterceptor implements MethodInterceptor{ @Override
public Object invoke(MethodInvocation methodinvocation) throws Throwable { //实现前置通知的方法
//得到方法 得到类
System.out.println("MoocMethodInterceptor1:"+methodinvocation.getMethod().getName()+" "
+methodinvocation.getStaticPart().getClass().getName()); Object obj = methodinvocation.proceed(); //实现后置通知方法
System.out.println("MoocMethodInterceptor2:"+obj); return obj;
} }

异常通知:

package com.imooc.aop.api;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

/**
* 引入spring aop jar包 ,实现 ThrowsAdvice 接口
* 实现异常通知
*
* 可以一参,可以多参
* @author weijingli
*
*/
public class MoocThrowsAdvice implements ThrowsAdvice{
public void afterThrowing(Method method,Object[] args,Object target,Exception ex) throws Throwable{
System.out.println("MoocThrowAdvice afterThrowiing2:"+method.getName()+" "
+target.getClass().getName());
}
}

打印日志:

四月 21, 2019 6:16:29 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Apr 21 18:16:28 CST 2019]; root of context hierarchy
四月 21, 2019 6:16:29 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aop-api.xml]
MoocBeforeAdvice:save com.imooc.aop.api.BizLogicImpl
MoocMethodInterceptor1:save java.lang.reflect.Method

四月 21, 2019 6:16:30 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5abce07: startup date [Sun Apr 21 18:16:28 CST 2019]; root of context hierarchy
syso save
MoocMethodInterceptor2:BizLogicImpl save
MoocAfterReturningAdvice:save 类的名称:com.imooc.aop.api.BizLogicImpl BizLogicImpl save

2.2  类的方式实现各种通知(和2.1对比)sa方法不执行

测试类:

package com.imooc.test.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.aop.api.BizLogic;
import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPAPI extends UnitTestBase{ public TestAOPAPI() {
super("classpath:spring-aop-api.xml");
// TODO Auto-generated constructor stub
} @Test
public void testSave(){
BizLogic bLogic = super.getbean("bizLogicImpl");
bLogic.sove();
} }

实现类:

package com.imooc.aop.api;

public class BizLogicImpl implements BizLogic{

    @Override
public String sove() {
System.out.println("syso save");
return "BizLogicImpl save";
}
}

接口类:

package com.imooc.aop.api;

public interface BizLogic {
String sove();
}

打印日志:

四月 22, 2019 9:49:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7fe219e7: startup date [Mon Apr 22 21:49:02 CST 2019]; root of context hierarchy
四月 22, 2019 9:49:03 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-aop-api.xml]
MoocMethodInterceptor1:sove java.lang.reflect.Method
四月 22, 2019 9:49:05 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7fe219e7: startup date [Mon Apr 22 21:49:02 CST 2019]; root of context hierarchy
syso save
MoocMethodInterceptor2:BizLogicImpl save
MoocAfterReturningAdvice:sove 类的名称:com.imooc.aop.api.BizLogicImpl BizLogicImpl save

Spring课程 Spring入门篇 6-2 ProxyFactoryBean及相关内容(上)的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring课程 Spring入门篇 2-1 IOC和bean容器

    课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...

  4. Spring课程 Spring入门篇 3-3 Spring bean装配(上)之aware接口

    课程链接: 本节主要介绍了以下内容: 1 aware介绍 2 代码演练 3 课程总结 1 aware介绍 1.1 为什么要使用aware? 在java类中,可以方便的获取xml配置文件中的bean的各 ...

  5. Spring课程 Spring入门篇 6-3 ProxyFactoryBean及相关内容(下)

    1 解析 1.1 使用global advisors demo 1.2 jdk代理和cglib代理的选择 1.3 如何强制使用CGLIB实现AOP? 1.4 JDK动态代理和CGLIB字节码生成的区别 ...

  6. Spring课程 Spring入门篇 7-2 Advice定义及实例

    1 解析 1.1 通知:after和afterreturning的区别 1.2 @RunWith 是什么? 2 代码演练 2.1 注解方式配置通知的两种方式 2.2 异常通知 2.3 非异常通知 1 ...

  7. Spring课程 Spring入门篇 5-2 配置切面aspect

    本节主要讲了在xml中配置切面的demo 1 解析 1.1 配置切面xml 1.2 配置切面xml 1.3 问:什么是动态代理? 2 代码演练 2.1 配置切面xml 1 解析 1.1 配置切面xml ...

  8. Spring课程 Spring入门篇 4-9 Spring bean装配之对jsr支持的说明

    1 解析 1.1 疑问:2.2去掉@resource注解,为什么不能赋值?不是有set方法了吗? 1.2 @resource注解版本支持 1.3 没有显式指定@resource的那么,默认名称从何获得 ...

  9. Spring课程 Spring入门篇 4-8 Spring bean装配之基于java的容器注解说明--基于泛型的自动装配

    1 解析 1.1 什么是泛型? 1.2 泛型有什么作用? 1.3 泛型装配样式? 2 代码演练 2.1 泛型应用 1 解析 1.1 什么是泛型? Java泛型设计原则:只要在编译时期没有出现警告,那么 ...

随机推荐

  1. composer手动安装到windows

    1.配置系统变量 Path 计算机->高级系统设置->环境变量->找到系统变量Path  双击 加入  ;php根目录地址:php中ext地址    如 :“;D:\phpStudy ...

  2. 8,Phaser__并发且多阶段任务

    使用场景 考选武状元 10 个 武生 参加考试 ,第一个关 靠耐力, 坚持最久的5个人进入第二关, 第二关考 力气,力气最大的 3个人进入第二关,第三关考兵法,兵法最好的当选武状元

  3. shell的算术运算

    变量的数值计算方法大致有双括号 (()), expr,  bc, $[ ] 例子1 注意:2**3表示2的3次方,a++表示先输出a自身的值,然后进行++的运算: --a表示先进行--的运算,然后再输 ...

  4. ibatis遍历数组:ParameterObject or property was not a Collection, Array or Iterator.

    这个问题在使用ibatis的<iterate></iterate>时出现的,很简单,但是蛋疼了很久,记下来 首先从错误提示看,明显意思是你给出ibatis的参数不对路,人家不认 ...

  5. IOS面试题(一)

    第一次写博客,我在这里先给大家分享一些iOS中常见的面试题吧! 1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? 答: ...

  6. Flink学习笔记:Operators之CoGroup及Join操作

    本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...

  7. springMVC中一些功能

    1.controller的生命周期 spring框架默认为单例模式,会使数据之间的传递互相影响,而springMVC给我们提供了request与session两个,request每次请求就会产生一个单 ...

  8. java多线程-cas及atomic

    大纲: cas atomic 一.cas cas:compareAndSwap,一种乐观锁. cas思想:cas需要三个值,v是内存值,e是期望值,n是要修改的值.当内存中的值v等于预期值e(说明内存 ...

  9. Python学习 day14

    一.生成器函数进阶 1.最后一个yield后的代码 先看示例: def generator(): print(123) yield 'a' print(456) yield 'b' print(789 ...

  10. Robot Framework常用关键字介绍

    常用关键字介绍 在学习一门编程语言的时候,大多教材都是从打印“hello world”开始.我们可以像编程语言一样来学习 Robot Framework.虽然通过 RIDE 提供“填表”一样的写测试用 ...