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

具体使用的地方

连接点

相关推荐
小锋学长生活大爆炸4 小时前
【福利】最新免费领取云服务器和虚拟主机攻略
网络·github
码流怪侠6 小时前
GitHub 2026年7月热门项目全景盘点:Agent Skills 生态炸裂,开源世界正在重写规则
程序员·github·agent
我叫黑大帅6 小时前
git 的 NFD 与 NFC 有什么区别?为什么我有个文件在 NFC 中间不会被当成改动,在 NFD 中就会当成改动
git·面试·github
DogDaoDao12 小时前
【GitHub】WorldMonitor:一个工程极致主义的实时全球情报仪表盘深度解析
python·程序员·架构·github·go语言·worldmonitor·实时全球情报
其实防守也摸鱼12 小时前
GitHub开源项目破圈方法论:从技术自嗨到生态共赢
服务器·数据库·学习·开源·github·命令行·linux系统
析数塔13 小时前
SleeperGem 技术拆解:恶意 gem 是怎么躲过 CI/CD 扫描的
安全·ci/cd·github
Lvan的前端笔记14 小时前
ios:GitHub Actions云端Xcode26打包上架TestFlight
macos·github·xcode
炸膛坦客15 小时前
Git 和 GitHub:(五)一文给出常用 Git 命令,持续更新中......
git·github
迷茫中的自我16 小时前
GitHub Actions自动化运维实战:从CI/CD到云原生部署
运维·自动化·github
炸膛坦客1 天前
Git 和 GitHub:(三)基础知识与实用技巧
git·github