Spring AOP
: μ€νλ§ νλ μμν¬μμ ν΅μ¬ λΉμ¦λμ€ λ‘μ§κ³Ό ν‘λ¨μ κ΄μ¬μ¬λ₯Ό λΆλ¦¬νλ κΈ°λ₯
Aspect Oriented Programming
Spring AOPλ₯Ό μ¬μ©νλ©΄ μ½λμ μ€λ³΅μ μ€μ΄κ³ , κ΄μ¬μ¬λ₯Ό λͺ¨λννμ¬ μ μ§λ³΄μμ±μ ν₯μμν€λ©°, μ ν리μΌμ΄μ
μ ν΅μ¬ λ‘μ§κ³Ό κ΄μ¬μ¬λ₯Ό λΆλ¦¬νλ λ±μ μ΄μ μ μ»μ μ μμ΅λλ€.
Spring AOPμμ μλΉμ€ λ μ΄μ΄μ λ©μλμ μ μ©λλ λ€μν μ΄λλ°μ΄μ€λ₯Ό μ μνμ¬ λ‘κΉ
λ° μμΈ μ²λ¦¬μ κ°μ λ€μν μμ
μ μνν©λλ€. μ΄λ₯Ό ν΅ν΄ μ ν리μΌμ΄μ
μ λμμ μΆμ νκ³ λͺ¨λν°λ§ν μ μμΌλ©°, μμΈ μ²λ¦¬λ₯Ό μννμ¬ μμ μ±μ μ μ§ν μ μμ΅λλ€.
AOP μ£Όμ μ©μ΄
μ©μ΄ | μ€λͺ
|
μ΄λλ°μ΄μ€(Advice) | ν‘λ¨ κ΄μ¬μ¬λ₯Ό μμ±ν λ©μλ |
κ΄μ (Aspect) | μ‘°μΈ ν¬μΈνΈμ ν¬μΈνΈμ»·μ μ μν μ΄λλ°μ΄μ€λ€μ λͺ¨μλμ ν΄λμ€ |
μ‘°μΈν¬μΈνΈ (Join Point) | μ΄λλ°μ΄μ€λ₯Ό νκ²μ μ μ©νλ νμ΄λ° |
ν¬μΈνΈμ»· (Pointcut) | μ΄λλ°μ΄μ€ μ μ© μ‘°κ±΄ |
νκ² (Target) | μ΄λλ°μ΄μ€κ° μ μ©λλ ν΄λμ€ |
νλ‘μ(Proxy), 리νλ μ
(Reflection)
Spring AOPλ μλ° μΈμ΄μ νλ‘μμ 리νλ μ
μ νμ©νμ¬ AOPλ₯Ό ꡬνν©λλ€.
Spring AOP μ μ© λ°©λ²
1.
μμ‘΄μ± μ€μ
a.
Spring AOP
b.
aspectjrt
c.
aspectjweaver
2.
Aspect ν΄λμ€ μ μ
a.
@EnableAspectJAutoProxy
b.
@Component
c.
@Aspect
3.
ν¬μΈνΈμ»· μ μ
a.
μ΄λλ°μ΄μ€ λ©μλμ ν¬μΈνΈμ»· ννμ μ μ
b.
ν¬μΈνΈμ»· λ©μλ μ μ
4.
μ΄λλ°μ΄μ€ μ μ
a.
JoinPoint
b.
ProceedingJoinPoint
μμ μμ
β’
μμ‘΄μ± μ€μ
β¦
Spring AOP
β¦
aspectj
βͺ
aspectjrt
βͺ
aspectjweaver
β’
Spring AOP νμ±ν
β’
λΉμ¦λμ€ λ‘μ§ μ½λ μμ±
β’
Aspect ν΄λμ€ μ½λ μμ±
β’
ν¬μΈνΈμ»· μ μ
β’
μ΄λλ°μ΄μ€ μ μ
μμ‘΄μ± μ€μ
β’
pom.xml
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.3.10</org.springframework-version>
<org.aspectj-version>1.9.7</org.aspectj-version>
<!-- μλ λ²μ μΌλ‘ νμ λ, @AfterReturning μμ returning μ΄ κ³μ null μΈ μ΄μ -->
<!-- <org.springframework-version>5.0.7.RELEASE</org.springframework-version> -->
<!-- <org.aspectj-version>1.5.4</org.aspectj-version> -->
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
Java
볡μ¬
<!-- Spring AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Aspect -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- μμ μ΄λ―Έ μλ€. -->
<!-- <dependency> -->
<!-- <groupId>org.aspectj</groupId> -->
<!-- <artifactId>aspectjrt</artifactId> -->
<!-- <version>${org.aspectj-version}</version> -->
<!-- <scope>runtime</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${org.aspectj-version}</version>
<scope>runtime</scope>
</dependency>
XML
볡μ¬
νλΌλ―Έν° μΆλ ₯ μλλ μ΄μ
β’
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
XML
볡μ¬
pom.xml μ 체μ½λ
β’
νλ‘μ νΈ μ€μ , μμ‘΄μ± μ€μ , νλ¬κ·ΈμΈ μ€μ
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.joeun</groupId>
<artifactId>test</artifactId>
<name>Spring-AOP</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.8</java-version>
<org.springframework-version>5.3.10</org.springframework-version>
<org.aspectj-version>1.9.7</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<!-- Spring AOP -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- AOP -->
<!-- μμ μ΄λ―Έ μλ€. -->
<!-- <dependency> -->
<!-- <groupId>org.aspectj</groupId> -->
<!-- <artifactId>aspectjrt</artifactId> -->
<!-- <version>${org.aspectj-version}</version> -->
<!-- <scope>runtime</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${org.aspectj-version}</version>
<scope>runtime</scope>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
XML
볡μ¬
Spring AOP νμ±ν
β’
root-context.xml
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
XML
볡μ¬
namespaces μμ aop 체ν¬λ°μ€ μ§μ
λΉμ¦λμ€ λ‘μ§ μ½λ μμ±
β’
Board.java
β’
BoardService.java
β’
BoardServiceImpl.java
β’
BoardDAO.java
β’
HomeController.java
Board.java
package com.joeun.test.dto;
import java.util.Date;
import lombok.Data;
@Data
public class Board {
private int boardNo;
private String title;
private String writer;
private String content;
private Date regDate;
private Date updDate;
public Board() {
}
public Board(String title, String writer, String content) {
this.title = title;
this.writer = writer;
this.content = content;
}
}
Java
볡μ¬
BoardService.java
package com.joeun.test.service;
import java.util.List;
import com.joeun.test.dto.Board;
public interface BoardService {
// κ²μκΈ λͺ©λ‘
public List<Board> list() throws Exception;
// κ²μκΈ μ‘°ν
public Board select(int boardNo);
// κ²μκΈ λ±λ‘
public int insert(Board board) throws Exception;
// κ²μκΈ μμ
public int update(Board board) throws Exception;
// κ²μκΈ μμ
public int delete(int boardNo) throws Exception;
}
Java
볡μ¬
BoardServiceImpl.java
package com.joeun.test.service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.joeun.test.HomeController;
import com.joeun.test.dao.BoardDAO;
import com.joeun.test.dto.Board;
@Service // λΉμ¦λμ€ λ‘μ§μ μ²λ¦¬νλ μλΉμ€ ν΄λμ€λ‘ λΉ λ±λ‘
public class BoardServiceImpl implements BoardService {
private static final Logger logger = LoggerFactory.getLogger(BoardServiceImpl.class);
@Autowired
private BoardDAO boardDAO;
@Override
public List<Board> list() throws Exception {
List<Board> boardList = boardDAO.list();
int count = boardList.size();
return boardList;
}
@Override
public Board select(int boardNo) {
Board board = boardDAO.select(boardNo);
return board;
}
@Override
public int insert(Board board) throws Exception {
int result = boardDAO.insert(board);
return result;
}
@Override
public int update(Board board) throws Exception {
int result = boardDAO.update(board);
return result;
}
@Override
public int delete(int boardNo) throws Exception {
int result = boardDAO.delete(boardNo);
return result;
}
}
Java
볡μ¬
BoardDAO.java
package com.joeun.test.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.joeun.test.dto.Board;
@Repository // λ°μ΄ν° μ‘μΈμ€ κ°μ²΄λ‘ λΉ λ±λ‘
public class BoardDAO {
public List<Board> list() {
List<Board> boardList = new ArrayList<Board>();
boardList.add(new Board("μ λͺ©1", "μμ±μ1", "λ΄μ©1"));
boardList.add(new Board("μ λͺ©2", "μμ±μ2", "λ΄μ©2"));
boardList.add(new Board("μ λͺ©3", "μμ±μ3", "λ΄μ©3"));
return boardList;
}
public Board select(int boardNo) {
// Board board = new Board("μ λͺ©", "μμ±μ", "λ΄μ©");
Board board = null;
return board;
}
public int insert(Board board) {
int result = 0;
return result;
}
public int update(Board board) {
int result = 0;
return result;
}
public int delete(int boardNo) {
int result = 0;
return result;
}
}
Java
볡μ¬
HomeController.java
package com.joeun.test;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.joeun.test.dto.Board;
import com.joeun.test.service.BoardService;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@Autowired
private BoardService boardService;
/**
* Simply selects the home view to render by returning its name.
* @throws Exception
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) throws Exception {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
// κ²μκΈ λͺ©λ‘ μμ²
List<Board> boardList = boardService.list();
model.addAttribute("boardList", boardList );
// κ²μκΈ μ‘°ν μμ²
Board board = boardService.select(10);
if( board == null )
logger.info("board null");
model.addAttribute("board", board);
// κ²μκΈ λ±λ‘ μμ²
Integer result = boardService.insert(board);
return "home";
}
}
Java
볡μ¬
Aspect ν΄λμ€ μ½λ μμ±
package com.joeun.test.common.aop;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import com.joeun.test.dto.Board;
@EnableAspectJAutoProxy
@Component
@Aspect
public class LoggingAspect {
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
/**
* μ΄λ λ°μ΄μ€ μ ν
* - Around
* - Before
* - After
* - AfterReturning
* - AfterThrowing
*/
/**
* ν¬μΈνΈμ»· ννμμ
* - μ§μμ : execution
* - λ°νκ°
* - ν¨ν€μ§
* - ν΄λμ€
* - λ©μλ
* - μΈμ
*/
@Before("execution(* com.joeun.test.service.BoardService*.*(..))")
public void before(JoinPoint jp) {
// jp.getSignature() : νκ² λ©μλμ μκ·Έμ² μ 보(λ°ννμ
ν¨ν€μ§.ν΄λμ€.λ©μλ) λ°ν
// jp.getArgs() : νκ² λ©μλμ 맀κ°λ³μλ₯Ό λ°ν
logger.info("===================================================================");
logger.info("[@Before] #########################################################");
logger.info("target : " + jp.getTarget().toString());
logger.info("signature : " + jp.getSignature());
logger.info("args : " + Arrays.toString(jp.getArgs()) );
// νλΌλ―Έν° μΆλ ₯
printParam(jp);
logger.info("===================================================================\n");
}
@After("execution(* com.joeun.test.service.BoardService*.*(..))")
public void after(JoinPoint jp) {
logger.info("===================================================================");
logger.info("[@After] ##########################################################");
logger.info("target : " + jp.getTarget().toString());
logger.info("signature : " + jp.getSignature());
logger.info("args : " + Arrays.toString(jp.getArgs()) );
// νλΌλ―Έν° μΆλ ₯
printParam(jp);
logger.info("===================================================================\n");
}
/*
* @Around μ νμ μ μ©νλ©΄, @After μ΄λλ°μ΄μ€λ μ€νλμ§ μλλ€.
*
* ProceedingJoinPoint : μ΄λλ°μ΄μ€μμ λμ λ©μλμ μ€νμ μ μ΄νκ³ νΈμΆνλ κ°μ²΄
* - proceed() : λμ λ©μλ νΈμΆ
*/
@Around("execution(* com.joeun.test.service.BoardService*.*(..))")
public Object around(ProceedingJoinPoint jp) {
logger.info("===================================================================");
logger.info("[@Around] #########################################################");
logger.info("target : " + jp.getTarget().toString());
logger.info("signature : " + jp.getSignature());
logger.info("args : " + Arrays.toString(jp.getArgs()) );
LocalDateTime time = LocalDateTime.now();
logger.info("νμ¬ μκ° : " + time);
Object result = null;
try {
result = jp.proceed();
if( result != null )
logger.info("λ°νκ° : " + result.toString());
} catch (Throwable e) {
logger.error("μμΈκ° λ°μνμ΅λλ€.");
e.printStackTrace();
}
// After, Around λ₯Ό ν¨κ» μ¬μ©νλ €λ©΄, Around μ΄λλ°μ΄μ€ μμ After μ΄λλ°μ΄μ€λ₯Ό νΈμΆν΄μ€λ€.
after(jp);
logger.info("===================================================================\n");
return result;
}
// pointcut : ν¬μΈνΈμ»· ννμ
// returning : νκ² λ©μλμ λ°νκ°μ μ μ₯νλ 맀κ°λ³μλͺ
μ§μ
@AfterReturning(pointcut = "execution(* com.joeun.test.service.BoardServiceImpl.*(..))", returning = "result")
public Object afterReturning(JoinPoint jp, Object result) {
logger.info("===================================================================");
logger.info("[@AfterReturning] #################################################");
logger.info("target : " + jp.getTarget().toString());
logger.info("signature : " + jp.getSignature());
logger.info("args : " + Arrays.toString(jp.getArgs()) );
// νλΌλ―Έν° μΆλ ₯
printParam(jp);
// λ°νκ° μΆλ ₯
if( result != null )
logger.info("λ°νκ° : " + result.toString());
logger.info("===================================================================\n");
return result;
}
@AfterThrowing(pointcut = "execution(* com.joeun.test.service.BoardService*.*(..))", throwing ="exception")
public void afterThrowing(JoinPoint jp, Exception exception) {
logger.info("===================================================================");
logger.info("[@AfterThrowing] ##################################################");
logger.info("target : " + jp.getTarget().toString());
logger.info("signature : " + jp.getSignature());
logger.info( exception.toString() );
logger.info("===================================================================\n");
}
/**
* νλΌλ―Έν° μΆλ ₯
* @param jp
*/
public void printParam(JoinPoint jp) {
Signature signature = jp.getSignature();
// νκ² λ©μλμ νλΌλ―Έν° μ΄λ¦ κ°μ Έμ€κΈ°
String[] parameterNames = ((MethodSignature) signature).getParameterNames();
// νκ² λ©μλμ μΈμ κ°μ Έμ€κΈ°
Object[] args = jp.getArgs();
// νλΌλ―Έν° μ΄λ¦κ³Ό κ°μ μΆλ ₯
if( parameterNames != null )
for (int i = 0; i < parameterNames.length; i++) {
String paramName = parameterNames[i];
Object paramValue = args[i];
logger.info("νλΌλ―Έν°λͺ
: " + paramName + ", κ°: " + paramValue);
}
}
}
Java
볡μ¬
λ‘κ·Έ κ²°κ³Ό
β’
BoardService.list()
===================================================================
[@Around] #########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : List com.joeun.test.service.BoardService.list()
args : []
νμ¬ μκ° : 2023-10-14T12:02:11.680185900
===================================================================
[@Before] #########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : List com.joeun.test.service.BoardService.list()
args : []
===================================================================
===================================================================
[@AfterReturning] #################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : List com.joeun.test.service.BoardService.list()
args : []
λ°νκ° : [Board(boardNo=0, title=μ λͺ©1, writer=μμ±μ1, content=λ΄μ©1, regDate=null, updDate=null), Board(boardNo=0, title=μ λͺ©2, writer=μμ±μ2, content=λ΄μ©2, regDate=null, updDate=null), Board(boardNo=0, title=μ λͺ©3, writer=μμ±μ3, content=λ΄μ©3, regDate=null, updDate=null)]
===================================================================
===================================================================
[@After] ##########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : List com.joeun.test.service.BoardService.list()
args : []
===================================================================
λ°νκ° : [Board(boardNo=0, title=μ λͺ©1, writer=μμ±μ1, content=λ΄μ©1, regDate=null, updDate=null), Board(boardNo=0, title=μ λͺ©2, writer=μμ±μ2, content=λ΄μ©2, regDate=null, updDate=null), Board(boardNo=0, title=μ λͺ©3, writer=μμ±μ3, content=λ΄μ©3, regDate=null, updDate=null)]
===================================================================
[@After] ##########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : List com.joeun.test.service.BoardService.list()
args : []
===================================================================
===================================================================
Java
볡μ¬
β’
BoardService.select(int)
===================================================================
[@Around] #########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Board com.joeun.test.service.BoardService.select(int)
args : [10]
νμ¬ μκ° : 2023-10-14T12:02:11.681185800
===================================================================
[@Before] #########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Board com.joeun.test.service.BoardService.select(int)
args : [10]
νλΌλ―Έν°λͺ
: boardNo, κ°: 10
===================================================================
===================================================================
[@AfterReturning] #################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Board com.joeun.test.service.BoardService.select(int)
args : [10]
νλΌλ―Έν°λͺ
: boardNo, κ°: 10
λ°νκ° : Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)
===================================================================
===================================================================
[@After] ##########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Board com.joeun.test.service.BoardService.select(int)
args : [10]
νλΌλ―Έν°λͺ
: boardNo, κ°: 10
===================================================================
λ°νκ° : Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)
===================================================================
[@After] ##########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Board com.joeun.test.service.BoardService.select(int)
args : [10]
νλΌλ―Έν°λͺ
: boardNo, κ°: 10
===================================================================
Java
볡μ¬
β’
BoardService.insert(Board)
===================================================================
[@Around] #########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Integer com.joeun.test.service.BoardService.insert(Board)
args : [Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)]
νμ¬ μκ° : 2023-10-14T12:02:11.682188500
===================================================================
[@Before] #########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Integer com.joeun.test.service.BoardService.insert(Board)
args : [Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)]
νλΌλ―Έν°λͺ
: board, κ°: Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)
===================================================================
===================================================================
[@AfterReturning] #################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Integer com.joeun.test.service.BoardService.insert(Board)
args : [Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)]
νλΌλ―Έν°λͺ
: board, κ°: Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)
λ°νκ° : 1
===================================================================
===================================================================
[@After] ##########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Integer com.joeun.test.service.BoardService.insert(Board)
args : [Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)]
νλΌλ―Έν°λͺ
: board, κ°: Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)
===================================================================
λ°νκ° : 1
===================================================================
[@After] ##########################################################
target : com.joeun.test.service.BoardServiceImpl@6c624938
signature : Integer com.joeun.test.service.BoardService.insert(Board)
args : [Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)]
νλΌλ―Έν°λͺ
: board, κ°: Board(boardNo=0, title=μ λͺ©, writer=μμ±μ, content=λ΄μ©, regDate=null, updDate=null)
===================================================================
=========================================================
Java
볡μ¬
JoinPoint
Spring AOPμμ νκ² λ©μλμ μ 보μ μ μ΄λ₯Ό μ 곡νλ μΈν°νμ΄μ€
μ κ·Ό κ°λ₯ν μ 보
β’
μ€ν μ€μΈ λ©μλμ μκ·Έλμ² (λ©μλ μ΄λ¦, λ¦¬ν΄ νμ
λ±)
β’
λ©μλμ νλΌλ―Έν°
β’
νκ² κ°μ²΄ (μ€μ λ‘ λ©μλκ° νΈμΆλλ κ°μ²΄)
β’
κΈ°ν μ€ν κ΄λ ¨ μ 보
λ©μλ | μ€λͺ
|
getSignature() | μ€ν μ€μΈ λ©μλμ μκ·Έλμ² μ 보λ₯Ό κ°μ Έμ΅λλ€. |
getTarget() | μ€ν μ€μΈ λ©μλμ νκ² κ°μ²΄(μ€μ λ‘ λ©μλλ₯Ό νΈμΆνλ κ°μ²΄)λ₯Ό κ°μ Έμ΅λλ€. |
getArgs() | μ€ν μ€μΈ λ©μλμ μΈμ(νλΌλ―Έν°) λ°°μ΄μ κ°μ Έμ΅λλ€. |
toLongString() | JoinPointμ λν μμΈν λ¬Έμμ΄ ννμ λ°νν©λλ€. |
toShortString() | JoinPointμ λν κ°λ΅ν λ¬Έμμ΄ ννμ λ°νν©λλ€. |
getThis() | JoinPointλ₯Ό νΈμΆν νλ‘μ(λκ° λμ κ°μ²΄)λ₯Ό λ°νν©λλ€. |
getKind() | JoinPointμ μ’
λ₯(λ©μλ νΈμΆ, μμ±μ νΈμΆ λ±)λ₯Ό λ°νν©λλ€. |
getStaticPart() | JoinPointμ μ μ λΆλΆ(μκ·Έλμ²)μ λ°νν©λλ€. |
getSourceLocation() | JoinPointμ μμ€ μμΉ(λ©μλκ° μ μΈλ μμΉ)λ₯Ό λ°νν©λλ€. |
μκ·Έλμ² μ 보
리ν΄νμ
ν¨ν€μ§.ν΄λμ€λͺ
.λ©μλλͺ
(맀κ°λ³μνμ
1, 맀κ°λ³μνμ
2, ...)
Java
볡μ¬
void com.joeun.test.service.BoardService.insert(Board)
Java
볡μ¬
ProceedingJoinPoint
JoinPoint μΈν°νμ΄μ€λ₯Ό μμν μΈν°νμ΄μ€.
proceed()λ‘ νκ² λ©μλλ₯Ό μ§μ νΈμΆν μ μμ΄ νκ² λ©μλμ λν μμΈμ²λ¦¬ν μ μλ€. μ£Όλ‘ @Around μ΄λλ°μ΄μ€μμ μ¬μ©νλ€.
@Around("execution(* com.joeun.test.service.BoardService*.*(..))")
public void aroundLog(ProceedingJoinPoint jp) {
logger.info("===========================================");
logger.info("Service - @Around");
LocalDateTime time = LocalDateTime.now();
logger.info("νμ¬ μκ° : " + time);
try {
Object result = jp.proceed();
} catch (Throwable e) {
logger.error("μμΈκ° λ°μνμ΅λλ€.");
e.printStackTrace();
}
// After, Around λ₯Ό ν¨κ» μ¬μ©νλ €λ©΄, Around μ΄λλ°μ΄μ€ μμ After μ΄λλ°μ΄μ€λ₯Ό νΈμΆν΄μ€λ€.
endLog(jp);
}
Java
볡μ¬
μ΄λλ°μ΄μ€ μ ν
β’
@Around : νΈμΆ μ ν λ° μμΈ μ²λ¦¬λ₯Ό μν
β’
@Before : μ μ²λ¦¬ μμ
μ μν
β’
@After : νμ²λ¦¬ μμ
μ μν
β’
@AfterReturning : λ©μλμ λ°νκ°μ νμΈ
β’
@AfterThrowing
1.
@Around
β’
@Around μ΄λλ°μ΄μ€λ λμ λ©μλμ μ€νμ μμ ν μ μ΄νλ©°, λ©μλ νΈμΆ μ ν λ° μμΈ μ²λ¦¬λ₯Ό μνν μ μμ΅λλ€.
β’
μ£Όλ‘ λμ λ©μλ μ€ν μ νμ μΆκ° μμ
μ μννκ±°λ, λ©μλ μ€νμ μ€λ¨νκ±°λ λ³κ²½νλ €λ κ²½μ°μ μ¬μ©λ©λλ€.
β’
ProceedingJoinPoint κ°μ²΄λ₯Ό μ¬μ©νμ¬ λ©μλ μ€νμ μ§μ μ μ΄ν μ μμ΅λλ€.
2.
@Before
β’
@Before μ΄λλ°μ΄μ€λ λμ λ©μλμ μ€ν μ μ μ€νλ©λλ€.
β’
μ£Όλ‘ λμ λ©μλκ° νΈμΆλκΈ° μ μ μ μ²λ¦¬ μμ
μ μννλ λ° μ¬μ©λ©λλ€.
β’
μλ₯Ό λ€μ΄, μ
λ ₯ 맀κ°λ³μλ₯Ό κ²μ¦νκ±°λ λ‘κΉ
μ μνν μ μμ΅λλ€.
3.
@After
β’
@After μ΄λλ°μ΄μ€λ λμ λ©μλμ μ€ν νμ μ€νλ©λλ€.
β’
λμ λ©μλκ° μμΈλ₯Ό λμ§λλΌλ μ€νλ©λλ€.
β’
μ£Όλ‘ λμ λ©μλ μ€ν νμ νμ²λ¦¬ μμ
μ μννλ λ° μ¬μ©λ©λλ€.
4.
@AfterReturning
β’
@AfterReturning μ΄λλ°μ΄μ€λ λμ λ©μλκ° μμΈ μμ΄ μ€νλκ³ κ²°κ³Όλ₯Ό λ°νν κ²½μ°μ μ€νλ©λλ€.
β’
μ£Όλ‘ λμ λ©μλμ λ°νκ°μ νμΈνκ³ λ‘κΉ
λλ κ²°κ³Όμ λ°λ₯Έ μΆκ° μμ
μ μννλ λ° μ¬μ©λ©λλ€.
5.
@AfterThrowing
β’
@AfterThrowing μ΄λλ°μ΄μ€λ λμ λ©μλμμ μμΈκ° λ°μν κ²½μ°μ μ€νλ©λλ€.
β’
μ£Όλ‘ μμΈ μ 보λ₯Ό λ‘κΉ
νκ±°λ μμΈμ λ°λ₯Έ μ²λ¦¬λ₯Ό μννλ λ° μ¬μ©λ©λλ€.
ν¬μΈνΈμ»· ννμ
Spring AOPμμ μ΄λ€ λ©μλλ₯Ό μ μ€ννΈμ μ μ©ν μ§ μ ννλ λ° μ¬μ©λλ ννμ
μ§μμ λ°νκ° ν¨ν€μ§ ν΄λμ€ λ©μλ 맀κ°λ³μ
μμΌλ μΉ΄λ
κΈ°νΈ | μλ―Έ |
* | λͺ¨λ λ¬Έμμ΄(ν΄λμ€, λ©μλ)μ λ체νμ¬ μ§μ ν©λλ€.
ex) * com.aloha.spring.*.*(..) |
.. | λͺ¨λ ν¨ν€μ§λ λͺ¨λ 맀κ°λ³μλ₯Ό λ체νμ¬ μ§μ ν©λλ€. |
* com.aloha.spring..*.*(..)
1 - * : λͺ¨λ λ°ν νμ
2 - com.aloha.spring : ν΄λΉ ν¨ν€μ§ κ²½λ‘λ₯Ό μ§μ
3 - .. : λͺ¨λ νμ ν¨ν€μ§λ₯Ό μ§μ
3 - * : λͺ¨λ ν΄λμ€λ₯Ό μ§μ
4 - * : λͺ¨λ λ©μλλ₯Ό μ§μ
5 - (..) : λͺ¨λ ν¨ν€μ§λ, 맀κ°λ³μλ₯Ό μ§μ
μ§μμ
β’
execution
β’
within
β’
args
β’
this
β’
target
execution
λ©μλ μ€ν μμ μ μ§μ ν©λλ€.
execution(* com.aloha.spring..*.*(..) )
Java
볡μ¬
within
νΉμ ν¨ν€μ§ λλ νμ
λ΄μμ λ©μλλ₯Ό μ νν©λλ€.
within(com.aloha.spring.service.*)
Java
볡μ¬
args
νΉμ 맀κ°λ³μ νμ
μ κ°λ λ©μλλ₯Ό μ νν©λλ€.
args(java.util.Integer)
args(java.util.String)
args(com.aloha.spring.dto.Board)
Java
볡μ¬
this
ν΄λμ€μ μΈμ€ν΄μ€ λ©μλλ₯Ό λμμΌλ‘ μ νν©λλ€.
this(com.aloha.spring.service.BoardService)
this(com.aloha.spring.dao.BoardDAO)
Java
볡μ¬
target
ν΄λμ€μ νκ² κ°μ²΄μ λ©μλλ₯Ό λμμΌλ‘ μ νν©λλ€.
@ALOHA μ½λ λ΄μ© κ²ν ν μ
λ°μ΄νΈ μμ - this? target?
this(com.aloha.spring.service.BoardServiceImpl)
this(com.aloha.spring.dao.BoardDAO)
Java
볡μ¬
λ Όλ¦¬μ°μ°μ
&& (AND), || (OR), ! (NOT) λ
Όλ¦¬ μ°μ°μ μ§μ νμ¬ ν¬μΈνΈμ»· ννμμ μ§μ ν©λλ€.
execution(* com.aloha.spring.service.BoardService*.*(..)) ||
within(com.aloha.spring.dao)
Java
볡μ¬
execution(public * *(..) ) && within(com.aloha.spring.dao)
Java
볡μ¬
!execution(* com.aloha.spring.service.CommentService*.*(..) )
Java
볡μ¬