我正在学习面向方面的编程概念和Spring AOP.我无法理解Pointcut和Joinpoint之间的区别 - 它们对我来说似乎都是一样的.Pointcut是您应用建议的地方,Joinpoint也是我们可以应用我们建议的地方.那有什么区别?
切入点的一个例子可以是:
@Pointcut("execution(* * getName()")
Run Code Online (Sandbox Code Playgroud)
什么是Joinpoint的例子?
我有一个在Java 6/Spring 3中实现的服务类,它需要一个注释来限制角色访问.
我已经定义了一个名为RequiredPermission的注释,它具有一个名为OperationType的枚举中的一个或多个值作为其value属性:
public @interface RequiredPermission {
/**
* One or more {@link OperationType}s that map to the permissions required
* to execute this method.
*
* @return
*/
OperationType[] value();}
public enum OperationType {
TYPE1,
TYPE2;
}
package com.mycompany.myservice;
public interface MyService{
@RequiredPermission(OperationType.TYPE1)
void myMethod( MyParameterObject obj );
}
package com.mycompany.myserviceimpl;
public class MyServiceImpl implements MyService{
public myMethod( MyParameterObject obj ){
// do stuff here
}
}
Run Code Online (Sandbox Code Playgroud)
我还有以下方面定义:
/**
* Security advice around methods that are annotated with …Run Code Online (Sandbox Code Playgroud) 我试图定义一个切入点,它将捕获每个注释的方法(即)@CatchThis.这是我自己的注释.
此外,我想访问该方法的第一个参数,它将是Long类型.可能还有其他争论,但我不关心它们.
编辑
这就是我现在所拥有的.我不知道的是如何传递带注释的方法的第一个参数@CatchThis.
@Aspect
public class MyAspect {
@Pointcut(value = "execution(public * *(..))")
public void anyPublicMethod() {
}
@Around("anyPublicMethod() && @annotation(catchThis)")
public Object logAction(ProceedingJoinPoint pjp, CatchThis catchThis) throws Throwable {
return pjp.proceed();
}
}
Run Code Online (Sandbox Code Playgroud) 我在aspectJ表达式中:
@Pointcut("within(com.param.cpms.dao.impl.ProjectMetaDaoImpl)")
public void daoExceptionHandle() {
}
Run Code Online (Sandbox Code Playgroud)
在Spring 3.0启动时,我收到以下错误:
nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
Run Code Online (Sandbox Code Playgroud) 如何编写适用于覆盖带注释的接口方法的方法执行的aspectj切入点?例如:
interface A {
@MyAnnotation void method();
}
class B implements A {
void method();
}
Run Code Online (Sandbox Code Playgroud)
切入点execution(@MyAnnotation * *.*(..))仅在B.method()带有注释本身时匹配.还有另一种方法吗?
我正在尝试定义一个切入点表达式来匹配包含用特定注释注释的参数的方法,无论参数位于什么位置.在我的情况下,我正在寻找@Constraint注释.例如:
匹配方法:
public void method1(@Constraint Car car)
public void method2(String id, @Constraint Plane plane)
public void method3(Wheel wheel, @Constraint List<Train> trains, @Constraint Plane plane)
public void method4(Motor motor, @Constraint Set<Train> trains, Bicycle bike, Wheel wheel)
public void method5(Wing wing, Motorcycle moto, @Constraint Truck truck, Bicycle bike, Wheel wheel)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了以下表达式,没有运气:
@Before("execution(public * *.*(..)) and @args(com.example.Constraint)") // there can be only one parameter
@Before("execution(public * *.*(..)) and @args(..,com.example.Constraint)") // parameter must be in last position
@Before("execution(public * *.*(..)) and @args(com.example.Constraint,..)") …Run Code Online (Sandbox Code Playgroud) 我能够从建议的方法调用中获取签名和参数,但我无法弄清楚如何获取返回值或异常.我有点假设它可以以某种方式使用并继续.
说我有这样的方法:
public void method(@CustomAnnotation("value") String argument)
Run Code Online (Sandbox Code Playgroud)
是否有切入点表达式可以选择带有@CustomAnnotation注释的参数的所有方法?如果是这样,我可以获得访问的方式去"价值"参数?
我在基于spring mvc的应用程序中使用Aspect进行日志记录活动.我正在使用@controller注释来定义我的应用程序中的任何控制器.我在两个不同的包中有两个不同的控制器说
我可以通过使用将方面应用于一个特定的控制器包
<aop:config>
<aop:pointcut id="pointcut1"
expression="execution(* package1.*.*(..))"
id="policy1" />
<aop:aspect ref="aspect1" order="1">
<aop:before pointcut-ref="pointcut1" method="before" arg-names="joinPoint" />
<aop:after-returning returning="returnValue" arg-names="joinPoint, returnValue" pointcut-ref="pointcut1" method="after" />
</aop:aspect>
</aop:config>
<bean id="aspect1" class="com......aspectclass" />
Run Code Online (Sandbox Code Playgroud)
我的问题是如何指定在一个以上的不同包装的表达(*包1. ..(..))**.
现在我宣布为每个包在一个方面的一个单独的一个单独的切入点aop:before,并aop:after为每个切入点条目.但我认为这应该是定义多个包切入点的理想方式.
我正在摆弄AspectJ并想出一个我似乎无法正确实现的想法(我的生活故事).
我已经定义了一个方面:
package my.package;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.ProceedingJoinPoint;
@Aspect
public class MyAspect {
@Pointcut("execution(* *(..)) && this(o)")
public void instanceMethod(Object o) {}
@Pointcut("within(@Marker *)")
public void methodsFromMarkedClasses() {}
@Around("methodsFromMarkedClasses() && instanceMethod(o)")
public Object markedMethodsAdvice(ProceedingJoinPoint joinPoint, Object o) throws Throwable {
// do awesome stuff
return null; //<- not the actual return, just added this so that my head wouldn't hurt
}
}
Run Code Online (Sandbox Code Playgroud)
我已经定义了@Marker一个空的注释.
我们的想法是在markedMethodsAdvice任何时候对标记为的类的对象调用方法时执行建议@Marker.甚至(这里是棘手的部分):
情况1
如果所述方法继承自未标记的类,请参阅示例:
特定
package my.package;
public class Alpha {
public …Run Code Online (Sandbox Code Playgroud) pointcut ×10
aop ×7
spring ×7
aspectj ×6
java ×6
spring-aop ×5
annotations ×1
aspect ×1
exception ×1
inheritance ×1
pointcuts ×1
return ×1