1.3.spring aop

什么是aop

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。

AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。

利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

 

流程

 

 

使用

maven

 

<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

  

待切入方法

 

public class Method {
    public void hello(){
        System.out.println("hello");
    }
}

  

切入方法

 

@Aspect
public class PointCut {
    @Around(value = "execution(* aop.Method.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("开始");
        Signature signature = jp.getSignature();
        System.out.println("signature:"+signature);
        jp.proceed();
        System.out.println("结束");
    }
}

  

配置文件

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="method" class="aop.Method"></bean>
    <bean id="cutpoint" class="aop.PointCut"></bean>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

  

测试

 

@Test
    public void aopTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("myaop.xml");
        Method bean = context.getBean("method", Method.class);
        bean.hello();
    }

  

 

posted @ 2021-02-26 00:51  ljk12345  阅读(30)  评论(0)    收藏  举报