好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring基于AspectJ的AOP开发案例解析

使用AspectJ实现AOP 注解方式 XML方式

AspectJ简介

AspectJ是一个基于Java语言的AOP框架 Spring2.0以后新增了对AspectJ切点表达式支持 @AspectJ是AspectJ1.5新增的功能,通过JDK5注解技术,允许直接在Bean类中定义切面 新版本Spring框架,建议使用AspectJ方式来开发AOP 使用AspectJ需要导入Spring AOP和AspectJ相关jar包 Spring-aop springsource.org.aopalliance spring-aspects springsource.org.aspectj.weaver

注解开发

环境准备

应入相关的 jar包 junit,javax.annotation-api,spring-core,spring-beans,spring-context,spring-expression,aopalliance,spring-aop(Spring基础包) aspectjweaver,spring-aspects(AspectJ使用的) spring-test(测试使用) XML引入相应配置

?

1

2

3

4

5

6

7

8

9

<? xml version = "1.0" encoding = "UTF-8" ?>

< beans xmlns = "http://HdhCmsTestspringframework.org/schema/beans"

        xmlns:xsi = "http://HdhCmsTestw3.org/2001/XMLSchema-instance"

        xmlns:aop = "http://HdhCmsTestspringframework.org/schema/aop" xsi:schemaLocation="

         http://HdhCmsTestspringframework.org/schema/beans http://HdhCmsTestspringframework.org/schema/beans/spring-beans.xsd

         http://HdhCmsTestspringframework.org/schema/aop http://HdhCmsTestspringframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

         <!--需要单独引入-->

         < aop:aspectj-autoproxy />

</ beans >

不同的通知类型

@Before前置通知,相当于BeforeAdvice @AfterReturning后置通知,相当于AfterReturningAdvice @Around环绕通知,相当于MethodInterceptor(可以阻止方法的进行,功能最强。列:事务管理) @AfterThrowing异常抛出通知,相当于ThrowAdvice @After最终final通知,不管是否异常,该通知都会执行 @DeclarwParents引介通知,相当于IntroductionInterceptor(不要求掌握)

最通知中通过value属性定义切点

通过execution函数,可以定义切点的方法切入 语法: --execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)(访问修饰符可以省略) 列如

--匹配所有类public方法 execution(publice * * (..))---第一个*:任意返回值 第二个*:任意名称 (..):任意参数 相当于所有以public开头的方法加了一个前置通知的话都会执行 --匹配指定包下所有类方法 execution(* top.odliken.demo.(..)) 不包含子包 .:包 --execution(* top.odliken.demo..(..)) ..便是包、子酸包下所有类 --匹配指定类所有方法 execution( top.odlien.demo.UserService.(..)) 第一个*:任意返回值 UserService.:UserService类下面的所有方法 --匹配实现特定接口所有方法 execution( top.odliken.demo.GenericDao+.(..)) +:子类 .:方法名 --匹配所有save开头的方法 execution(* save*(..))

入门案列

?

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

============XML==========

<!--配置目标类=================-->

<bean id= "customerDao" class = "com.imooc.aspectJ.demo2.CustomerDaoImpl" />

<!--配置切面类-->

<bean id= "myAspectXml" class = "com.imooc.aspectJ.demo2.MyAspectXml" />

===========ProductDao===========

public class ProductDao {

     public void save() {

         System.out.println( "保存商品....." );

     }

     public void findOne() {

         System.out.println( "查找一个商品....." );

     }

     public void findAll() {

         System.out.println( "查找所有商品....." );

     }

     public void update() {

         System.out.println( "修改商品....." );

     }

     public void delete() {

         System.out.println( "删除商品....." );

     }

}

===========MyAspectAnno===========

@Aspect

public class MyAspectAnno {

     @Before (value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))" )

     public void before(){

         System.out.println( "=========前置通知=========" );

     }

}

===========Springdemo1===========

@RunWith (SpringJUnit4ClassRunner. class )

@ContextConfiguration ( "classpath:afterglow.xml" )

public class Springdemo1 {

     @Resource (name = "productDao" )

     private ProductDao productDao;

     @Test

     public void demo1(){

         productDao.save();

         productDao.findOne();

         productDao.findAll();

         productDao.update();

         productDao.delete();

     }

}

@Before前置通知

可以在方法中传入JoinPoint对象,用来获取切点信息

?

1

2

3

public void before(JoinPoint joinPoint){

     System.out.println( "=========前置通知=========" +joinPoint);

}

@AfterReturning后置通知

通过returning属性可以定义返回值,作为参数

?

1

2

3

4

@AfterReturning (value = "execution(* top.odliken.aspectJ.demo1.ProductDao.update(..))" ,returning = "result" )

public void afterReturing(Object result){

     System.out.println( "==========后置通知==========" +result);

}

@Around环绕通知

Around方法的返回值就是目标代理方法执行返回值 参数ProceedingJoinPoint 可以调用拦截目标方法执行 如果不调用 ProceedingJoinPoint的 proceed方法,那么目标方法就背拦截了

?

1

2

3

4

5

6

7

@Around (value = "execution(* top.odliken.aspectJ.demo1.ProductDao.delete(..)))" )

public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

     System.out.println( "环绕通知======前" );

     Object obj = joinPoint.proceed(); //执行目标方法 不调用就不执行

     System.out.println( "环绕通知======后" );

     return obj;

}

@AfterThrowing 异常抛出通知

通过设置throwing属性,可以设置发生异常对象参数

?

1

2

3

4

@AfterThrowing (value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findOne(..)))" ,throwing = "e" )

public void   afterThrowing(Throwable e){

     System.out.println( "==========异常通知==========" +e.getMessage());

}

@After 最终通知

无论是否出现异常,最终通知总是会被执行的。就像Java异常中的 finall块一样

?

1

2

3

4

@After (value = "execution(* top.odliken.aspectJ.demo1.ProductDao.findAll(..))" )

public void after(){

     System.out.println( "==========最终通知==========" );

}

通过@Pointcut为切点命名

在每个通知内定义切点,会造成工作量大,不易维护,对于重复的切点,可以使用@Pointcut进行定义 切点方法:private void 无参方法,方法名为切点名 当通知多个切点是,可以使用||连接

?

1

2

3

4

5

6

@Before (value = "myPointcut1()" )

public void before(JoinPoint joinPoint){

     System.out.println( "=========前置通知=========" +joinPoint);

}

@Pointcut (value = "execution(* top.odliken.aspectJ.demo1.ProductDao.save(..))" )

private void myPointcut1(){}

AspectJ的XML方式的AOP开发

?

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

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

==========CustomerDao==========

public interface CustomerDao {

     public void save();

     public String update();

     public void delete();

     public void findOne();

     public void findAll();

}

==========CustomerDaoImpl==========

public class CustomerDaoImpl implements CustomerDao {

     public void save() {

         System.out.println( "保存客户..." );

     }

     public String update() {

         System.out.println( "修改客户..." );

         return "spring" ;

     }

     public void delete() {

         System.out.println( "删除客户..." );

     }

     public void findOne() {

         System.out.println( "查询一个客户..." );

//       int a = 1/ 0;

     }

     public void findAll() {

         System.out.println( "查询多个客户..." );

//        int b = 1/0;

     }

}

==========MyAspectXml==========

public class MyAspectXml {

     // 前置通知

     public void before(JoinPoint joinPoint) {

         System.out.println( "XML方式的前置通知==============" + joinPoint);

     }

     // 后置通知

     public void afterReturing(Object result) {

         System.out.println( "XML方式的后置通知==============" + result);

     }

     // 环绕通知

     public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

         System.out.println( "XML方式的环绕前通知==============" );

         Object obj = joinPoint.proceed();

         System.out.println( "XML方式的环绕后通知==============" );

         return obj;

     }

     // 异常抛出通知

     public void afterThrowing(Throwable e) {

         System.out.println( "XML方式的异常抛出通知=============" + e.getMessage());

     }

     // 最终通知

     public void after() {

         System.out.println( "XML方式的最终通知=================" );

     }

}

==========SpringDemo2==========

@RunWith (SpringJUnit4ClassRunner. class )

@ContextConfiguration (value= "classpath:applicationContext2.xml" )

public class SpringDemo2 {

     @Resource (name= "customerDao" )

     private CustomerDao customerDao;

     @Test

     public void demo1(){

         customerDao.save();

         customerDao.update();

         customerDao.delete();

         customerDao.findOne();

         customerDao.findAll();

     }

}

==========XML==========

<!--XML的配置方式完成AOP的开发===============-->

<!--配置目标类=================-->

<bean id= "customerDao" class = "com.imooc.aspectJ.demo2.CustomerDaoImpl" />

<!--配置切面类-->

<bean id= "myAspectXml" class = "com.imooc.aspectJ.demo2.MyAspectXml" />

<!--aop的相关配置=================-->

<aop:config>

     <!--配置切入点-->

     <aop:pointcut id= "pointcut1" expression= "execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))" />

     <aop:pointcut id= "pointcut2" expression= "execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))" />

     <aop:pointcut id= "pointcut3" expression= "execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))" />

     <aop:pointcut id= "pointcut4" expression= "execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))" />

     <aop:pointcut id= "pointcut5" expression= "execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))" />

     <!--配置AOP的切面-->

     <aop:aspect ref= "myAspectXml" >

         <!--配置前置通知-->

         <aop:before method= "before" pointcut-ref= "pointcut1" />

         <!--配置后置通知-->

         <aop:after-returning method= "afterReturing" pointcut-ref= "pointcut2" returning= "result" />

         <!--配置环绕通知-->

         <aop:around method= "around" pointcut-ref= "pointcut3" />

         <!--配置异常抛出通知-->

         <aop:after-throwing method= "afterThrowing" pointcut-ref= "pointcut4" throwing= "e" />

         <!--配置最终通知-->

         <aop:after method= "after" pointcut-ref= "pointcut5" />

     </aop:aspect>

</aop:config>

到此这篇关于Spring的基于AspectJ的AOP开发的文章就介绍到这了,更多相关Spring基于AspectJ的AOP开发内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://juejin.cn/post/7096745547414896648

查看更多关于Spring基于AspectJ的AOP开发案例解析的详细内容...

  阅读:12次