@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Loggable { } public class Example { @Loggable public void doSomething() { // perform some action } } public class Logger { public static void logMethodCall(Method method) { System.out.println("Method called: " + method.getName()); } } public class LoggableAspect { @Around("@annotation(Loggable)") public Object logMethodInvocation(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Logger.logMethodCall(method); return joinPoint.proceed(); } } @Configuration @EnableAspectJAutoProxy public class AppConfig { @Bean public LoggableAspect loggableAspect() { return new LoggableAspect(); } @Bean public Example example() { return new Example(); } } public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Example example = context.getBean(Example.class); example.doSomething(); context.close(); } }


上一篇:
下一篇:
切换中文