Spring Aspectj 사용

2254 단어 springAOPbean
Spring Aop 을 배 웠 는데 Aspectj 가 좋 은 것 같 아서 적어 서 공유 해 드 리 겠 습 니 다.
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

<!--   @Aspect      -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--     -->
<bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"></bean>

 
package cn.itcast.service;

import org.aspectj.lang.ProceedingJoinPoint;
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.annotation.Pointcut;

//              
@Aspect
public class MyInterceptor {

	//              
	@Pointcut("execution (* cn.itcast.service.impl..*.*(..))")
	private void anyMethod(){}//           	

	@Before("anyMethod()")//      
	public void doAccessCheck() {
		System.out.println("    ");
	}	
	
	@After("anyMethod()")
	public void doAfter()//      
	{
		System.out.println("    ");
	}

	@AfterReturning(pointcut="anyMethod()")
	public void doAfterReturning()//      
	{
		System.out.println("    ");
	}
	//  :                  ,        
	
	@AfterThrowing(pointcut="anyMethod()", throwing="ex")//      
    public void doExceptionAction(Exception ex) {
	System.out.println("    ");
	}

	@Around("anyMethod()")
	//      ,          
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable
	{
		System.out.println("      ");
		Object result=pjp.proceed();
		System.out.println("      ");
		return result;
	}
	
	
}

좋은 웹페이지 즐겨찾기