AOP ์ ์ฉ
Aspect ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ๊ต์ฐจ ์ ๋จ ์์ ์ ์ํํ๋๋ก ์ค๊ณ๋์์ผ๋ฉฐ, ์ด๋ ์๋ก ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ๋ฌ ๋ฒ ๋ฐ๋ณต๋ ์ ์๋ ๋ชจ๋ ์ฝ๋๊ฐ ๋ ์ ์์ผ๋ฉฐ ๋ณ๋์ ๋ชจ๋๋ก ์์ ํ ๊ตฌ์กฐํ๋ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ AOP์์๋ ์ด๋ฅผ ๋ฉ์ธ ์ฝ๋ ์ธ๋ถ์ ์ ์งํ๊ณ ์์ง์ผ๋ก ์ ์ธํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ณด์ ์ ์ฑ ์ ์ฌ์ฉํ๋ ๊ฒ์ ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ๋ณด์์ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฌ๋ฌ ์์๋ฅผ ํตํด ์คํ๋ฉ๋๋ค. ๋ํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ณด์ ์ ์ฑ ์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ ๊ธฐ์กด ๋ถ๋ถ๊ณผ ์๋ก์ด ๋ถ๋ถ์ ๋์ผํ๊ฒ ์ ์ฉ๋์ด์ผ ํฉ๋๋ค. ๋์์ ์ฌ์ฉ ์ค์ธ ๋ณด์ ์ ์ฑ ์์ฒด๊ฐ ์งํํ ์ ์์ต๋๋ค. ์ด๊ฒ์ AOP๋ฅผ ์ฌ์ฉํ๊ธฐ์ ์๋ฒฝํ ์ฅ์์ ๋๋ค . ๋ํ ๋ ๋ค๋ฅธ ์๋ ๋ก๊น ์ ๋๋ค.. ๋ก๊น ๊ธฐ๋ฅ์ ์๋์ผ๋ก ์ถ๊ฐํ๋ ๋์ ๋ก๊น ์ ๋ํ AOP ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉํ๋ฉด ๋ช ๊ฐ์ง ์ด์ ์ด ์์ต๋๋ค.-
๋ก๊น ์ ์ํ ์ฝ๋๋ ์ฝ๊ฒ ์ถ๊ฐํ๊ณ ์ ๊ฑฐํ ์ ์์ต๋๋ค. ์ผ๋ถ ์ธก๋ฉด์ ๋ช ๊ฐ์ง ๊ตฌ์ฑ์ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
-
๋ก๊น ์ ์ํ ๋ชจ๋ ์์ค ์ฝ๋๋ ํ ๊ณณ์ ๋ณด๊ด๋๋ฏ๋ก ์ฌ์ฉ๋๋ ๋ชจ๋ ๊ณณ์ ์๋์ผ๋ก ์ถ์ ํ ํ์๊ฐ ์์ต๋๋ค.
-
๋ก๊น ์ฝ๋๋ ์ด๋ฏธ ์์ฑ๋ ๋ฉ์๋ ๋ฐ ํด๋์ค ๋๋ ์๋ก์ด ๊ธฐ๋ฅ์ ์๊ด์์ด ์ด๋์๋ ์ถ๊ฐํ ์ ์์ต๋๋ค. ์ด๊ฒ์ ์ฝ๋ฉ ์ค๋ฅ์ ์๋ฅผ ์ค์ ๋๋ค.
๋ํ ๋์์ธ ๊ตฌ์ฑ์์ ์ธก๋ฉด์ ์ ๊ฑฐํ ๋ ๋ชจ๋ ์ถ์ ์ฝ๋๊ฐ ์ฌ๋ผ์ง๊ณ ์๋ฌด ๊ฒ๋ ๋๋ฝ๋์ง ์์๋์ง ํ์ธํ ์ ์์ต๋๋ค.
- Aspect๋ ๊ณ์ํด์ ๊ฐ์ ํ๊ณ ์ฌ์ฉํ ์ ์๋ ๋ณ๋์ ์ฝ๋์ ๋๋ค.
AOP์ ๊ธฐ๋ณธ ์์น
์ด ์ฃผ์ ์์ ๋ ๋์๊ฐ๊ธฐ ์ํด ๋จผ์ AOP์ ์ฃผ์ ๊ฐ๋ ์ ์์๋ด ์๋ค. ์กฐ์ธ โ ์กฐ์ธ ํฌ์ธํธ์์ ํธ์ถ๋๋ ์ถ๊ฐ ๋ ผ๋ฆฌ ๋๋ ์ฝ๋์ ๋๋ค. ์ด๋๋ฐ์ด์ค๋ ์กฐ์ธ ํฌ์ธํธ ์ด์ , ์ดํ ๋๋ ๋์ ์ ์ํ๋ ์ ์์ต๋๋ค(์์ธํ ๋ด์ฉ์ ์๋ ์ฐธ์กฐ). ๊ฐ๋ฅํ ์กฐ์ธ ์ ํ :-
์ด์ โ ์ด ์ ํ์ ์ด๋๋ฐ์ด์ค๋ ๋์ ๋ฉ์๋, ์ฆ ์กฐ์ธ ํฌ์ธํธ๊ฐ ์คํ๋๊ธฐ ์ ์ ์คํ๋ฉ๋๋ค. aspect๋ฅผ ํด๋์ค๋ก ์ฌ์ฉํ ๋ @Before ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ์ด๋๋ฐ์ด์ค๋ฅผ ์ด์ ์ ์จ ๊ฒ์ผ๋ก ํ์ํ๋ค. aspect๋ฅผ .aj ํ์ผ๋ก ์ฌ์ฉํ ๋ ์ด๊ฒ์ before() ๋ฉ์๋๊ฐ ๋ ๊ฒ์ด๋ค .
- After โ ๋ฉ์๋(์กฐ์ธ ํฌ์ธํธ) ์คํ์ด ์๋ฃ๋ ํ ์คํ๋๋ ์ด๋๋ฐ์ด์ค์
๋๋ค. ์ ์ ์คํ ์์ ์์ธ ๋ฐ์ ์ ๋ชจ๋ ๋ง์ฐฌ๊ฐ์ง์
๋๋ค.
aspect๋ฅผ ํด๋์ค๋ก ์ฌ์ฉํ ๋ @After ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ์ด๊ฒ์ด ๋ค์ ์ค๋ ์ด๋๋ฐ์ด์ค์์ ๋ํ๋ผ ์ ์๋ค.
.aj ํ์ผ ๋ก aspect๋ฅผ ์ฌ์ฉํ ๋ ์ด๊ฒ์ after() ๋ฉ์๋์ ๋๋ค.
-
After Returning โ ์ด ์กฐ์ธ์ ๋์ ๋ฉ์๋๊ฐ ์ค๋ฅ ์์ด ์ ์์ ์ผ๋ก ์๋ฃ๋ ๋๋ง ์ํ๋ฉ๋๋ค.
๊ด์ ์ด ํด๋์ค๋ก ํํ๋ ๋ @AfterReturning ์ฃผ์์ ์ฌ์ฉํ์ฌ ์ด๋๋ฐ์ด์ค๊ฐ ์ฑ๊ณต์ ์ธ ์๋ฃ ํ ์คํ๋๋ ๊ฒ์ผ๋ก ํ์ํ ์ ์์ต๋๋ค.
aspect๋ฅผ .aj ํ์ผ๋ก ์ฌ์ฉํ ๋ ์ด๊ฒ์ (Object obj) ๋ฉ์๋๋ฅผ ๋ฐํํ๋ after() ๊ฐ ๋ฉ๋๋ค .
-
After Throwing โ ์ด ์กฐ์ธ์ ๋ฉ์๋, ์ฆ ์กฐ์ธ ํฌ์ธํธ๊ฐ ์์ธ๋ฅผ throwํ๋ ์ธ์คํด์ค๋ฅผ ์ํ ๊ฒ์ ๋๋ค. ์ด ์กฐ์ธ์ ์ฌ์ฉํ์ฌ ํน์ ์ข ๋ฅ์ ์คํจํ ์คํ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค(์: ํ์ํ ์ถ์ ์์ค์ผ๋ก ์ ์ฒด ํธ๋์ญ์ ๋๋ ๋ก๊ทธ ๋กค๋ฐฑ).
ํด๋์ค ์ธก๋ฉด์์ @AfterThrowing ์ด๋ ธํ ์ด์ ์ ์์ธ๋ฅผ ๋์ง ํ์ ์ด ์ด๋๋ฐ์ด์ค๊ฐ ์ฌ์ฉ๋์์์ ๋ํ๋ด๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค.
aspect๋ฅผ .aj ํ์ผ๋ก ์ฌ์ฉํ ๋ ์ด๊ฒ์ after() throw(์์ธ e) ๋ฉ์๋ ๊ฐ ๋ฉ๋๋ค .
-
์ฃผ๋ณ โ ์๋ง๋ ๊ฐ์ฅ ์ค์ํ ์ ํ์ ์กฐ์ธ ์ค ํ๋์ผ ๊ฒ์ ๋๋ค. ์ฆ, ์๋ฅผ ๋ค์ด ์ฃผ์ด์ง ์กฐ์ธ ํฌ์ธํธ ๋ฐฉ๋ฒ์ ์ํํ ์ง ์ฌ๋ถ๋ฅผ ์ ํํ๋ ๋ฐ ์ฌ์ฉํ ์ ์๋ ์กฐ์ธ ํฌ์ธํธ๋ฅผ ๋๋ฌ์๋๋ค.
์กฐ์ธ ํฌ์ธํธ ๋ฉ์๋๊ฐ ์คํ๋๊ธฐ ์ ๊ณผ ํ์ ์คํ๋๋ ์ด๋๋ฐ์ด์ค ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
around ์ด๋๋ฐ์ด์ค๋ ์กฐ์ธ ํฌ์ธํธ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ๋ฉ์๋๊ฐ ๋ฌด์ธ๊ฐ๋ฅผ ๋ฆฌํดํ๋ ๊ฒฝ์ฐ ๋ฆฌํด ๊ฐ์ ๋ด๋นํฉ๋๋ค. ์ฆ, ์ด ์ด๋๋ฐ์ด์ค์์๋ ํธ์ถํ์ง ์๊ณ ๋์ ๋ฉ์๋์ ์๋์ ๊ฐ๋จํ๊ฒ ์๋ฎฌ๋ ์ดํธํ๊ณ ์ํ๋ ๊ฒ์ ๋ฆฌํด ๊ฒฐ๊ณผ๋ก ๋ฆฌํดํ ์ ์์ต๋๋ค.
์ธก๋ฉด์ ํด๋์ค๋ก ์ง์ ํ๋ฉด ์กฐ์ธ ํฌ์ธํธ๋ฅผ ๋ํํ๋ ์ด๋๋ฐ์ด์ค๋ฅผ ์์ฑํ๊ธฐ ์ํด @Around ์ฃผ์์ ์ฌ์ฉํฉ๋๋ค . .aj ํ์ผ ํ์์ ์ ์คํํธ๋ฅผ ์ฌ์ฉํ ๋ ์ด ๋ฉ์๋๋ around() ๋ฉ์๋ ๊ฐ ๋ฉ๋๋ค .
-
Compile-time weaving โ aspect์ ์์ค ์ฝ๋์ aspect๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๊ฐ ์๋ค๋ฉด AspectJ ์ปดํ์ผ๋ฌ๋ฅผ ์ฌ์ฉํ์ฌ ์์ค ์ฝ๋์ aspect๋ฅผ ์ง์ ์ปดํ์ผํ ์ ์๋ค.
-
์ปดํ์ผ ํ ์๋น(๋ฐ์ด๋๋ฆฌ ์๋น) โ ์์ค ์ฝ๋ ๋ณํ์ ์ฌ์ฉํ์ฌ aspect๋ฅผ ์ฝ๋๋ก ์ฎ์ ์ ์๊ฑฐ๋ ์ํ์ง ์๋ ๊ฒฝ์ฐ ์ด์ ์ ์ปดํ์ผ๋ ํด๋์ค ๋๋ jar ํ์ผ์ ๊ฐ์ ธ ์์ aspect๋ฅผ ์ฃผ์ ํ ์ ์์ต๋๋ค.
-
Load-time weaving โ ํด๋์ค ๋ก๋๊ฐ ํด๋์ค ํ์ผ์ ๋ก๋ํ๊ณ JVM์ ๋ํ ํด๋์ค๋ฅผ ์ ์ํ ๋๊น์ง ์ง์ฐ๋๋ ๋ฐ์ด๋๋ฆฌ ์๋น์ ๋๋ค.
์ด๋ฅผ ์ง์ํ๋ ค๋ฉด ํ๋ ์ด์์ ์๋น ํด๋์ค ๋ก๋๊ฐ ํ์ํฉ๋๋ค. ๋ฐํ์์ ์ํด ๋ช ์์ ์ผ๋ก ์ ๊ณต๋๊ฑฐ๋ "์๋น ์์ด์ ํธ"์ ์ํด ํ์ฑํ๋ฉ๋๋ค.
์๋ฐ์ ์
๋ค์์ผ๋ก AOP๋ฅผ ๋ ์ ์ดํดํ๊ธฐ ์ํด ์์ "Hello World" ์คํ์ผ ์์ ๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ๋ฐฉ๋ง์ด ์ค๋ฅธ์ชฝ์์ ์ฐ๋ฆฌ์ ์์ ๋ ์ปดํ์ผ ํ์ ์๋น์ ์ฌ์ฉํ๋ค๋ ์ ์ ์ฃผ๋ชฉํ๊ฒ ์ต๋๋ค . ๋จผ์ pom.xml ํ์ผ ์ ๋ค์ ์ข ์์ฑ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค .
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.5</version>
</dependency>
์ผ๋ฐ์ ์ผ๋ก ํน๋ณํ ajc ์ปดํ์ผ๋ฌ๋ aspect๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค. IntelliJ IDEA ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํฌํจํ์ง ์์ผ๋ฏ๋ก ์ ํ๋ฆฌ์ผ์ด์
์ปดํ์ผ๋ฌ๋ก ์ ํํ ๋ 5168 75 AspectJ ๋ฐฐํฌ์ ๋ํ ๊ฒฝ๋ก๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค. ์ด๊ฒ์ด ์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ์ด์์ต๋๋ค. ๋ด๊ฐ ์ฌ์ฉํ ๋ ๋ฒ์งธ๋ pom.xml ํ์ผ์ ๋ค์ ํ๋ฌ๊ทธ์ธ์ ๋ฑ๋กํ๋ ๊ฒ์
๋๋ค.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.8</complianceLevel>
<source>1.8</source>
<target>1.8</target>
<showWeaveInfo>true</showWeaveInfo>
<<verbose>true<verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
๊ทธ๋ฐ ๋ค์ Maven ์์ ๋ค์ ๊ฐ์ ธ์ค๊ณ mvn clean compile ์ ์คํํ๋ ๊ฒ์ด ์ข์ต๋๋ค . ์ด์ ์์ ๋ก ์ง์ ์งํํ๊ฒ ์ต๋๋ค.
์ 1
๋ฉ์ธ ํด๋์ค๋ฅผ ๋ง๋ค์ด ๋ด ์๋ค . ์ฌ๊ธฐ์๋ ์ฝ์์ ์ ๋ฌ๋ ์ด๋ฆ์ ์ธ์ํ๋ ์ง์ ์ ๊ณผ ๋ฉ์๋๊ฐ ์์ต๋๋ค.
public class Main {
public static void main(String[] args) {
printName("Tanner");
printName("Victor");
printName("Sasha");
}
public static void printName(String name) {
System.out.println(name);
}
}
์ฌ๊ธฐ์๋ ๋ณต์กํ ๊ฒ์ด ์์ต๋๋ค. ์ด๋ฆ์ ์ ๋ฌํ๊ณ ์ฝ์์ ํ์ํ์ต๋๋ค. ์ง๊ธ ํ๋ก๊ทธ๋จ์ ์คํํ๋ฉด ์ฝ์์ ๋ค์์ด ํ์๋ฉ๋๋ค.
public aspect GreetingAspect {
pointcut greeting() : execution(* Main.printName(..));
before() : greeting() {
System.out.print("Hi, ");
}
}
์ด ํ์ผ์ ํด๋์ค์ ๋น์ทํฉ๋๋ค. ์ฌ๊ธฐ์ ๋ฌด์จ ์ผ์ด ์ผ์ด๋๋์ง ๋ด
์๋ค. pointcut ์ ์กฐ์ธ ํฌ์ธํธ ์งํฉ์
๋๋ค. greeting()์ ์ด ํฌ์ธํธ์ปท์ ์ด๋ฆ์
๋๋ค; : ์คํ์ Main.printName(...) ๋ฉ์๋์ ๋ชจ๋ ( * ) ํธ์ถ์ ์คํํ๋ ๋์ ์ ์ฉํ๋๋ก ๋ํ๋
๋๋ค . ๋ค์์ ๋์ ๋ฉ์๋๊ฐ ํธ์ถ๋๊ธฐ ์ ์ ์คํ๋๋ ํน์ ์ด๋๋ฐ์ด์ค( before()) ์
๋๋ค. : greeting()์ ์ด ์ด๋๋ฐ์ด์ค๊ฐ ์๋ตํ๋ ์ปทํฌ์ธํธ์
๋๋ค. ์, ์๋์์ ์ฐ๋ฆฌ๊ฐ ์ดํดํ๋ Java ์ธ์ด๋ก ์์ฑ๋ ๋ฉ์๋ ์์ฒด์ ๋ณธ๋ฌธ์ ๋ณผ ์ ์์ต๋๋ค. ์ด ์ธก๋ฉด์ด ์๋ ์ํ์์ main์ ์คํํ๋ฉด ๋ค์ ์ฝ์ ์ถ๋ ฅ์ด ํ์๋ฉ๋๋ค.
@Aspect
public class GreetingAspect{
@Pointcut("execution(* Main.printName(String))")
public void greeting() {
}
@Before("greeting()")
public void beforeAdvice() {
System.out.print("Hi, ");
}
}
.aj ์ ์คํํธ ํ์ผ ์ดํ์๋ ์ฌ๊ธฐ์์ ๋ชจ๋ ๊ฒ์ด ๋ ๋ช
ํํด์ง๋๋ค.
- @Aspect๋ ์ด ํด๋์ค๊ฐ ๊ด์ ์์ ๋ํ๋ ๋๋ค.
- @Pointcut("execution(* Main.printName(String))") ์ ์ ํ์ด String ์ธ ์ ๋ ฅ ์ธ์๋ฅผ ์ฌ์ฉํ์ฌ Main.printName ์ ๋ํ ๋ชจ๋ ํธ์ถ์ ๋ํด ํธ๋ฆฌ๊ฑฐ๋๋ ์ปทํฌ์ธํธ์ ๋๋ค .
- @Before("greeting()")์ greeting() ์ปทํฌ์ธํธ ์ ์ง์ ๋ ์ฝ๋๋ฅผ ํธ์ถํ๊ธฐ ์ ์ ์ ์ฉ๋๋ ์กฐ์ธ์ ๋๋ค .
์ 2
ํด๋ผ์ด์ธํธ๋ฅผ ์ํด ๋ช ๊ฐ์ง ์์ ์ ์ํํ๋ ๋ฉ์๋๊ฐ ์๊ณ ์ด ๋ฉ์๋๋ฅผ main ์์ ํธ์ถํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค .
public class Main {
public static void main(String[] args) {
performSomeOperation("Tanner");
}
public static void performSomeOperation(String clientName) {
System.out.println("Performing some operations for Client " + clientName);
}
}
@Around ์ฃผ์์ ์ฌ์ฉํ์ฌ "์์ฌ ๊ฑฐ๋"๋ฅผ ์์ฑํด ๋ณด๊ฒ ์ต๋๋ค.
@Aspect
public class TransactionAspect{
@Pointcut("execution(* Main.performSomeOperation(String))")
public void executeOperation() {
}
@Around(value = "executeOperation()")
public void beforeAdvice(ProceedingJoinPoint joinPoint) {
System.out.println("Opening a transaction...");
try {
joinPoint.proceed();
System.out.println("Closing a transaction...");
}
catch (Throwable throwable) {
System.out.println("The operation failed. Rolling back the transaction...");
}
}
}
ProceedingJoinPoint ๊ฐ์ฒด์ ์งํ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋๋ฐ์ด์ค์์ ์์น๋ฅผ ๊ฒฐ์ ํ๊ธฐ ์ํด ๋ํ ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค. ๋ฐ๋ผ์ ์ ๋ฉ์๋์ ์ฝ๋๋ joinPoint.proceed(); ๋ Before ์ด๊ณ ๊ทธ ์๋์ ์ฝ๋๋ After ์
๋๋ค . main ์ ์คํํ๋ฉด ์ฝ์์ ๋ค์๊ณผ ๊ฐ์ด ํ์๋ฉ๋๋ค.
public static void performSomeOperation(String clientName) throws Exception {
System.out.println("Performing some operations for Client " + clientName);
throw new Exception();
}
๊ทธ๋ฐ ๋ค์ ๋ค์ ์ฝ์ ์ถ๋ ฅ์ ์ป์ต๋๋ค.
์ 3
๋ค์ ์์์๋ ์ฝ์์ ๋ก๊ทธ์ธํ๋ ๊ฒ๊ณผ ๊ฐ์ ์์ ์ ์ํํด ๋ณด๊ฒ ์ต๋๋ค. ๋จผ์ ์ ์ฌ ๋น์ฆ๋์ค ๋ก์ง์ ์ถ๊ฐํ Main ์ ์ดํด๋ณด์ธ์ .
public class Main {
private String value;
public static void main(String[] args) throws Exception {
Main main = new Main();
main.setValue("<some value>");
String valueForCheck = main.getValue();
main.checkValue(valueForCheck);
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
public void checkValue(String value) throws Exception {
if (value.length() > 10) {
throw new Exception();
}
}
}
main ์์ setValue ๋ฅผ ์ฌ์ฉํ์ฌ value ์ธ์คํด์ค ๋ณ์ ์ ๊ฐ์ ํ ๋นํฉ๋๋ค . ๊ทธ๋ฐ ๋ค์ getValue๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ๊ฐ์ ธ์จ ๋ค์ checkValue๋ฅผ ํธ์ถํ์ฌ 10์๋ณด๋ค ๊ธด์ง ํ์ธํฉ๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด์ ๋ฉ์๋ ์์
์ ๊ธฐ๋กํ๋ ๋ฐ ์ฌ์ฉํ ์ธก๋ฉด์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
@Aspect
public class LogAspect {
@Pointcut("execution(* *(..))")
public void methodExecuting() {
}
@AfterReturning(value = "methodExecuting()", returning = "returningValue")
public void recordSuccessfulExecution(JoinPoint joinPoint, Object returningValue) {
if (returningValue != null) {
System.out.printf("Successful execution: method โ %s method, class โ %s class, return value โ %s\n",
joinPoint.getSignature().getName(),
joinPoint.getSourceLocation().getWithinType().getName(),
returningValue);
}
else {
System.out.printf("Successful execution: method โ %s, class โ %s\n",
joinPoint.getSignature().getName(),
joinPoint.getSourceLocation().getWithinType().getName());
}
}
@AfterThrowing(value = "methodExecuting()", throwing = "exception")
public void recordFailedExecution(JoinPoint joinPoint, Exception exception) {
System.out.printf("Exception thrown: method โ %s, class โ %s, exception โ %s\n",
joinPoint.getSignature().getName(),
joinPoint.getSourceLocation().getWithinType().getName(),
exception);
}
}
๋ฌด์จ ์ผ์ด์ผ? @Pointcut("execution(* *(..))") ์ ๋ชจ๋ ๋ฉ์๋์ ๋ชจ๋ ํธ์ถ์ ์ฐธ์ฌํฉ๋๋ค. @AfterReturning(value = "methodExecuting()", Returning = "returningValue")๋ ๋์ ๋ฉ์๋๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์คํ๋ ํ ์คํ๋ ์ด๋๋ฐ์ด์ค์
๋๋ค. ์ฌ๊ธฐ์๋ ๋ ๊ฐ์ง ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค.
- ๋ฉ์๋์ ๋ฐํ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ โ if (returningValue! = Null) {
- ๋ฐํ ๊ฐ์ด ์์ ๋ โ else {
GO TO FULL VERSION