Bean_AOP

Bean

源码

https://github.com/cmdch2017/Bean_IOC.git

获取Bean对象


BeanFactory

Bean的作用域

第三方Bean需要用@Bean注解


比如消息队列项目中,需要用到Json的消息转换器,这是第三方的Bean对象,所以不能用@Component,而要用@Bean

AOP

如何生成切面方法的执行时间

execution表达式(一般方法)

表达式

c 复制代码
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

modifiers-pattern: 方法的修饰符模式,例如 public, protected, private, * 等。

ret-type-pattern: 方法的返回类型模式。

declaring-type-pattern: 方法所在类的类型模式。

name-pattern: 方法名模式。

param-pattern: 方法参数模式。

throws-pattern: 方法抛出的异常模式。

举例

匹配在 com.example 包下的任何类中以 "find" 开头的方法,且返回类型为 List:"

java 复制代码
execution(List com.example..*find*(..))

匹配包名为 com.example 下的任何类的任何方法:

c 复制代码
execution(* com.example..*(..))

下面的代码中是为了记录方法的执行时间:

java 复制代码
@Component
@Aspect
public class TimeAspect {
    @Pointcut("execution(* org.wego.controller.*.*(..))")
    private void pt() {
    }

    @Around("pt()")
    public Object calTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object object = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        System.out.println(joinPoint.getSignature() + "执行时间:" + (endTime - startTime) + "ms");
        return object;
    }
}

annotation方法(灵活但麻烦,需要在使用的方法上加上注解,仅在处理不好描述execution表达式的时候)

MyLog

java 复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyLog {
}

MyAspect

java 复制代码
@Component
@Aspect
public class MyAspect {
    @Pointcut("@annotation(org.wego.aop.MyLog)")
    private void pt() {
    }

    @Around("pt()")
    public Object calTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object object = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        System.out.println(joinPoint.getSignature() + "执行时间:" + (endTime - startTime) + "ms");
        return object;
    }
}

具体使用的地方

连接点

相关推荐
m0_6948455710 小时前
Oh My Zsh 使用指南:Zsh 终端配置与插件管理教程
服务器·前端·小程序·开源·github
米小虾11 小时前
hackerbot-claw 攻击事件深度解析:AI Agent 时代的安全警钟
github·ai编程
逛逛GitHub13 小时前
AI 搜索利器 MiroThinker 发布新模型!我接入小龙虾,太实用了。
github
happyprince13 小时前
2026年03月18日热门Model/github项目
github
无限进步_14 小时前
深入解析list:一个完整的C++双向链表实现
开发语言·c++·git·链表·github·list·visual studio
AI成长日志14 小时前
【datawhale】hello agents开源课程第1章学习记录:初识智能体
学习·开源·github
爆打维c15 小时前
Github配置SSH Key(新手友好版)
ssh·github
老虎062716 小时前
Netty[ NIO 核心速成 ] ---- NIO三大组件(Channel & Buffer&selector)
java·github·nio
屑曦晨17 小时前
创建签名密钥库和配置CICD签名
github
成都极云科技17 小时前
2026年服务器托管收费标准解析 性价比对比及边缘节点选型全指南
运维·服务器·github