@AspectJベースSpring AOP使用
[list=1] springのxmlプロファイルでaspectj を次のように構成します.注釈を作成する定義 は次のとおりです.切断面の宣言、切り込み点の宣言、ターゲットオブジェクトに対するブロック方法の呼び出し は、次のようになります.業務のサービス追加@AroundPointCut注釈 は以下の通りである.
[/list]
<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(accessRead = true)
public Object queryProuct() throws ApplicationException {
//TODO
return null;
}
[/list]