Spring AOP的配置和实现

因为接下来要记录日志,所以把日志的配置文件也一起导入

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

然后定义一个注解,带有一个方法参数,目的是记录这个方法的运行时间

import java.lang.annotation.*;

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLog {

    String methodName() default "";  //操作名称

}

接下来定义一个类处理切面,并且获取方法执行的时间,其中

joinPoint.proceed();

是执行这个方法

在这个类中,首先定义一个@Pointcut这样一个切面的void方法,这个切面切的是之前的带有MethodLog注解的切面,然后再定义一些方法,带上@Before或者@Around注解,参数是这个切面的void方法,即可对这个切面做相应的Before或者Around操作了。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StopWatch;

import java.lang.reflect.Method;


@Aspect
@Configuration
public class LogAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("@annotation(com.mayproject.seckill.util.MethodLog)")
    public void pointCutMethod() {
    }

    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        Method method = ms.getMethod();
        MethodLog methodLog = method.getAnnotation(MethodLog.class);
        String key = methodLog.methodName();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Object o = joinPoint.proceed();
        stopWatch.stop();
        String result = MessageFormat.format("{0}, 耗时:{1}ms\r", key, stopWatch.getTime());
        LOGGER.info(result);
        return o;
    }
}

因为日志功能暂时还没完全配好,所以先用打印来代替

然后在使用的时候,在对应方法上面加上这个注解即可

比如:

@Transactional
    @MethodLog(methodName = "秒杀动作")
    public OrderInfo miaosha(MiaoshaUser miaoshaUser,GoodsVo goodsVo){

即可在控制台输入以下结果:

登录动作 耗时:7ms
秒杀动作 耗时:12ms

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP配置可以通过XML方式或注解方式进行。 XML方式的配置步骤如下: 1. 在Spring配置文件中引入aop的约束,例如:xmlns:aop="http://www.springframework.org/schema/aop"。 2. 把通知Bean交给Spring管理,使用<bean>标签进行配置。 3. 使用<aop:config>标签开始AOP配置。 4. 使用<aop:aspect>标签配置切面。 5. 使用对应的标签配置通知的类型,例如<aop:before>表示前置通知。 6. 在<aop:before>标签中指定通知方法和切入点表达式。 注解方式的配置步骤如下: 1. 在配置类上使用@Configuration注解进行标记。 2. 使用@ComponentScan注解指定需要扫描的包。 3. 使用@EnableAspectJAutoProxy注解开启Spring对注解AOP的支持。 以上是关于Spring AOP配置的简要说明,具体的配置内容可以参考引用的资料\[1\]、\[2\]和\[3\]。 #### 引用[.reference_title] - *1* *3* [Spring AOP 应用:三种配置实现方式](https://blog.csdn.net/qq_37829947/article/details/117955529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【spring配置】——spring aop配置](https://blog.csdn.net/javawebxy/article/details/50492616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值