@AspectJベースSpring AOP使用


[list=1]
  • springのxmlプロファイルでaspectj
  • を次のように構成します.
    
    <aop:aspectj-autoproxy />
    
  • 注釈を作成する定義
  • は次のとおりです.
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface AroundPointCut {
    	boolean accessRead() default false;
    }
    
  • 切断面の宣言、切り込み点の宣言、ターゲットオブジェクトに対するブロック方法の呼び出し
  • は、次のようになります.
    
    <bean id="loggerAspect" class="com.aspect.AroundAspect"></bean>
    
    
    @Aspect //     
    public class AroundAspect {
    
    	//      
    	@Pointcut("execution(@com.crane.aspect.AroundPointCut public * * (..))")
    	public void aroundPointCut() {
    
    	}
    
    	//            
    	@Around("com.crane.aspect.LoggerAspect.aroundPointCut()")
    	public Object doLoggerPointCut(ProceedingJoinPoint jp) throws Throwable {
    		//             
    		MethodSignature joinPointObject = (MethodSignature) jp.getSignature();
    		//         
    		Method method = joinPointObject.getMethod();
    		//         
    		String name = method.getName();
    		Class<?>[] parameterTypes = method.getParameterTypes();
    		//             
    		Object target = jp.getTarget();
    		//       
    		method = target.getClass().getMethod(name, parameterTypes);
    		//   @AroundPointCut     
    		AroundPointCut joinPoint = method.getAnnotation(AroundPointCut.class);
    		if (!joinPoint.accessRead()) {
    			throw new ApplicationException("    !");
    		}
    		return jp.proceed();
    	}
    }
    
  • 業務のサービス追加@AroundPointCut注釈
  • は以下の通りである.
    
    	@AroundPointCut(accessRead = true)
    	public Object queryProuct() throws ApplicationException {
    		//TODO
    		return null;
    	}
    

    [/list]