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

具体使用的地方

连接点

相关推荐
妙娲种子22 分钟前
gittee连接github镜像
gitee·github
无限进步_41 分钟前
C语言实现贪吃蛇游戏完整教程【最终版】
c语言·开发语言·c++·git·游戏·github·visual studio
CoderJia程序员甲1 小时前
GitHub 热榜项目 - 日榜(2026-01-21)
ai·开源·大模型·github·ai教程
亮子AI1 小时前
【Github】如何取消 issue 自动加入 project 的功能?
github·issue
徐小夕@趣谈前端2 小时前
【推荐】jitword协同文档新增AI公文助手,一键生成红头文件
vue.js·人工智能·开源·编辑器·github
Allen_LVyingbo2 小时前
用Python实现辅助病案首页主诊断编码:从数据清洗到模型上线(上)
开发语言·python·github·知识图谱·健康医疗
海雅信子2 小时前
上传本地文件到github
github
Cliven_2 小时前
Github自动打包推送Maven中央仓库
java·github·maven
DarrenPig11 小时前
【常州工NEC】ROBOCON小白入门之路(二)2026CURC-NEC赛季规划
开源·github·交流·robocon
ccino .17 小时前
【将更改从 GitHub Desktop 推送到 GitHub】
github