一:你应该明白的知识

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的实现的更多相关文章

  1. Spring @AspectJ 实现AOP 入门例子(转)

    AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): package com.samter.common ...

  2. Spring框架(6)---AspectJ实现AOP

    AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...

  3. 使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存

    使用java5的注解和Sping/AspectJ的AOP 来实现Memcached的缓存 今天要介绍的是Simple-Spring-Memcached,它封装了对MemCached的调用,使MemCa ...

  4. 8 -- 深入使用Spring -- 4...2 使用AspectJ实现AOP

    8.4.2 使用AspectJ实现AOP AspectJ是一个基于Java语言的AOP框架.Spring 4.0 的AOP对AspectJ很好的集成. AspectJ是Java 语言的一个AOP实现, ...

  5. spring3: schema的aop与Aspectj的aop的区别

    schema的aop如下: 接口: package chapter6.service; public interface IHelloAroundService { public void sayAr ...

  6. Spring整合AspectJ的AOP

    学而时习之,不亦说乎!                              --<论语> 看这一篇之前最好先看前面关于AOP的两篇. http://www.cnblogs.com/z ...

  7. 利用基于@AspectJ的AOP实现权限控制

    一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...

  8. (转)Spring使用AspectJ进行AOP的开发:注解方式

    http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...

  9. Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...

随机推荐

  1. Atitti knn实现的具体四个距离算法 欧氏距离、余弦距离、汉明距离、曼哈顿距离

    Atitti knn实现的具体四个距离算法  欧氏距离.余弦距离.汉明距离.曼哈顿距离 1. Knn算法实质就是相似度的关系1 1.1. 文本相似度计算在信息检索.数据挖掘.机器翻译.文档复制检测等领 ...

  2. Linux初学 - head,tail,grep,sed,yum,find

    head 查看文件头部 -n 指定查看行数 默认10行 tail 查看文件尾部 n 指定查看行数 默认10行 Grep 命令 用法大全 . 参数: -I :忽略大小写 -c :打印匹配的行数 -l : ...

  3. Findbugs介绍及使用方法

    1.   简介 Findbugs 是一个静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题.利用这个工具,就可以在不实际运行程序的情况对软件进行分析.它可以帮助改 ...

  4. javascript中function 函数递归的陷阱问题

    //看下这个递归方法,最后输出的值function fn(i){ i++; if(i<10){ fn(i); } else{ return i; } } var result = fn(0); ...

  5. 初次使用IDEA的相关技巧

    前言:由于初次使用IDEA,所以很多配置都不是非常熟悉,经过一下午慢慢熟悉和同事的帮助,终于有所斩获,现在我把这个总结写出来,希望能够帮助初次使用的java工程师. 1:下载和安装 下载地址:http ...

  6. Android开发之广播

    广播是Android开发中的一个重要的功能,在Android里面有各式各样的广播,比如:电池的状态变化.信号的强弱状态.电话的接听和短信的接收等等,现在给大家简单介绍一下系统发送.监听这些广播的机制. ...

  7. NSIS使用教程(安装包制作安装文件教程,如何封装打包文件) 中文版

    nsis中文版(Nullsoft Scriptable Install System)是一个专业的开源的可以用来封闭Windows程序的实用工具,是一个开源的 Windows 系统下安装程序制作程序. ...

  8. 微信内置浏览器WebApp开发,踩坑 · Issue #31 · maxzhang/maxzhang.github.com · GitHub

    最近花6天时间完成了一个七夕的小活动,是一个简单的WebApp.由于我前期对面向微信的Web开发评估不足,导致开发过程十分艰难.写这篇文章总结下,惊醒自己未来不要再犯这样的错误. 问题: 1. 有些比 ...

  9. JAVA 设计模式 访问者模式

    用途 访问者模式 (Visitor) 表示一个作用于某对象结构中的各元素的操作. 它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 访问者模式是一种行为型模式. 用途

  10. 你必须知道ASP.NET知识------从IIS到httpmodule(第一篇)

    一.写在前面 最近有时间,顺便将这系列洗完,接着上文:IIS各个版本知识总结 这篇文章原本计划写到HttpHandler为止,但限于篇幅就写到httpmodule 本文有不足之处,求指正,希望我能将它 ...