(十)AOP

什么是AOP

AOP

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

AOP在Spring中的作用

提供声明式事务;允许用户自定义切面

  • 横切关注点:跨越应用程序多个模块的方法或功能。即,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点,如日志,安全,缓存,事务等等…
  • 切面(aspect):横切关注点被模块化的特殊对象。即,一个类。
  • 通知(advice):切面必须要完成的工作。即,类中的一个方法。
  • 目标(Target):被通知对象
  • 代理(Proxy):向目标对象应用通知之后创建的对象。
  • 切入点(PointCut):切面通知执行的"地点"的定义。
  • 连接点(joinPoint):与切入点匹配的执行点。

AOP实现机制

在这里插入图片描述

使用Spring 原生API实现AOP(侧重Spring API接口实现)

Execution表达式:

标识符含义
execution()表达式的主题
第一个" * "符号表示返回值的类型任意
com.xiao.service.UserServicelmplAOP所切的服务包,即,需要横切的业务类
第二个” * “表示类名,*表示所有类
.*(…)表示任意方法名,括号包括参数,两个点表示任意参数类型

整体思路:

Service很纯粹做Service层的事情,日志类也只是执行日志的任务,最后通过Spring作为中间件,执行AOP切入。切入需要切入点,要切入的信息

先导入依赖包:

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

在这里插入图片描述
UserService

package com.xiao.service;

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();

}

UserServicelmpl

package com.xiao.service;

public class UserServicelmpl implements UserService{


    public void add() {
        System.out.println("增加了一个用户");
    }

    public void delete() {
        System.out.println("删除了一个用户");
    }

    public void update() {
        System.out.println("更新了一个用户");
    }

    public void select() {
        System.out.println("查询了一个用户");
    }
}

afterlog

package com.xiao.Log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class afterlog implements AfterReturningAdvice {
    public void afterReturning(Object returnvalue, Method method, Object[] objects, Object targer) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnvalue);
    }
}

log

package com.xiao.Log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class log implements MethodBeforeAdvice {

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

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


    <!-- 注册bean -->
    <bean id="UserService" class="com.xiao.service.UserServicelmpl"/>
    <bean id="log" class="com.xiao.Log.log"/>
    <bean id="afterlog" class="com.xiao.Log.afterlog"/>

    <!-- 方式一:使用原生Spring API接口 -->
    <!-- 配置AOP:需要导入aop的约束-->
    <aop:config>
        <!-- 切入点,即我们需要在哪个地方执行spring的方法;
            expression:表达式,execution(要执行的位置)
        -->
        <!-- *表示任意/所有;..表示可以有任意参数 -->
        <aop:pointcut id="pointcut" expression="execution(* com.xiao.service.UserServicelmpl.*(..))"/>

        <!--切入信息-->
        <!-- 将log这个类切入到pointcut的方法上面 -->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <!-- 将afterlog这个类切入到pointcut的方法上面 -->
        <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

测试类

import com.xiao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
       ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) Context.getBean("UserService");

        userService.add();

    }
}

方式二:使用自定义类实现AOP(侧重切面定义)

自定义类

package com.xiao.Diy;

public class DiyPointCut {
    public void before(){
        System.out.println("执行方法前===========");

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

        <!--方式二:自定义类-->
        <bean id="diy" class="com.xiao.Diy.DiyPointCut"/>
        <aop:config>
            <!--自定义切面,将diy类定义为切面,ref要引用的类-->
            <aop:aspect ref="diy">
                <!-- 切入点 -->
                <aop:pointcut id="point" expression="execution(* com.xiao.service.UserService.*(..))"/>

                <!--通知,即切面要完成的工作,即,类中的一个方法-->
                <aop:before method="before" pointcut-ref="point"/>
                <aop:after method="after" pointcut-ref="point"/>
            </aop:aspect>
        </aop:config>

方式三:使用注解实现

package com.xiao.Diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

import java.lang.reflect.Proxy;

@Aspect     //表明这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.xiao.service.UserServicelmpl.*(..))")      // 切入点
    public void before(){
        System.out.println("=========输出方法前==========");
    }
    @After("execution(* com.xiao.service.UserServicelmpl.*(..))")
    public void after(){
        System.out.println("=========输出方法后==========");
    }
    //在环绕增强中,可以给定一个参数,代表要获取切入的点
    @Around("execution(* com.xiao.service.UserServicelmpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");

       /* Signature signature = jp.getSignature();//获得签名
        System.out.println("sinature" + signature);*/
        //执行方法
        Object proceed = jp.proceed();

        System.out.println("环绕后");
    }
}
    <!-- 方式三 注解实现-->
    <bean id="AnnotationPointCut" class="com.xiao.Diy.AnnotationPointCut"/>
    <!-- 开启注解支持-->
    <aop:aspectj-autoproxy/>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值