From de04e5e85682717ae89ea7849a909fa4366b84f3 Mon Sep 17 00:00:00 2001 From: jdegand <70610011+jdegand@users.noreply.github.com> Date: Sun, 28 May 2023 14:39:31 -0400 Subject: [PATCH] LoggerAOPadded --- README.md | 11 +++++++++- Spring/LoggerAOP.java | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Spring/LoggerAOP.java diff --git a/README.md b/README.md index 3e82e23..0c8d01a 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,13 @@ - [Stack Overflow](https://stackoverflow.com/questions/8247970/using-like-wildcard-in-prepared-statement) - using like wildcard in prepared statement - [Mkyong](https://mkyong.com/spring/spring-jdbctemplate-querying-examples/) - jdbctemplate querying examples - [Spring Docs](https://docs.spring.io/spring-framework/reference/integration/scheduling.html) - scheduling -- [Stack Overflow](https://stackoverflow.com/questions/29832122/create-a-new-instance-of-component-in-spring-boot-framework) - create new instance of component in spring boot \ No newline at end of file +- [Stack Overflow](https://stackoverflow.com/questions/29832122/create-a-new-instance-of-component-in-spring-boot-framework) - create new instance of component in spring boot +- [Medium](https://medium.com/@KosteRico/spring-aop-in-2021-level-up-your-logging-8d1498242ba2) - level up your logging +- [Baeldung](https://www.baeldung.com/spring-boot-logging) - spring boot logging +- [Baeldung](https://www.baeldung.com/spring-aop) - spring aop +- [Spring Docs](https://docs.spring.io/spring-framework/docs/2.0.x/reference/aop.html) - aop +- [Eclipse](https://www.eclipse.org/aspectj/doc/released/runtime-api/org/aspectj/lang/JoinPoint.html#getArgs%28%29) - JoinPoint +- [Nickolas Fisher](https://nickolasfisher.com/blog/How-to-Use-Springs-Aspect-Oriented-Programming-to-log-all-Public-Methods) - AOP to log all public methods +- [Spring Docs](https://docs.spring.io/spring-framework/reference/core/aop/ataspectj/advice.html#aop-ataspectj-around-advice) - declaring advice +- [Stack Overflow](https://stackoverflow.com/questions/44635757/spring-4-join-point-to-get-method-argument-names-and-values) - spring 4 join point to get method argument names and values +- [Stack Overflow](https://stackoverflow.com/questions/64571161/use-aop-in-a-spring-boot-app-to-log-methods-calls-and-their-parameters) - use aop in spring boot to log method calls and their parameters \ No newline at end of file diff --git a/Spring/LoggerAOP.java b/Spring/LoggerAOP.java new file mode 100644 index 0000000..45dc2c0 --- /dev/null +++ b/Spring/LoggerAOP.java @@ -0,0 +1,48 @@ +import org.aspectj.lang.*; +import org.aspectj.lang.annotation.*; +import org.springframework.context.annotation.*; +import org.springframework.stereotype.Component; +import org.springframework.beans.factory.annotation.*; +import java.lang.annotation.*; +import java.util.*; + +@Aspect +@Component +public class LoggerAOP { + @Autowired private Logger logger; + + @Around("execution(public * *(..)) && @annotation(LogExecution)") + public void loggingAdvice(JoinPoint jp) { + String method = jp.getSignature().getName(); + logger.log(method); + } +} + +@Component +class NameRepository { + @LogExecution + public List getNames() { + List names = new ArrayList<>(); + names.add("John"); + names.add("Mary"); + return names; + } +} + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@interface LogExecution {} + +interface Logger { + public void log(String data); +} + +@Configuration +@EnableAspectJAutoProxy +@Import({LoggerAOP.class, NameRepository.class}) +class Config { + @Bean + public Logger logger() { + return (message) -> System.out.println(message); + } +} \ No newline at end of file