Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- jsp연결
- jsp
- 버추얼머신
- cisco packet
- html
- rinux
- VLAN
- 원형그래프
- autoset
- 자바
- 참조타입
- 정보처리기사
- Cisco
- 라우터
- 오라클
- ospf
- ciscopacket
- Java
- 네트워크관리사
- php
- sql
- 데이터베이스
- javaee
- 네트워크
- 리눅스
- w3school
- NCS
- 이것이 자바다
- 정처기필기
- Oracle
Archives
- Today
- Total
기록해! 정리해!
JoinPoint 메소드 사용하기 본문
[ Before 어드바이스 ] : 비즈니스 로직 메소드 이름과 매개변수 값 을 사용 할 수 있다.
package com.springbook.biz.common;
import org.aspectj.lang.JoinPoint;
public class BeforeAdvice {
void beforeLog(JoinPoint jp) {
String method = jp.getSignature().getName(); // 비즈니스 로직 메소드 이름 불러오기
Object[] args = jp.getArgs(); // 비즈니스 로직 메소드의 매개변수에 들어오는 args 값 읽어오기
System.out.println("[사전처리]" + method + "()메소드 ARGS 정보: " + args[0].toString() );
}
}
=====================================================================
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.springbook.biz" />
<bean id="beforeAdvice" class="com.springbook.biz.common.BeforeAdvice" />
<aop:config>
<aop:pointcut id="allPointcut"
expression="execution(* com.springbook.biz..*ServiceImpl.*(..))" />
<aop:aspect ref="beforeAdvice">
<aop:before method="beforeLog" pointcut-ref="allPointcut" />
</aop:aspect>
</aop:config>
</beans>
[ After Returning 어드바이스 ]
1) 비즈니스 로직 메소드 이름과 매개변수 값 을 사용 할 수 있다.
2) 리턴되는 값을 사용 할 수 있다.
package com.springbook.biz.common;
import org.aspectj.lang.JoinPoint;
import com.springbook.biz.user.UserVo;
public class AfterReturningAdvice {
void afterLog(JoinPoint jp, Object returnObj) {
String method=jp.getSignature().getName();
if ( returnObj instanceof UserVo) {
System.out.println("===> returnObj instanceof UserVo 확인");
UserVo user =(UserVo) returnObj;
if(user.getRole().equals("User") ) {
System.out.println(user.getName() + "로그인(Admin)");
}
}
System.out.println("[ 사후처리 ]" + method +
"() 메소드 리턴값:" + returnObj.toString() );
}
}
=======================================================================
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.springbook.biz" />
<bean id="beforeAdvice" class="com.springbook.biz.common.BeforeAdvice" />
<bean id="afterReturningAdvice" class="com.springbook.biz.common.AfterReturningAdvice" />
<aop:config>
<aop:pointcut id="allPointcut"
expression="execution(* com.springbook.biz..*ServiceImpl.*(..))" />
<aop:aspect ref="beforeAdvice">
<aop:before method="beforeLog" pointcut-ref="allPointcut" />
</aop:aspect>
<aop:aspect ref="afterReturningAdvice">
<aop:after-returning method="afterLog"
returning="returnObj" pointcut-ref="allPointcut" />
</aop:aspect>
</aop:config>
</beans>
[ Around 어드바이스 ]
package com.springbook.biz.common;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;
public class AroundAdvice {
public Object aroundLog( ProceedingJoinPoint pjp) throws Throwable {
String method = pjp.getSignature().getName();
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object returnObj =pjp.proceed();
stopWatch.stop();
System.out.println( method + "() 메서드 수행에 걸린 시간 : " + stopWatch.getTotalTimeMillis() + "(ms)초" );
return returnObj;
}
}
==========================================================================
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:component-scan base-package="com.springbook.biz" />
<bean id="beforeAdvice" class="com.springbook.biz.common.BeforeAdvice" />
<bean id="afterReturningAdvice" class="com.springbook.biz.common.AfterReturningAdvice" />
<bean id="aroundAdvice" class="com.springbook.biz.common.AroundAdvice" />
<aop:config>
<aop:pointcut id="allPointcut"
expression="execution(* com.springbook.biz..*ServiceImpl.*(..))" />
<!--
<aop:aspect ref="beforeAdvice">
<aop:before method="beforeLog" pointcut-ref="allPointcut" />
</aop:aspect>
<aop:aspect ref="afterReturningAdvice">
<aop:after-returning method="afterLog"
returning="returnObj" pointcut-ref="allPointcut" />
</aop:aspect>
-->
<aop:aspect ref="aroundAdvice">
<aop:around method="aroundLog"
pointcut-ref="allPointcut" />
</aop:aspect>
</aop:config>
</beans>
'Spring' 카테고리의 다른 글
Spring JDBC 방법 (0) | 2022.09.22 |
---|---|
어노데이션 기본 AOP - Before 어드바이스 (0) | 2022.09.22 |
AOP 사용하기 (0) | 2022.09.22 |
비즈니스 컴포넌트 실습 II ( 어노테이션으로 수정 ) (0) | 2022.09.22 |
비즈니스 컴포넌트 실습 II (0) | 2022.09.22 |
Comments