Spring (三) --- AOP面向切面编程

一,AOP介绍

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率

1,主要功能

日志记录,性能统计,安全控制,事务处理,异常处理等等

2,主要意图

将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码

3,AOP在Spring中的作用

提供声明式事务;允许用户自定义切面
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …

切面(ASPECT)横切关注点 被模块化 的特殊对象,即,它是一个类
通知(Advice)切面必须要完成的工作。即,它是类中的一个方法
目标(Target)被通知对象
代理(Proxy)向目标对象应用通知之后创建的对象
切入点(PointCut)切面通知 执行的 “地点”的定义
连接点(JointPoint)与切入点匹配的执行点

在这里插入图片描述

二,使用SpringAPI实现AOP

1,接口(User)

//抽象角色
public interface User {

    public void show();

}

2,实现类(UserImpl)

//真实角色
public class UserImpl implements User{

    public void show() {
        System.out.println("这是一个show方法");
    }

}

3,定义日志增加类实现

3.1,Log

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

    //method:被调用目标对象的方法
    //args:要被调用的方法的参数
    //target:被调用的目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("目标对象:"+target.getClass().getName()+"的"
                +method.getName()+"方法被执行了");
    }
}

3.2,AfterLog

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {

    //returnValue : 返回值
    //method : 被调用目标对象的方法
    //args : 要被调用的方法的参数
    //target : 被调用的目标对象
    
    public void afterReturning(Object returnValue, Method method, 
                                  Object[] args, Object target) throws Throwable {
        System.out.println("执行了目标对象:"+target.getClass().getName()+"的"
                +method.getName()+"方法,返回值:"+returnValue);
    }
    
}

4,编写Spring核心配置文件(logbeans.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"
       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">

    <!--注册bean-->
    <bean id="User" class="service.UserImpl"/>

    <!--注册日志的bean-->
    <bean id="Log" class="log.Log"/>
    <bean id="AfterLog" class="log.AfterLog"/>

    <!--使用spring的AOP切入-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="pointcut" expression="execution(* service.UserImpl.*(..))"/>
        <!--执行通知,增强-->
        <aop:advisor advice-ref="Log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

【注意点】
在这里插入图片描述

5,编写测试类(AopTest)

public class AopTest {
    @Test
    public void Test01(){
        ClassPathXmlApplicationContext context = 
                                  new ClassPathXmlApplicationContext("logbeans.xml");
        User user = (User) context.getBean("User");

        user.show();
    }
}

【注意点】
测试时需要导入AOP的包在你的pom.xml中

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

6,运行结果

在这里插入图片描述

三,自定义类实现AOP

和使用SpringAPI实现AOP一样,先要建立抽象角色和真实角色,我们这里的接口与实体类和前面一样

1,自定一个Aop增强类:也就是所谓的切面

public class Diy {

    public void before(){
        System.out.println("show方法执行前");
    }

    public void after(){
        System.out.println("show方法执行后");
    }
}

2,配置文件(diybeans.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"
       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">

    <!--注册bean-->
    <bean id="User" class="service.UserImpl"/>

    <!--自定义的AOP增强类-->
    <bean id="Diy" class="diy.Diy"/>

    <!--编写aop配置文件-->
    <aop:config>
        
        <!--切面-->
        <aop:aspect ref="Diy">
            <!--切入点-->
        <aop:pointcut id="diyPointCut" expression="execution(* service.UserImpl.*(..))"/>
            <aop:before method="before" pointcut-ref="diyPointCut"/>
            <aop:after method="after" pointcut-ref="diyPointCut"/>
        </aop:aspect>
        
    </aop:config>



</beans>

3,测试类

public class AopTest {
    @Test
    public void Test02(){
        ClassPathXmlApplicationContext context = 
                                new ClassPathXmlApplicationContext("diybeans.xml");
        User user = (User) context.getBean("User");

        user.show();
    }
}

4,运行结果

在这里插入图片描述

四,使用注解实现AOP

接口和实体类不变

1,编写AOP增强类

@Aspect
public class Anno {

    @Before("execution(* service.UserImpl.*(..))")
    public void before(){
        System.out.println("show方法执行前");
    }

    @After("execution(* service.UserImpl.*(..))")
    public void after(){
        System.out.println("show方法执行后");
    }

}

【注意点】
在这里插入图片描述

2,配置文件(annobeans.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"
       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">

    <!--注册bean-->
    <bean id="User" class="service.UserImpl"/>

    <!--注册日志的bean-->
    <bean id="Log" class="log.Log"/>
    <bean id="AfterLog" class="log.AfterLog"/>

    <!--使用spring的AOP切入-->
    <aop:config>
        <!--切入点-->
        <aop:pointcut id="pointcut" expression="execution(* service.UserImpl.*(..))"/>
        <!--执行通知,增强-->
        <aop:advisor advice-ref="Log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

3,测试类

public class AopTest {
    @Test
    public void Test03(){
        ClassPathXmlApplicationContext context = 
                        new ClassPathXmlApplicationContext("annobeans.xml");
        User user = (User) context.getBean("User");

        user.show();
    }
}

4,运行结果

在这里插入图片描述

五,AOP小结

  • 本质就是动态代理
  • 需要到一个包,用来进行aop织入的包: aspectjweaver
  • 注意别遗漏了切面;
  • 三种实现AOP的方法
    • 使用SpringAPI来实现AOP
    • 使用自定义类来实现AOP
    • 使用注解实现AOP
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值