2.3、spring+aspectj

  Spring在集成了aspectj后,同时也保留了以上的切面与代理的配置方式。

  将Spring与aspectj集成与直接使用aspectj不同,不需要定义Aspectj类(它扩展了Java语法的一种新语言,还需要特定的编译器),只需要使用Aspectj切点表达式即可。

1、Spring + Aspectj(基于注解:通过Aspectj execution表达式拦截方法)

增加GreetingAspect类


  其中包含了环绕增强

  该类中的@Aspect注解,表明该类是一个Aspect,其实就是Advisor。该类无需实现任何的接口,只需定义一个方法(名称无所谓),在方法上标注Around注解即可,在注解中使用Aspectj切点表达式。方法的参数中包含一个ProceedingJoinPoint对象,他在aop中称为joinpoint(连接点),可以通过该对象获取方法的任何信息。

  切点表达式分析,execution(* com.lhx.chapter4.aop.GreetingImpl.*(..))

  execution()标示拦截方法,括号中需要定义匹配的规则

  第一个“*”表示方法的返回值是任意的

  第二个“*”表示匹配该类中的所有方法

  (..)表示方法的参数是任意的

配置Spring的xml

  proxy-target-class,默认是false,只能代理接口,默认使用JDK代理,true时,代理目标类,使用CGLib代理。

client调用

2、Spring + Aspectj(基于注解:通过Aspectj @annotation 表达式拦截方法)

为拦截指定的注解的方法,需要先自定义一个注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Tag {
}

  以上定义了一个注解,此注解可标注在方法上,在运行时生效。

Greeting的Aspect类

  使用了@annotation()表达式,只需要在括号内定义需要拦截的注解名称即可。

  直接将Tag注解定义在想要拦截的方法上即可。  

client调试

当然也需要使用上一个的xml文件

相关注解说明

  Before--前置增强

  After--后置增强

  Aroung--环绕增强

  AfterThrowing--抛出增强

  DeclareParents--引入增强

  AfterReturning--返回后增强,即finall增强,相当于finally语句,方法结束后执行,比After还要晚一些。

3、Spring + Aspectj:引入增强

定义一个引入增强类

package com.lhx.chapter4.aop.aopaspectjdeclare;

import com.lhx.chapter4.aop.springaopintroductionadvice.Apology;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component; @Aspect
@Component
public class GreetingAspectDeclare {
@DeclareParents(value = "com.lhx.chapter4.aop.GreetingImpl",defaultImpl = ApologyImpl.class)
private Apology apology;
}

  在Aspect类中定义一个需要引用增强的接口,它也就是运行时需要动态实现的接口,在这个接口上标注@DeclareParents注解,注解属性有,

    value--目标类,defaultImpl--引入接口的默认实现类。

对引入接口提供实现

package com.lhx.chapter4.aop.aopaspectjdeclare;

import com.lhx.chapter4.aop.springaopintroductionadvice.Apology;

public class ApologyImpl implements Apology {
@Override
public void saySorry(String name) {
System.out.println("Sorry " + name);
}
}

spring的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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--扫描指定包(将带有Compontent注解的类自动定义为Spring bean)-->
<context:component-scan base-package="com.lhx.chapter4.aop"></context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>

客户端校验

package com.lhx.chapter4.aop.aopaspectjdeclare;

import com.lhx.chapter4.aop.Greeting;
import com.lhx.chapter4.aop.springaopintroductionadvice.Apology;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
//获取spring content
ApplicationContext context =new ClassPathXmlApplicationContext("application-aop-declare.xml");
//从Content中根据id获取Bean对象,其实就是一个代理
Greeting greetingProxy = (Greeting) context.getBean("greetingImpl");
greetingProxy.sayHelloNoPreSub("muzixu ");
Apology apology = (Apology) greetingProxy;
//将目标类强制向上转型为Apology接口类型【引入增强的特性,即“动态接口实现”功能】
apology.saySorry("Jack"); }
}

4、Spring + Aspectj:基于配置

  除了使用aspectj注解来定义切面外,Spring AOP也提供了基于配置的方式来定义切面类

配置

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="greetingImpl" class="com.lhx.chapter4.aop.GreetingImpl"></bean>
<bean id="greetingAspect" class="com.lhx.chapter4.aop.aopaspectj.GreetingAspect"></bean>
<aop:config>
<aop:aspect ref="greetingAspect">
<aop:around method="around" pointcut="execution(* com.lhx.chapter4.aop.GreetingImpl.*(..))"/>
</aop:aspect>
</aop:config>
</beans>

client使用  

package com.lhx.chapter4.aop.aopaspectjconfig;

import com.lhx.chapter4.aop.Greeting;
import com.lhx.chapter4.aop.springaopintroductionadvice.Apology;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client {
public static void main(String[] args) {
//获取spring content
ApplicationContext context =new ClassPathXmlApplicationContext("application-aop-config.xml");
//从Content中根据id获取Bean对象,其实就是一个代理
Greeting greetingProxy = (Greeting) context.getBean("greetingImpl");
greetingProxy.sayHelloNoPreSub("muzixu "); }
}

三、aop总结

1、思维导图

  

2、各类增强类型对应的解决方案

3、spring aop的URL类图框架

006-搭建框架-实现AOP机制【三】AOP技术的更多相关文章

  1. Castle框架中的IOC和AOP机制

    反转控制(IOC)和面向切面编程(AOP)技术作为当前比较流行的技术,其优势已受到广泛关注,但是这两项新技术在实际项目上的应用研究却很落后,而且在.NET平台下实现这两项技术没有形成可以广泛套用的框架 ...

  2. Java基础---Java---基础加强---类加载器、委托机制、AOP、 动态代理技术、让动态生成的类成为目标类的代理、实现Spring可配置的AOP框架

    类加载器 Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader,AppClassLoader 类加载器也是Jav ...

  3. spring框架学习(三)——AOP( 面向切面编程)

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  4. Spring框架的核心功能之AOP技术

     技术分析之Spring框架的核心功能之AOP技术 AOP的概述        1. 什么是AOP的技术?        * 在软件业,AOP为Aspect Oriented Programming的 ...

  5. Spring(三)——AOP

    AOP全名为Aspect-Oriented Programming,意思是面向横切面编程,前边我们有过介绍   面向横切面编程AOP的理解 ,我们通过这种编程思想很容易的扩展我们的应用程序. 一,如何 ...

  6. Unity 游戏框架搭建 (五) 简易消息机制

    什么是消息机制? 23333333,让我先笑一会. 为什么用消息机制?   三个字,解!!!!耦!!!!合!!!!. 我的框架中的消息机制用例: 1.接收者 ``` using UnityEngine ...

  7. Spring 框架的核心功能之AOP技术

    1. AOP 的概述 AOP, Aspect Oriented Programming, 面向切面编程; 通过预编译方式和运行期动态代理实现程序功能的统一维护的技术; AOP 采取横向抽取机制,取代了 ...

  8. spring框架学习(六)AOP

    AOP(Aspect-OrientedProgramming)面向方面编程,与OOP完全不同,使用AOP编程系统被分为方面或关注点,而不是OOP中的对象. AOP的引入 在OOP面向对象的使用中,无可 ...

  9. spring框架学习(四)AOP思想

    什么是AOP 为什么需要AOP 从Spring的角度看,AOP最大的用途就在于提供了事务管理的能力.事务管理就是一个关注点,你的正事就是去访问数据库,而你不想管事务(太烦),所以,Spring在你访问 ...

随机推荐

  1. Error: [vuex] vuex requires a Promise polyfill in this browser. 与 babel-polyfill 的问题

    Error: [vuex] vuex requires a Promise polyfill in this browser. 与 babel-polyfill 的问题 采用最笨重的解决方案就是npm ...

  2. Atitit.attilax的 case list 项目经验 案例列表

    Atitit.attilax的 case list 项目经验 案例列表 1. Atian inputmethod 输入法3 2. Ati desktop engine桌面引擎3 3. Acc资金账户系 ...

  3. 谱聚类python实践

    聚类后: # -*- coding: utf-8 -*-"""Created on 09 05 2017 @author: similarface"" ...

  4. Unity3D - LINEAR INTERPOLATION

    原文地址:http://unity3d.com/learn/tutorials/modules/beginner/scripting/linear-interpolation 水平有限,翻译粗略,欢迎 ...

  5. CronTrigger中cron表达式使用

    1.定时任务,当执行是具体时间的时候,不会立即执行,而是到指定时间执行. 2.实现Job接口时候,类中要有无参的public构造方法. 3.表达式中共用七个字段,按顺序是秒.分.时.日.月.周.年,默 ...

  6. springboot开启事务管理

    spring中开启事务管理需要在xml配置文件中配置,springboot中采取java config的配置方式. 核心是@EnableTransactionManager注解,该注解即为开启事务管理 ...

  7. 解决PHP编译cURL的reinstall the libcurl问题

    今天正好要用到PHP的curl功能,发现服务器上的PHP并没有配置curl,进而查询PHP官方文档,得知编译PHP时需要带上 –with-curl参数,才能把curl模块编译进去.我现在PHP已经编译 ...

  8. Python 爬虫实战2 百度贴吧帖子

    爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 对百度贴吧的任意帖子进行抓取 指定是否只抓取楼主发帖内容 将抓取到的内容分析并保存到文件 1.URL格式的确定 首先, ...

  9. OC对象给分类加入属性

    OC对象中不能给分类加入属性.可是在实际开发中.常常为了更好的性能须要给分类加入属性,那么 加入的属性不能有默认的成员变量.须要我们自己实现set和get方法,要用到执行时 例如以下: #import ...

  10. iOS 数组的去重(普通的无序的去重和排序好的去重)

    本文转载至 http://blog.csdn.net/zhaopenghhhhhh/article/details/24972645 有时需要将NSArray中去除重复的元素,而存在NSArray中的 ...