본문 바로가기
카테고리 없음

AOP 관점지향 프로그래밍 구현하기

by 그리득 2023. 12. 12.
728x90

 

 

 

spring/pom.xml에 aspectj 라이브러리 추가

 

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${org.aspectj-version}</version>
    <scope>runtime</scope>
</dependency>

 

aspect클래스 생성

 

//공통관심기능을 관리하는 클래스
@Slf4j
public class LoggerAspect {

	//공통의 기능(메소드)을 정의
	//메소드 선언방식이 정해져있음
	//Before, After, AfterReturning...
	//void 메소드명(JoinPoint)
	
	public void loggerBefore(JoinPoint jp) {
		log.debug("------ aop before메소드 실행 ------");
		
		log.debug("================================");
	}
	
	
	//Around
	//Object 메소드명(ProceedingJoinPoint) throws Throwable
	
	
}
aop-context.xml 생성

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
      
	<!-- aop적용하기 -->
    <bean id="loggerAspect" class="com.bs.spring.common.aop.LoggerAspect"/>
    
    <aop:config>
    	<aop:aspect ref="loggerAspect">
    		<aop:pointcut expression="execution(* com.bs.spring.memo..*(..))" id="memoPoint"/>
    		<aop:before method="loggerBefore" pointcut-ref="memoPoint"/>
    	</aop:aspect>
    </aop:config>
      
</beans>

 

 

web.xml 서블릿 등록
<!-- Processes application requests -->
	<!-- 서블릿에 해당하는 것들 -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
			/WEB-INF/spring/appServlet/servlet-context.xml
			/WEB-INF/spring/appServlet/aop-context.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>