什么是AspectJ
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
AspectJ是一个基于Java语言的AOP框架
Spring2.0以后新增了对AspectJ切点表达式支持
@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
新版本Spring框架,建议使用AspectJ方式来开发AOP
AspectJ表达式:
语法:execution(表达式)
- execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
- execution(“ cn.itcast.spring3.demo1.dao.(..)”) ---只检索当前包
- execution(“ cn.itcast.spring3.demo1.dao..(..)”) ---检索包及当前包的子包.
- execution( cn.itcast.dao.GenericDAO+.(..)) ---检索GenericDAO及子类
AspectJ增强:
- @Before 前置通知,相当于BeforeAdvice
- @AfterReturning 后置通知,相当于AfterReturningAdvice
- @Around 环绕通知,相当于MethodInterceptor
- @AfterThrowing抛出通知,相当于ThrowAdvice
- @After 最终final通知,不管是否异常,该通知都会执行
- @DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)
基于注解
第一步:引入相应jar包.
aspectj依赖aop环境.
spring-aspects-3.2.0.RELEASE.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
第二步:编写被增强的类:
UserDao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package cn.spring3.demo1;
/**
* @author NOP
* 被代理的对象
*/
public class UserDao {
public void add() {
// TODO Auto-generated method stub
System.out.println( "添加客户" );
}
public void delete() {
// TODO Auto-generated method stub
System.out.println( "删除客户" );
int i= 1 / 0 ;
}
public String find() {
// TODO Auto-generated method stub
System.out.println( "查询客户" );
return "fanhuizhi" ;
}
public void update() {
// TODO Auto-generated method stub
System.out.println( "修改客户" );
}
}
|
第三步:使用AspectJ注解形式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package cn.spring3.demo1;
import org.aspectj.lang.JoinPoint;
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;
import org.aspectj.lang.annotation.Pointcut;
/**
* @author NOP 切面类:就是切点与增强结合
* 前置增强
*/
@Aspect
public class MyAspect {
@Before (value = "execution(* cn.spring3.demo1.UserDao.add(..))" ) //这里写表达式,写哪些类需要添加
public void before(JoinPoint joinpoint) {
System.out.println( "前置增强..." +joinpoint);
}
@AfterReturning (value = "execution(* cn.spring3.demo1.UserDao.find(..))" ,returning= "returnVal" ) //这里写表达式,写哪些类需要添加
public void afterReturning(Object returnVal){
System.out.println( "后置增强..." + "方法的返回值" +returnVal);
}
@Around (value = "execution(* cn.spring3.demo1.UserDao.delete(..))" ) //这里写表达式,写哪些类需要添加
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println( "环绕前增强..." );
Object obj = proceedingJoinPoint.proceed();
System.out.println( "环绕后增强..." );
return obj;
}
@AfterThrowing (value = "execution(* cn.spring3.demo1.UserDao.delete(..))" ,throwing= "ex" ) //这里写表达式,写哪些类需要添加
public void afterThrowing(Throwable ex) throws Throwable{
System.out.println( "不跑了出异常了..." +ex.getMessage());
}
//@After(value = "execution(* cn.spring3.demo1.UserDao.delete(..))")//这里写表达式,写哪些类需要添加
@After ( "MyAspect.MyPointCut()" ) //类名.方法名
public void after(){
System.out.println( "最终通知" ); //不管有没有异常都会通知
}
//仅是为了定义一个通用的表达式
@Pointcut (value = "execution(* cn.spring3.demo1.UserDao.delete(..))" )
private void MyPointCut(){
}
}
|
第四步:创建applicationContext.xml
1
2
3
4
5
6
7
8
9
|
xmlns:aop="http://www.springframework.org/schema/aop"
* 引入aop的约束:
* <!-- 自动生成代理 底层就是AnnotationAwareAspectJautoProxyCreator -->
< aop:aspectj-autoproxy />
< bean id = "userDao" class = "cn.spring3.demo1.UserDao" />
< bean id = "MyAspect" class = "cn.spring3.demo1.MyAspect" />
|
第五步,编写测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package cn.spring3.demo1;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
/**
* @author NOP
* 自动方式代理没有切点切面的增强
*/
@RunWith (SpringJUnit4Cla***unner. class )
@ContextConfiguration ( "classpath:applicationContext.xml" )
public class SpringTest1 {
@Autowired
@Qualifier ( "userDao" )
private UserDao userDao;
@Test
public void demo1(){
System.out.println( "-----------------" );
userDao.add();
System.out.println( "-----------------" );
userDao.find();
System.out.println( "-----------------" );
userDao.delete();
System.out.println( "-----------------" );
userDao.update();
System.out.println( "-----------------" );
}
}
测试结果:
-----------------
前置增强...execution( void cn.spring3.demo1.UserDao.add())
添加客户
-----------------
查询客户
后置增强...方法的返回值fanhuizhi
-----------------
环绕前增强...
删除客户
最终通知
不跑了出异常了.../ by zero
|
AspectJ的通知类型:
- @Before 前置通知,相当于BeforeAdvice
- 就在方法之前执行.没有办法阻止目标方法执行的.
- @AfterReturning 后置通知,相当于AfterReturningAdvice
- 后置通知,获得方法返回值.
- @Around 环绕通知,相当于MethodInterceptor
- 在可以方法之前和之后来执行的,而且可以阻止目标方法的执行.
- @AfterThrowing抛出通知,相当于ThrowAdvice
- @After 最终final通知,不管是否异常,该通知都会执行
- @DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)
切点的定义:
@Pointcut("execution(* cn.itcast.spring3.demo1.UserDao.find(..))")
private void myPointcut(){}
面试:
Advisor和Aspect的区别?
Advisor:Spring传统意义上的切面:支持一个切点和一个通知的组合.
Aspect:可以支持多个切点和多个通知的组合
基于XML
ProductDao
第一步:编写被增强的类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package cn.spring3.demo2;
public class ProductDao {
public void add() {
// TODO Auto-generated method stub
System.out.println( "添加商品" );
}
public void delete() {
// TODO Auto-generated method stub
System.out.println( "删除商品" );
}
public void find() {
// TODO Auto-generated method stub
System.out.println( "查询商品" );
int i = 1 / 0 ;
}
public String update() {
// TODO Auto-generated method stub
System.out.println( "修改商品" );
return "woshitest" ;
}
}
|
第二步:定义切面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package cn.spring3.demo2;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @author NOP
* 切面类
*/
public class MyAspectXML {
/*
*
*/
public void before(){
System.out.println( "前置增强" );
}
public void afterReturning(Object returnVal){
System.out.println( "后置增强" +returnVal);
}
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println( "环绕前增强" );
Object obj = proceedingJoinPoint.proceed();
System.out.println( "环绕后增强" );
return obj;
}
public void afterThrowing(Throwable ex){
System.out.println( "不跑了出异常了..." +ex.getMessage());
}
public void after(){
System.out.println( "最终通知" ); //不管有没有异常都会通知
}
}
|
第三步:配置applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<!-- 定义被增强的类 -->
< bean id = "productDao" class = "cn.spring3.demo2.ProductDao" />
<!-- 定义切面 -->
< bean id = "myAspectXML" class = "cn.spring3.demo2.MyAspectXML" />
<!-- 定义aop配置 -->
< aop:config >
<!-- 定义切点: -->
< aop:pointcut expression = "execution(* cn.spring3.demo2.ProductDao.add(..))" id = "mypointcut" />
<!-- 定义切点: -->
< aop:pointcut expression = "execution(* cn.spring3.demo2.ProductDao.update(..))" id = "mypointcutar" />
<!-- 定义切点: -->
< aop:pointcut expression = "execution(* cn.spring3.demo2.ProductDao.delete(..))" id = "mypointcutaar" />
<!-- 定义切点: -->
< aop:pointcut expression = "execution(* cn.spring3.demo2.ProductDao.find(..))" id = "mypointcutaat" />
< aop:aspect ref = "myAspectXML" >
<!-- 前置通知-->
< aop:before method = "before" pointcut-ref = "mypointcut" />
<!-- 后置通知 -->
< aop:after-returning method = "afterReturning" pointcut-ref = "mypointcutar" returning = "returnVal" />
<!-- 环绕通知 -->
< aop:around method = "around" pointcut-ref = "mypointcutaar" />
<!-- 异常通知 -->
< aop:after-throwing method = "afterThrowing" throwing = "ex" pointcut-ref = "mypointcutaat" />
<!-- 最终通知//不管有没有异常都会通知 -->
< aop:after method = "after" pointcut-ref = "mypointcutaat" />
</ aop:aspect >
</ aop:config >
|
第四步:编写测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package cn.spring3.demo2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;
@RunWith (SpringJUnit4Cla***unner. class )
@ContextConfiguration ( "classpath:applicationContext2.xml" )
public class SpringTest2 {
@Autowired
@Qualifier ( "productDao" )
private ProductDao productDao;
@Test
public void demo1(){
System.out.println( "-----------------" );
productDao.add();
System.out.println( "-----------------" );
productDao.find();
System.out.println( "-----------------" );
productDao.delete();
System.out.println( "-----------------" );
productDao.update();
System.out.println( "-----------------" );
}
}
测试结果:
-----------------
前置增强
添加商品
-----------------
查询商品
不跑了出异常了.../ by zero
最终通知
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.51cto.com/4534309/2112449