AspectJ对AOP的实现
一:你应该明白的知识
1.对于AOP这种编程思想,很多框架都进行了实现。Spring就是其中之一,可以完成面向切面编程。然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且还支持注解式开发。所以,Spring又将AspectJ对于AOP的实现也引入到了自己的框架中。在Spring中使用AOP开发时,一般使用AspectJ的实现方式。
2.Spring的经典AOP配置方案
01.使用的是Aspectj第三方框架,实现了AOP思想
02.注解配置的AOP
03.纯POJO <aop:config>
3..切入点表达式
execution(【modifiers-pattern?】 访问修饰符
ret-type-pattern 返回值类型
【declaring-type-pattern?】 全限定性类名
name-pattern(param-pattern) 方法名(参数名)
【throws-pattern?】) 抛出异常类型
切入点表达式要匹配的对象就是目标方法的方法名。所以,execution表达式中明显就是方法的签名。注意:表达式中加[]的部分表示可省略部分,各部分间用空格分开。在其中可以使用以下符号:
符号 意义
* 0至多个任意字符
.. 用在方法参数中,表示任意多个参数
用在包名后,表示当前包及其子包路径
+ 用在类名后,表示当前类及其子类
用在接口后,表示当前接口及其实现类
案例:
execution(public * *(..)) 指定切入点为:任意公共方法
execution(* set*(..)) 指定切入点为:任何一个以"set"开始的方法
二.使用的是Aspectj第三方框架注解配置的AOP增强
源码介绍:
1.ISomeService.java
package entity;
//业务接口
public interface ISomeService {
//1.1 执行事务
public void doTransaction();
//1.2 书写日志
public String doLog();
}
2.SomeServiceImpl.java
package entity;
//接口的实现类
public class SomeServiceImpl implements ISomeService {
//实现接口中的方法
@Override
public void doTransaction() {
System.out.println("----开启事务----");
} @Override
public String doLog() {
//int i=5/0;//制造一个错误,用于测试异常增强
System.out.println("---书写日志-----");
return "abc";
} }
3.MyAspect.java(使用的是Aspectj第三方框架注解配置的AOP增强)
package aop;
//aspectj注解实现aop增强
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; //该类为切面
@Aspect
public class MyAspect { // 前置通知
@Before(value = "execution(public * *..ISomeService.doLog(..))")
public void myBefore() {
System.out.println("这是前置增强");
} // 后置通知
@AfterReturning(value = "execution(public * *..ISomeService.doLog(..))")
public void myAfterReturning() {
System.out.println("这是后置增强");
} // 环绕增强
@Around(value = "execution(public * *..ISomeService.doLog(..))")
public void myAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("这是环绕前置增强"); pjp.proceed(); System.out.println("这是环绕后置增强");
} // 异常增强
@AfterThrowing(value = "execution(public * *..ISomeService.doLog(..))")
public void myAfterThrowing() {
System.out.println("这是异常增强");
} // 最终增强
@After(value = "execution(public * *..ISomeService.doLog(..))")
public void myAfter() {
System.out.println("这是最终增强");
}
}
4.applicationContext.xml(Spring的配置文件)
<?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="someService" class="entity.SomeServiceImpl"></bean> <!-- 切面: -->
<bean id="myAspect" class="aop.MyAspect"></bean> <!-- 自动代理 -->
<aop:aspectj-autoproxy/>
</beans>
5.MyTest.java
package test;
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.ISomeService; public class MyTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ISomeService biz=(ISomeService)ctx.getBean("someService");
biz.doLog();
biz.doTransaction();
System.out.println("success!");
} }
6.log4j.properties(日志配置文件)
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=info, stdout
7.当然,同志们别忘了引入jar包啊!
8.其测试结果展示:
三:使用的是Aspectj第三方框架 纯POJO 基于Schema配置 (<aop:config>)
源码介绍:
1.MyAspect.java
package aop; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class MyAspect {
// 前置通知
public void myBefore() {
System.out.println("这是前置增强");
} public void before(JoinPoint jp) {
System.out.println("前置通知方法before() jp = " + jp);
} // 后置通知
public void myAfterReturning() {
System.out.println("这是后置增强");
} // 带参数
public void afterReturing(String result) {
System.out.println("后置通知方法 result = " + result);
} // 环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕通知方法,目标方法执行之前");
// 执行目标方法
Object result = pjp.proceed();
System.out.println("环绕通知方法,目标方法执行之后");
return ((String) result).toUpperCase();
} // 异常通知
public void afterThrowing() {
System.out.println("异常通知方法");
} public void afterThrowing(Exception ex) {
System.out.println("异常通知方法 ex = " + ex.getMessage());
} // 最终通知
public void after() {
System.out.println("最终通知方法");
}
}
2.applicationContext.xml(Spring的配置文件)
<?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="someService" class="entity.SomeServiceImpl"></bean> <!-- 切面: -->
<bean id="myAspect" class="aop.MyAspect"></bean> <aop:config>
<!--expression:切入点表达式 -->
<aop:pointcut expression="execution(public * *..ISomeService.doLog(..))"
id="beforePointcut" />
<aop:aspect ref="myAspect"><!-- ref:指定切面 -->
<!-- method:指定切面类中的方法; pointcut-ref:指定定义的切点 -->
<!-- 前置增强 -->
<aop:before method="myBefore" pointcut-ref="beforePointcut" />
<!-- 前置增强 带参 -->
<aop:before method="before(org.aspectj.lang.JoinPoint)"
pointcut-ref="beforePointcut" />
<!-- 后置增强 -->
<aop:after-returning method="myAfterReturning"
pointcut-ref="beforePointcut" />
<!-- 后置增强 带参 -->
<aop:after-returning method="afterReturing(java.lang.String)"
pointcut-ref="beforePointcut" returning="result" />
<!-- 环绕增强 -->
<aop:around method="around" pointcut-ref="beforePointcut" />
<!-- 异常增强 -->
<aop:after-throwing method="afterThrowing"
pointcut-ref="beforePointcut" />
<!-- 异常增强 带参 -->
<aop:after-throwing method="afterThrowing(java.lang.Exception)"
pointcut-ref="beforePointcut" throwing="ex" />
<!-- 最终增强 -->
<aop:after method="after" pointcut-ref="beforePointcut" />
</aop:aspect>
</aop:config>
</beans>
其中,其他的类和文件和上面的例子中完全相同。这里则不做介绍。
3.其测试结果展示:
AspectJ对AOP的实现的更多相关文章
- Spring @AspectJ 实现AOP 入门例子(转)
AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): package com.samter.common ...
- Spring框架(6)---AspectJ实现AOP
AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...
- 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存
使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存 今天要介绍的是Simple-Spring-Memcached,它封装了对MemCached的调用,使MemCa ...
- 8 -- 深入使用Spring -- 4...2 使用AspectJ实现AOP
8.4.2 使用AspectJ实现AOP AspectJ是一个基于Java语言的AOP框架.Spring 4.0 的AOP对AspectJ很好的集成. AspectJ是Java 语言的一个AOP实现, ...
- spring3: schema的aop与Aspectj的aop的区别
schema的aop如下: 接口: package chapter6.service; public interface IHelloAroundService { public void sayAr ...
- Spring整合AspectJ的AOP
学而时习之,不亦说乎! --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...
- 利用基于@AspectJ的AOP实现权限控制
一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...
- (转)Spring使用AspectJ进行AOP的开发:注解方式
http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...
- Spring 基于 AspectJ 的 AOP 开发
Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...
随机推荐
- ligerUI Tree 实例 代码
http://www.oschina.net/code/snippet_1762525_47819#68813
- SSIS Send Mail
在SSIS中Send Mail的方法主要有三种,使用Send Mail Task,使用Script Task和使用存储过程msdb.dbo.sp_send_dbmail. 一,使用Send Mail ...
- AIX碎碎念
1. 查看系统版本 oslevel 2. 查看系统内存 svmon -G (注意,size的单位为4k) 3. 查看机器型号 uname -Mu 4. 查看机器硬件信息,如cpu,内存等 prtcon ...
- Java多线程系列--“JUC集合”01之 框架
概要 之前,在"Java 集合系列目录(Category)"中,讲解了Java集合包中的各个类.接下来,将展开对JUC包中的集合进行学习.在学习之前,先温习一下"Java ...
- Java多线程系列--“JUC集合”02之 CopyOnWriteArrayList
概要 本章是"JUC系列"的CopyOnWriteArrayList篇.接下来,会先对CopyOnWriteArrayList进行基本介绍,然后再说明它的原理,接着通过代码去分析, ...
- 使用Unity3D Asset Server进行联合开发和版本控制
前言:感觉在功能方面,其实Github更加强大易用,但是鉴于网络延迟问题,学一下AssetServer也是不错的.关于Asset Server的搭建步骤,其实官网论坛上已经有了解释得比较详细明了,在这 ...
- 云计算之路-阿里云上:Web服务器遭遇奇怪的“黑色30秒”问题
今天下午访问高峰的时候,主站的Web服务器出现奇怪的问题,开始是2台8核8G的云服务器(ECS),后来又加了1台8核8G的云服务器,问题依旧. 而且3台服务器特地使用了不同的配置:1台是禁用了虚拟内存 ...
- Android进程间通信之socket通信
用Java中的socket编程. 通过socket实现两个应用之间的通信,可以接收和发送数据,同时将接收到的数据显示在activity界面上. Server端: ServerLastly.java p ...
- 学习RxJS:Cycle.js
原文地址:http://www.moye.me/2016/06/16/learning_rxjs_part_two_cycle-js/ 是什么 Cycle.js 是一个极简的JavaScript框架( ...
- Azure ARM (4) 开始创建ARM Resource Group并创建存储账户
<Windows Azure Platform 系列文章目录> 好了,接下来我们开始创建Azure Resource Group. 1.我们先登录Azure New Portal,地址是: ...