Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d5ebdae

Browse files
Part 25: Add SecondAspectAndFirstAdvice.java in myfirst-logging-spring-boot-starter module
1 parent 7491667 commit d5ebdae

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package spring.oldboy.logging.aop;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.annotation.*;
6+
import org.springframework.transaction.annotation.Transactional;
7+
8+
@Slf4j
9+
@Aspect
10+
public class SecondAspectAndFirstAdvice {
11+
12+
/*
13+
Lesson 119:
14+
Аннотация с параметрами - @Before("execution(public * spring.oldboy.service.*Service.findById(*))"),
15+
равносильна той, что применена ниже, но менее наглядна.
16+
17+
И так, мы планируем, что данный Advice будет внедрен перед каждым запуском методов .findById(*) из
18+
пакета service, согласно нами же заданному срезу - pointcut.
19+
20+
Тут и далее мы будем доставать один и тот же pointcut метод из FirstAspect.java
21+
*/
22+
@Before("spring.oldboy.logging.aop.FirstAspect.anyFindByIdServiceMethod()")
23+
public void addLogging() {
24+
log.info("BEFORE invoked findById method without param");
25+
}
26+
27+
/* Lesson 120: Используя точки среза напрямую в параметрах аннотации получаем нужные нам параметры */
28+
@Before(value = "spring.oldboy.logging.aop.FirstAspect.anyFindByIdServiceMethod() " +
29+
"&& args(id) " +
30+
"&& target(service) " +
31+
"&& this(serviceProxy)" +
32+
"&& @within(transactional)",
33+
argNames = "joinPoint,id,service,serviceProxy,transactional")
34+
public void addLoggingWithJoinPointAndParam(JoinPoint joinPoint,
35+
Object id,
36+
Object service,
37+
Object serviceProxy,
38+
Transactional transactional) {
39+
log.info("BEFORE invoked findById method in class {}, with id {}", service, id);
40+
}
41+
42+
/*
43+
Свойство 'returning' определяет наш возвращаемый объект используя
44+
стандартный синтаксис pointcut-ов мы этого сделать не можем.
45+
*/
46+
@AfterReturning(value = "spring.oldboy.logging.aop.FirstAspect.anyFindByIdServiceMethod() " +
47+
"&& target(service)",
48+
returning = "result")
49+
public void addLoggingAfterReturning(Object result, Object service) {
50+
log.info("AFTER RETURNING - invoked findById method in class {}, result {}", service, result);
51+
}
52+
53+
/*
54+
В данном advice-е у нас есть доступ к исключениям 'throwing',
55+
передаем его, как параметр в метод и используем для записи в
56+
лог.
57+
*/
58+
@AfterThrowing(value = "spring.oldboy.logging.aop.FirstAspect.anyFindByIdServiceMethod() " +
59+
"&& target(service)",
60+
throwing = "exception")
61+
public void addLoggingAfterThrowing(Throwable exception, Object service) {
62+
log.info("AFTER THROWING - invoked findById method in class {}, exception {}: {}", service,
63+
exception.getClass(),
64+
exception.getMessage());
65+
}
66+
67+
/*
68+
В данном advice-е у нас нет доступа ни к исключениям, ни к
69+
возвращаемым методом объектам, их может просто не быть.
70+
*/
71+
@After("spring.oldboy.logging.aop.FirstAspect.anyFindByIdServiceMethod() && target(service)")
72+
public void addLoggingAfterFinally(Object service) {
73+
log.info("AFTER (FINALLY) - invoked findById method in class {}", service);
74+
}
75+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /