好得很程序员自学网

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

Spring框架AOP面向切面编程原理全面分析

1.什么是AOP

AOP:Aspect Oriented Programming ?向切?编程。

AOP面向切面的优势

降低模块之间的耦合度。 使系统更容易扩展。 更好的代码复?。 ?业务代码更加集中,不分散,便于统?管理。 业务代码更加简洁存粹,不参杂其他代码的影响。

AOP 是对?向对象编程的?个补充,在运?时,动态地将代码切?到类的指定?法、指定位置上的编程 思想就是?向切?编程。将不同?法的同?个位置抽象成?个切?对象,对该切?对象进?编程就是 AOP。

AOP需要添加的依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

< dependency >

       < groupId >org.springframework</ groupId >

       < artifactId >spring-aop</ artifactId >

       < version >5.3.9</ version >

     </ dependency >

     < dependency >

       < groupId >org.springframework</ groupId >

       < artifactId >spring-aspects</ artifactId >

       < version >5.3.9</ version >

     </ dependency >

     < dependency >

       < groupId >org.springframework</ groupId >

       < artifactId >spring-context</ artifactId >

       < version >5.3.9</ version >

     </ dependency >

2.简述AOP工作运行原理

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

import java.util.Arrays;

public class MyInvocationHandler implements InvocationHandler {

     private Object object= null ;

     public Object bind(Object object){

         this .object=object;

         return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(), this );

     }

     @Override

     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

         System.out.println(method.getName()+ "方法的参数" + Arrays.toString(args));

         Object result=method.invoke( this .object,args);

         System.out.println(method.getName()+ "的结果是" +result);

         return result;

     }

}

以上是动态创建AOP的方法,首先通过bind返回

Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass.getInterfaces(),this)返回代理对象

接着运用反射的invoke方法,实现面向切面编程

用method.getName()获取方法名

用arg获取属性

动态创建的总结:

以上是通过动态代理实现 AOP 的过程,?较复杂,不好理解,Spring 框架对 AOP 进?了封装,使? Spring 框架可以??向对象的思想来实现 AOP

3.使用Spring创建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

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect

@Component

public class LoggerAspect {

     //@before表适方法执行的具体位置和时机

     @Before ( "execution(public int Util.impl.Calimpl.*(..))" )

     public void before(JoinPoint joinPoint){

         //获取方法名

         String name=joinPoint.getSignature().getName();

         //获取参数

         String args= Arrays.toString(joinPoint.getArgs());

         System.out.println(name+ "方法的参数是" +args);

     }

     @After ( "execution(public int Util.impl.Calimpl.*(..))" )

     public   void after(JoinPoint joinPoint){

         String name=joinPoint.getSignature().getName();

         System.out.println(name+ "执行完毕" );

     }

     @AfterReturning (value = "execution(public int Util.impl.Calimpl.*(..))" ,returning = "result" )

     public void returning(JoinPoint joinPoint,Object result){

         System.out.println( "结果是:" +result);

     }

}

注解@Aspect指的是面向切面编程

@Component指的是交给Ioc容器管理

通过Joinpoint.getSignature().getname()获取方法名

然后实现在调用这个方法前,进行运行所需要的日志信息

测试类

?

1

2

3

4

5

6

7

8

9

10

import Util.Cal;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test2 {

     public static void main(String[] args) {

         ApplicationContext applicationContext= new ClassPathXmlApplicationContext( "spring-aop.xml" );

         Cal proxy = (Cal) applicationContext.getBean( "calimpl" );

         proxy.add( 1 , 1 );

     }

}

还是通过Application 读取Spring.xml 再将getBean 默认小写名字,再调用方法即可

Spring.xml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

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

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

        xmlns:context = "http://HdhCmsTestspringframework.org/schema/context"

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

        xmlns:p = "http://HdhCmsTestspringframework.org/schema/p"

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

http://HdhCmsTestspringframework.org/schema/aop/spring-aop-4.3.xsd

  http://HdhCmsTestspringframework.org/schema/beans

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

  http://HdhCmsTestspringframework.org/schema/context

http://HdhCmsTestspringframework.org/schema/context/spring-context-4.3.xsd

">

     <!-- ?动扫描 -->

     < context:component-scan base-package = "Util" >

     </ context:component-scan >

     <!-- 是Aspect注解?效,为?标类?动?成代理对象 -->

     < aop:aspectj-autoproxy ></ aop:aspectj-autoproxy >

</ beans >

以上就是Spring框架AOP面向切面编程全面分析的详细内容,更多关于Spring框架AOP面向切面的资料请关注其它相关文章!

原文链接:https://blog.csdn.net/DrLai/article/details/119295952

查看更多关于Spring框架AOP面向切面编程原理全面分析的详细内容...

  阅读:41次