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

具体使用的地方

连接点

相关推荐
u1301309 分钟前
GitHub 热榜项目:日榜(2026-10-07)
驱动开发·github
高频因子挖掘机21 分钟前
量化选股到底要不要每天拉全市场股票?从全市场扫描到候选池的行情数据设计
后端·github·api
miofly29 分钟前
GitHub 日榜趋势速报 | 2026-10-08
开源·github
Skrrapper31 分钟前
AI项目实战日记ep2:把 GitHub Issue 的第一轮脏活交给 AI:做一个 Issue 分诊助手
人工智能·github·issue
高频因子挖掘机40 分钟前
复权价格怎么算?从除权因子、时间方向到量化回测避坑
后端·github·api
Maynor在掘金16 小时前
Nano Banana 2.1 来了!21 个真实案例 + 公开提示词,这份 Awesome 合集请收好
github
miofly16 小时前
ChatGPT 账号用户可免费使用 Auto-review 双智能体审查功能
开源·github
高频因子挖掘机16 小时前
第一次补历史行情:按股票拆,还是按日期拆请求?
后端·github·api
高频因子挖掘机16 小时前
300 只股票 × 两年日 K:为什么逐只循环会变成维护灾难?
后端·github·api
Jooolin16 小时前
把 GitHub Issue 的第一轮脏活交给 AI:做一个 Issue 分诊助手
人工智能·github