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

具体使用的地方

连接点

相关推荐
qq7422349843 小时前
VitePress静态网站从零搭建到GitHub Pages部署一站式指南和DeepWiki:AI 驱动的下一代代码知识平台
人工智能·python·vue·github·vitepress·wiki
用户219916797039114 小时前
.Net通过EFCore和仓储模式实现统一数据权限管控并且相关权限配置动态生成
后端·github
DKNG17 小时前
【Windows Host】 hosts配置增加访问github流畅度
人工智能·git·github
逛逛GitHub18 小时前
我把公众号文章导入了腾讯 ima,可以对话找开源项目了。
前端·github
徐小夕1 天前
10k Star 的开源 AI 记忆引擎:6 行代码,用图谱+向量打造永不遗忘的 AI
前端·后端·github
慕容长风1 天前
github使用指南
github
WYiQIU1 天前
从今天开始备战1月中旬的前端寒假实习需要准备什么?(飞书+github+源码+题库含答案)
前端·javascript·面试·职场和发展·前端框架·github·飞书
一念一花一世界1 天前
Arbess从基础到实践(20) - 集成GitHub+SonarQube实现Java项目自动化部署
java·github·cicd·arbess
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2025-12-17)
ai·开源·大模型·github·ai教程
编程修仙2 天前
github的使用
github