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;
    }
}

具体使用的地方

连接点

相关推荐
峰向AI11 小时前
GenOffice:开源 Office 套件,字节级保留、本地转换、BYOK模式
github
其实防守也摸鱼12 小时前
权限提升与横向移动:从内网渗透到域控的完整技术图谱
运维·服务器·数据库·安全·github·copilot·渗透
m4Rk_13 小时前
【论文阅读】Agent 记忆机制(57):MemSearch-o1——从查询词元生长证据,重组 Deep Search 记忆路径
论文阅读·人工智能·学习·开源·github
其实防守也摸鱼13 小时前
免杀与持久化入门:从载荷免杀到隐蔽通道的完整指南
运维·服务器·数据库·安全·github·copilot·渗透
sec_jay13 小时前
一个面向有限本地语料库的、单智能体、工具调用型 Agentic RAG 原型--关于agent的实践调研报告
github
goplaysource16 小时前
IPTV 频道去重:用哈希解决"同一个频道出现十遍"的问题
github
liuyicenysabel17 小时前
从 0 到 1:一套 GitHub + GHCR + k3s 的全自动 CI/CD 流水线(Flask 项目实战)
ci/cd·flask·github
u13013018 小时前
GitHub 热榜项目:日榜(2026-08-31)
github
Justinsky19 小时前
DeepSeek Harness 开源一个月,我把它整个嵌进了 Electron 桌面软件
github
小宋102119 小时前
MCP 是什么:从零编写一个可供 AI 调用的工具服务
人工智能·github