Spring 中基于 AOP 的 @AspectJ
Spring 中基于 AOP 的 @AspectJ
@AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格。
通过在你的基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的。
<aop:aspectj-autoproxy/>
声明一个 aspect
Aspects 类和其他任何正常的 bean 一样,除了它们将会用 @AspectJ 注释之外,它和其他类一样可能有方法和字段,如下所示:
package org.xyz;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AspectModule {
}
它们将在 XML 中按照如下进行配置,就和其他任何 bean 一样:
<bean id="myAspect" class="org.xyz.AspectModule">
<!-- configure properties of aspect here as normal -->
</bean>
声明一个切入点
一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。在处理基于配置的 XML 架构时,切入点的声明有两个部分:
一个切入点表达式决定了我们感兴趣的哪个方法会真正被执行。
一个切入点标签包含一个名称和任意数量的参数。方法的真正内容是不相干的,并且实际上它应该是空的。
下面的示例中定义了一个名为 ‘businessService’ 的切入点,该切入点将与 com.xyz.myapp.service 包下的类中可用的每一个方法相匹配:
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.xyz.myapp.service.*.*(..))") // expression
private void businessService() {} // signature
下面的示例中定义了一个名为 ‘getname’ 的切入点,该切入点将与 com.tutorialspoint 包下的 Student 类中的 getName() 方法相匹配:
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.tutorialspoint.Student.getName(..))")
private void getname() {}
声明建议
你可以使用 @{ADVICE-NAME} 注释声明五个建议中的任意一个,如下所示。这假设你已经定义了一个切入点标签方法 businessService():
@Before("businessService()")
public void doBeforeTask(){
...
}
@After("businessService()")
public void doAfterTask(){
...
}
@AfterReturning(pointcut = "businessService()", returning="retVal")
public void doAfterReturnningTask(Object retVal){
// you can intercept retVal here.
...
}
@AfterThrowing(pointcut = "businessService()", throwing="ex")
public void doAfterThrowingTask(Exception ex){
// you can intercept thrown exception here.
...
}
@Around("businessService()")
public void doAroundTask(){
...
}
基于 AOP 的 @AspectJ 示例
新建Spring项目
在项目中添加 Spring AOP 指定的库文件 aspectjrt.jar, aspectjweaver.jar 和 aspectj.jar。
保证aspectjweaver.jar的版本在1.8以上,否则会报错。
创建 Java 类 Logging, Student 和 MainApp
这里是 Logging.java 文件的内容。这实际上是 aspect 模块的一个示例,它定义了在各个点调用的方法。
package hello;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
//import org.springframework.aop.aspectj.AspectJAfterThrowingAdvice;
@Aspect
public class Logging {
/** Following is the definition for a pointcut to select
* all the methods available. So advice will be called
* for all the methods.
*/
@Pointcut("execution(* hello.*.*(..))")
private void selectAll(){}
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
@Before("selectAll()")
public void beforeAdvice(){
System.out.println("Going to setup student profile.");
}
/**
* This is the method which I would like to execute
* after a selected method execution.
*/
@After("selectAll()")
public void afterAdvice(){
System.out.println("Student profile has been setup");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
@AfterReturning(pointcut = "selectAll()", returning = "retVal")
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:"+retVal.toString());
}
/**
* This is the method which I would like to execute
* if there is an exception raised.
*/
@AfterThrowing(pointcut = "selectAll()", throwing = "ex")
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("there has been an exception:"+ex.toString());
}
}
下面是 Student.java 文件的内容:
package hello;
//import org.springframework.beans.factory.annotation.Autowired;
public class Student {
private int age;
private String name;
public void setAge(int age){
this.age = age;
}
public int getAge(){
System.out.println("age:"+age);
return age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
System.out.println("name:"+name);
return name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
下面是 MainApp.java 文件的内容:
package hello;
//import org.springframework.context.support.AbstractApplicationContext;
//import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//import org.springframework.context.annotation.*;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
student.getName();
student.getAge();
//student.printThrowException();
}
}
下面是配置文件 Beans.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: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.xsd">
<!--aop:config>
<aop:aspect id="log" ref="logging">
<aop:pointcut id="selectAll" expression="execution(* hello.*.*(..))"/>
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
<aop:after pointcut-ref="selectAll" method="afterAdvice"/>
<aop:after-returning pointcut-ref="selectAll"
returning="retVal"
method="afterReturningAdvice"/>
<aop:after-throwing pointcut-ref="selectAll"
throwing="ex"
method="AfterThrowingAdvice"/>
</aop:aspect>
</aop:config-->
<aop:aspectj-autoproxy/>
<!-- Definition for student bean -->
<bean id="student" class="hello.Student">
<property name="name" value="番茄"/>
<property name="age" value="10"/>
</bean>
<!-- Definition for logging aspect -->
<bean id="logging" class="hello.Logging">
</bean>
</beans>
运行一下应用程序
Going to setup student profile.
name:番茄
Student profile has been setup
Returning:番茄
Going to setup student profile.
age:10
Student profile has been setup
Returning:10
Process finished with exit code 0
每天学习一点点,每天进步一点点。
Spring 中基于 AOP 的 @AspectJ的更多相关文章
- Spring中基于AOP的@AspectJ
以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/aspectj-based-aop-with- ...
- Spring 中基于 AOP 的 @AspectJ注解实例
@AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格.通过在你的基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的 ...
- Spring 中基于 AOP 的 XML架构
Spring 中基于 AOP 的 XML架构 为了使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述: <?xml version="1.0" e ...
- Spring中基于AOP的XML架构
以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/xml-schema-based-aop-wi ...
- spring中基于aop使用ehcache
我就是对着这个博客看的 http://www.cnblogs.com/ctxsdhy/p/6421016.html
- Spring中基于xml的AOP
1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...
- spring中基于注解使用AOP
本文内容:spring中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- JavaWeb_(Spring框架)认识Spring中的aop
1.aop思想介绍(面向切面编程):将纵向重复代码,横向抽取解决,简称:横切 2.Spring中的aop:无需我们自己写动态代理的代码,spring可以将容器中管理对象生成动态代理对象,前提是我们对他 ...
随机推荐
- IDEA 之 Java项目复制
1.复制一个项目,并改名字 2.更改以下文件名字 3.将以下文件中的原有名字,替换成更改后的名字(例如MyWebapp07替换成MyWebapp08) 4.将out文件夹给删除 5.然后用IDEA ...
- js之 DOM与BOM
JavaScript HTML DOM (文档对象模型)(Document Object Model) 什么是DOM? DOM是W3C标准. DOM定义了访问文档的标准: “W3C文档对象模型(DOM ...
- 小米Note 10 Lite海外发布 无缘中国市场
[TechWeb]5月1日消息,昨日晚间,小米Note 10 Lite在海外亮相.小米市场部副总经理臧智渊在微博透露,小米Note 10 Lite 6GB+64GB版售价349欧元(约合人民币2700 ...
- 标准SQL语句大全【持续更新】(navicat12版亲测有效)
提示:用ctrl+F快速查找相关指令哦 -- 创建数据库 create database test_sql; -- 修改数据库名称(只有 sysadmin 和 dbcreator 固定服务器角色的成员 ...
- 如何将Superset嵌入后台系统之实践
1. 前言 此次实践过程全属个人学习,我选择了在window下安装Superset,并进行嵌入后台系统实践.对此进行实践过程总结,实践成果分享给大家,供大家参考,如果你有更好的想法,欢迎留言交流. 2 ...
- Unity(GameObject)
####1. 这个方法用于发送一个数据到指定的方法中,第三个参数是是否强制接收 以下三种方法发送消息的形式,各有不同的效果,可以通过第一个参数指定要发送的方法名名,第二个是发送的参数值,第三个参数是是 ...
- 关于LinearLayout设置权重后width或height不设置0dp的影响说明
摘要 平时没那么注意LinearLayout布局时权重的问题,设置了权重属性后,通常建议将width或height的属性值设置为0dp,有时候设置权重后,还是习惯将width或height的属性设置为 ...
- Linux shell基础(五)sed命令
一.sed命令 sed是一种强大的流式编辑器 (stream editor for filtering and transforming text),它能够完美的使用正则表达式,逐行处理文本并把结果显 ...
- IEEE 754标准--维基百科
IEEE二进制浮点数算术标准(IEEE 754) 是20世纪80年代以来最广泛使用的浮点数运算标准,为许多CPU与浮点运算器所采用.这个标准定义了表示浮点数的格式(包括负零-0)与反常值(denorm ...
- 疯子的算法总结10--最小生成树Kruscal
按照权值排序可得,就有如下顺序: 1. 1-2 1 2. 1-4 2 3. 1-5 2 4. 2-5 3 5. 2-3 4 6. 4-5 4 每次选取最小边泉,判断是否同属一个集合,如果不属于同一集合 ...