AOP实现的三种方法 切入点 切入面

1.AOP实现的原生方法spring的API接口

动态代理类似

SpringAPI接口实现类

public class BeforeLog implements MethodBeforeAdvice {
    /**
     *
     * @param method 方法
     * @param args 参数
     * @param target 目标对象
     * @throws Throwable
     */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+target.getClass().getName()+"的"+method.getName()+"方法");
    }
}

spring配置文件的编写

<?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到spring容器中-->
    <bean id="UserImpl" class="service.UserImpl"/>
    <bean id="BeforeLog" class="log.BeforeLog"/>

    <aop:config>
<!--    添加切入点  execution(* service.UserImpl.*(..))表示切入到这个类的所有方法-->
        <aop:pointcut id="pointcut" expression="execution(* service.UserImpl.*(..))"/>
<!--    设置切入的日志类  pointcut-ref切入到哪里 -->
        <aop:advisor advice-ref="BeforeLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

接口和接口实现类的编写

package service;

/**
 * @author:LeeGaki
 * @date:2022/1/13
 */
public class UserImpl implements User{
    @Override
    public void add() {
        System.out.println("add");
    }

    @Override
    public void sel() {
        System.out.println("sel");

    }

    @Override
    public void del() {
        System.out.println("del");

    }

    @Override
    public void upd() {
        System.out.println("upd");

    }
}

测试类的编写

    @Test
    public void log(){
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取的返回值要是接口,获取的bean应该是实现类
        User userImpl = context.getBean("UserImpl", User.class);
        userImpl.add();
    }

2.AOP实现的第二种方法

自定义类(切面)的实现

配置文件中aop的配置

    <aop:config>
<!--        创建切入点 第一个*代表返回值类型 后面表示切入到这个类的所有方法-->
        <aop:pointcut id="point" expression="execution(* service.UserImpl.*(..))"/>
<!--        ref后是自定义切面 也就是自定义的类-->
        <aop:aspect ref="DiyLog">
<!--            切入到哪个方法的前还是后-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

自定义类(切面)

package log;

/**
 * @author:LeeGaki
 * @date:2022/1/13
 */
public class DiyLog {
    public void before(){
        System.out.println("====执行前========");
    }
    public void after(){
        System.out.println("====执行后========");
    }
}

测试类

    @Test
    public void log(){
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取的返回值要是接口,获取的bean应该是实现类
        User userImpl = context.getBean("UserImpl", User.class);
        userImpl.add();
    }

3.注解式实现aop

首先打开aop注解支持在配置文件中加入

<aop:aspectj-autoproxy/>

编写自定义类 添加注解

package log;

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

/**
 * @author:LeeGaki
 * @date:2022/1/13
 */
@Aspect
public class DiyLog {
    @Before("execution(* service.UserImpl.*(..))")
    public void before(){
        System.out.println("====执行前========");
    }
    @After("execution(* service.UserImpl.*(..))")
    public void after(){
        System.out.println("====执行后========");
    }

    @Around("execution(* service.UserImpl.*(..))")
    public void around(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("环绕前");
        Object proceed = pj.proceed();
        System.out.println("环绕后");
    }
}

前后环绕都存在的时候实现的顺序是

环绕前
====执行前========
add
====执行后========
环绕后
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Spring AOP实现返回前字典值翻译,可以使用自定义注解和 AOP 切面来实现。 首先,在返回值需要翻译的方法上添加自定义注解,例如 @DictTranslate: ```java @DictTranslate public List<User> getUsers() { // ... } ``` 然后,编写 AOP 切面,在方法执行完成后,对返回值进行翻译操作。可以使用 Map 存储字典值和翻译后的值的对应关系,使用反射获取方法返回值类型,判断是否是需要翻译的类型,然后遍历返回值进行翻译操作。 ```java @Aspect @Component public class DictTranslateAspect { // 字典值和翻译后的值的对应关系 private static final Map<String, String> DICT_MAP = new HashMap<>(); // 切入点,对添加了 @DictTranslate 注解的方法进行切面操作 @Pointcut("@annotation(com.example.DictTranslate)") public void dictTranslate() {} // 方法执行完成后,对返回值进行翻译操作 @AfterReturning(pointcut = "dictTranslate()", returning = "returnValue") public Object doAfterReturning(JoinPoint joinPoint, Object returnValue) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Class<?> returnType = methodSignature.getReturnType(); // 判断返回值类型是否是需要翻译的类型 if (List.class.isAssignableFrom(returnType)) { List<?> list = (List<?>) returnValue; // 遍历返回值进行翻译操作 for (Object obj : list) { Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Dict.class)) { field.setAccessible(true); Object value = field.get(obj); String dictType = field.getAnnotation(Dict.class).value(); String dictValue = DICT_MAP.get(dictType + "_" + value); if (dictValue != null) { field.set(obj, dictValue); } } } } } return returnValue; } } ``` 在上述代码中,@DictTranslate 注解用于标记需要进行翻译的方法,DictTranslateAspect 类是 AOP 切面,用于在方法执行完成后对返回值进行翻译操作。@Pointcut 注解用于定义切入点,@AfterReturning 注解用于定义方法执行完成后的操作。 需要注意的是,@Dict 注解用于标记需要翻译的字段,其 value 属性表示字典的类型。DICT_MAP 变量是字典值和翻译后的值的对应关系,可以从数据库或其他数据源中加载字典数据。在翻译操作中,使用 field.getAnnotation(Dict.class).value() 获取字典类型,使用 field.get(obj) 获取需要翻译的值,然后在 DICT_MAP 中查找对应的翻译后的值,最后使用 field.set(obj, dictValue) 将翻译后的值设置回对象中。 这样,在方法返回前,就可以完成对字典值的翻译操作了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LeeGaKi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值