[英]Spring 4 method interceptor and use custom implementation
我有以下课程和接口
interface abc {
public A do();
}
package x;
public Impl1 implements abc{
public A do(){
}
}
package y;
public Impl2 implements abc{
public A do(){
}
}
我没有Impl1或Impl2的源代码。 但是想拦截对do()方法的任何调用并使用我自己的实现。 同样基于某些条件可以调用实际的do()实现,在其他情况下则不会将其委派给原始实现。
您能否让我知道这是否可以实现。 如果是,该如何实施?
我正在使用Spring 4和JDK 7。
我将提供一个独立的AspectJ解决方案,但是在Spring AOP中,它将以相同的方式进行操作,只有方面和您的目标类需要是Spring bean / components,所以不要忘记像@Component
这样的注释,如Ian Mc所示。 。
助手类+接口+实现:
package de.scrum_master.app;
public class A {
private String name;
public A(String name) {
this.name = name;
}
@Override
public String toString() {
return "A [name=" + name + "]";
}
}
package de.scrum_master.app;
public interface MyInterface {
public A doSomething();
}
package de.scrum_master.app;
public class FirstImpl implements MyInterface {
@Override
public A doSomething() {
return new A("First");
}
}
package de.scrum_master.app;
public class SecondImpl implements MyInterface {
@Override
public A doSomething() {
return new A("Second");
}
}
驱动程序应用程序:
package de.scrum_master.app;
public class Application {
private static MyInterface myInterface;
public static void main(String[] args) {
myInterface = new FirstImpl();
for (int i = 0; i < 5; i++) {
System.out.println(myInterface.doSomething());
}
myInterface = new SecondImpl();
for (int i = 0; i < 5; i++) {
System.out.println(myInterface.doSomething());
}
}
}
没有方面的控制台日志:
A [name=First]
A [name=First]
A [name=First]
A [name=First]
A [name=First]
A [name=Second]
A [name=Second]
A [name=Second]
A [name=Second]
A [name=Second]
到目前为止,很无聊。
方面:
现在,让我们实现一个愚蠢的小方面,随机决定方法执行与跳过并改为提供另一个返回值(因为我不知道会导致您跳过方法执行的实际条件):
package de.scrum_master.aspect;
import java.util.Random;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import de.scrum_master.app.A;
@Aspect
public class MyAspect {
private static Random random = new Random();
@Around("execution(A de.scrum_master.app.MyInterface.*(..))")
public A interceptCalls(ProceedingJoinPoint thisJoinPoint) throws Throwable {
if (random.nextBoolean())
return (A) thisJoinPoint.proceed();
else
return new A("Aspect");
}
}
具有有效方面的控制台日志:
A [name=Aspect]
A [name=First]
A [name=Aspect]
A [name=Aspect]
A [name=First]
A [name=Aspect]
A [name=Second]
A [name=Second]
A [name=Aspect]
A [name=Second]
您的请求可以使用Spring AOP来完成,更具体地说,可以使用@Around
建议来完成。 通过@Around建议,您可以将调用直接传递给原始实现,也可以使调用短路并改为调用实现。 您需要提供选择一个或另一个的逻辑。
@Around方法传递给ProceedingJoinPoint。 要调用原始实现,请使用“ proceed
”方法。 如果要短路,则不要呼叫proc; 而是调用您自己的方法来创建一个“ A”对象。
下面的代码显示了一个基于@Aspect的类,该类演示了这两种技术。 您必须注入自己的实现,以便可以根据需要创建自己的A对象。
通常,您应该对Spring AOP进行一些阅读,更具体地说,是对Pointcuts(用于拦截呼叫)和@Around建议进行阅读。 值得注意的是,您可以组合切入点,并使用通配符,因此,如果使切入点具有足够的通用性以捕获所有实现的do方法,则很可能可以在@Aspect类中使用数量有限的方法来实现所需的功能。
示例代码显示了到原始实现的传递和调用您自己的短路。
@Aspect
@Component
public class DoAspects {
@Autowired
@Qualifier("YourQualifier")
private abc p3;
@Around("execution(* pkg1.Pkg1AImpl.do())")
public A p1(ProceedingJoinPoint jp) throws Throwable {
// Some logic determines to call the original implementation (i.e. proceed)
A a = (A)jp.proceed(); // Let the other implementation create A
return a;
}
@Around("execution(* pkg2.Pkg2AImpl.do())")
public A p2(ProceedingJoinPoint jp) {
// Some logic determines to short-circuit, and call own implementation
A a = p3.do(); // You create A
return a;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.