aop 예제

FRAMEWORK/Spring 2015. 8. 24. 19:30 |

AOP 예제 

 

XML 형식 aop 

sevlet-context.xml 에 

맨위 <beans>에 xmlns:aop="http://www.springframework.org/schema/aop" 추가 한다.


	<!-- XML 형식의 AOP -->
	<beans:bean id="loggingAspect" class="com.iuom.www.aop.LoggingAspect"/>
	
	<aop:config>
	    <aop:pointcut expression="execution(* com.iuom.www..*DAO.*(..))" id="loggingAspectPointCut"/>
	    <aop:aspect ref="loggingAspect">
	        <aop:before method="beforeMethod" pointcut-ref="loggingAspectPointCut"/>
	        <aop:after method="afterMethod" pointcut-ref="loggingAspectPointCut"/>
	        <aop:after-throwing method="errorMethod" throwing="exception" pointcut-ref="loggingAspectPointCut" />
	    </aop:aspect>
	</aop:config>



com.iuom.www..*DAO.*(..)) 이하에 모든 클래스에 pointcut 지정


com.iuom.www..*DAO.*(..)) 이하 클래스는 메소드를 실행하기전에 

aop:before 어드바이스에 의해  loggingAspect 의  beforeMethod 메소드 실행


com.iuom.www..*DAO.*(..)) 이하 클래스는 메소드 실행후

aop:after 어드바이스에 의해 loggingAspect의 afterMethod 실행


aop:after-throwing 에 의해 com.iuom.www..*DAO.*(..) 이하 모든 클래스 메소드가 실행후 에러가 나면

loggingAspect 의 errorMethod 실행




어노테이션 이용한 AOP

@Aspect
@Configuration
public class LoggingAnnotation {

	final private Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
	
	@Pointcut("execution(* com.iuom.www..*Service.*(..))")
	private void pointMethod() {}
	
	@Before("pointMethod()")
	public void testMethod() {
		logger.info("Aspect 로그 Service 체크 시작 ----->");
	}
}




'FRAMEWORK > Spring' 카테고리의 다른 글

Spring scheduler 설정 및 사용  (0) 2016.04.12
스프링 이클립스 연동하기  (0) 2015.10.20
aop  (0) 2015.08.23
Spring @PathVariable 이용하여 parameter 를 url 형식으로 받기  (0) 2015.08.13
[Spring] ehcache 캐싱하기  (0) 2015.08.12
Posted by 양승아
: