Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带注释的Spring接口 编辑_Java_Spring Aop_Interceptor - Fatal编程技术网

Java 带注释的Spring接口 编辑

Java 带注释的Spring接口 编辑,java,spring-aop,interceptor,Java,Spring Aop,Interceptor,如果Springbean实现的接口上存在注释,那么如何拦截Springbean上的任何一个方法 我添加了这个,因为它更准确地描述了试图解决的实际问题。下面是我解决这个问题的尝试。然而,完全不同的方法是可以接受的 原始问题 给定以下类 @Timed 公共静态接口TimedInterface{ 公共无效接口方法(); } 公共静态类TimedInterface实现实现TimedInterface{ @凌驾 公共无效接口方法(){ //无操作 } } 什么样的@advice实现可以通过检测@Time

如果Springbean实现的接口上存在注释,那么如何拦截Springbean上的任何一个方法

我添加了这个,因为它更准确地描述了试图解决的实际问题。下面是我解决这个问题的尝试。然而,完全不同的方法是可以接受的

原始问题 给定以下类

@Timed
公共静态接口TimedInterface{
公共无效接口方法();
}
公共静态类TimedInterface实现实现TimedInterface{
@凌驾
公共无效接口方法(){
//无操作
}
}
什么样的
@advice
实现可以通过检测
@Timed
注释来拦截对
接口方法的方法调用

我目前版本的spring aspect是

import org.aspectj.lang.ProceedingJoinPoint;
导入org.aspectj.lang.annotation.Around;
导入org.aspectj.lang.annotation.Aspect;
导入org.aspectj.lang.annotation.Pointcut;
@面貌
公共类计时方面{
专用TimerContext TimerContext;
公共计时规范(TimerContext ctx){
this.timerContext=ctx;
}
@切入点(value=“@within(timed)”)
public void BeanNotatedWithTimer(定时){}
@切入点(“执行(公共**(…)”)
public void publicMethod(){}
@切入点(“publicMethod()&&BeanNotatedWithTimer(timed)”)
public void publicMethodInsideaClassMarkedWithAtimer(定时){}
@大约(value=“执行(公共**+.*(..)”
+&&&@annotation(timed)”,argNames=“timed”)
public Object AroundanotatedMethod(final ProceedingJoinPoint joinPoint,Timed Timed)抛出可丢弃的{
返回timerContext.runThrowable(joinPoint.getSignature().getName(),joinPoint::procedure);
}
@大约(value=“publicMethodInsideAClassMarkedWithAtTimer(timed)”,argNames=“timed”)
NanotatedClass(最终处理joinPoint joinPoint,Timed Timed)周围的公共对象可抛出{
返回timerContext.runThrowable(joinPoint.getSignature().getName(),joinPoint::procedure);
} 
/**
*这是为了确保导入正确的注释。
* 
*它允许重构,我不希望这个方法真的能实现
*到处打电话。
* 
*@返回{@link Timer}批注的完全限定名。
*/
公共字符串注释名(){
返回Timed.class.getName();
}
}
这适用于具体注释的类(类型注释)。 这适用于带有类的带注释的方法(方法注释)

但是,我想修改它,使其适用于实现带注释接口的所有方法

请注意,我不介意从aspectj样式转换为其他样式。然而,spring创建的一些bean没有具体的类,我需要我的计时代码来拦截这些类

不起作用,因为这意味着要为每个接口编写一个处理器


声明这是不可能的。然而,我知道spring在其他注释中为
@Transactional
做这件事。

可以使用PointcutAdvisor在注释接口上拦截方法调用

可以通过切入点表达式执行,但我无法使其作为类don从接口继承类型级注释来工作

解决方案是实现一个抽象切入点顾问,并将其作为bean添加到spring应用程序上下文中

这篇文章的灵感来源于

注意:此实现与一些内部类相耦合,但它应该易于泛化,以使用自己的注释或执行不同的建议

注意:此实现与spring耦合,但这就是重点

注意:与所有spring实现一样,这是基于代理的,因此它不适用于自调用,也不适用于私有成员,而且它只代理SpringBean(因为它是执行代理的框架)

无评论的执行 如果您需要快速获得答案,此实现应该更易于扫描和阅读

如果需要导入,请查看完整的类

public class TimingAdvisor extends AbstractPointcutAdvisor {

    private static final long serialVersionUID = 1L;

    private final MethodInterceptor interceptor;
    private final StaticMethodMatcherPointcut pointcut = new TimingAnnotationOnClassOrInheritedInterfacePointcut();

    public TimingAdvisor(TimerContext timerContext) {
        super();
        this.interceptor = (MethodInvocation invocation) -> timerContext.runThrowable(invocation.getMethod().getName(),
                invocation::proceed);
    }

    @Override
    public Pointcut getPointcut() {
        return this.pointcut;
    }

    @Override
    public Advice getAdvice() {
        return this.interceptor;
    }

    private final class TimingAnnotationOnClassOrInheritedInterfacePointcut extends StaticMethodMatcherPointcut {
        @Override
        public boolean matches(Method method, Class<?> targetClass) {
            if (AnnotationUtils.findAnnotation(method, Timed.class) != null) {
                return true;
            }
            return AnnotationUtils.findAnnotation(targetClass, Timed.class) != null;
        }
    }
}
公共类TimingAdvisor扩展了AbstractPointcutAdvisor{
私有静态最终长serialVersionUID=1L;
专用最终方法拦截器;
private final StaticMethodMatcherPointcut pointcut=新计时说明OnClassorInheritedInterfacePointcut();
公共定时提示(TimerContext TimerContext){
超级();
this.interceptor=(MethodInvocation invocation)->timerContext.runThrowable(invocation.getMethod().getName(),
调用::继续);
}
@凌驾
公共切入点getPointcut(){
返回这个.pointcut;
}
@凌驾
公众意见{
返回此.interceptor;
}
私有最终类计时说明OnClassorInheritedInterfacePointcut扩展了StaticMethodMatcherPointcut{
@凌驾
公共布尔匹配(方法,类targetClass){
if(AnnotationUtils.findAnnotation(方法,Timed.class)!=null){
返回true;
}
返回AnnotationUtils.FindAnotation(targetClass,Timed.class)!=null;
}
}
}
实施
import java.lang.reflect.Method;
导入org.aopalliance.aop.Advice;
导入org.aopalliance.intercept.MethodInterceptor;
导入org.aopalliance.intercept.MethodInvocation;
导入org.springframework.aop.Pointcut;
导入org.springframework.aop.support.AbstractPointcutAdvisor;
导入org.springframework.aop.support.StaticMethodMatcherPointcut;
导入org.springframe
import java.lang.reflect.Method;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.core.annotation.AnnotationUtils;

/**
 * <p>
 * Intercepts all calls to beans with methods annotated with {@link Timed}.
 * </p>
 * 
 * <p>
 * The following use cases have been tested.
 * </p>
 * <ul>
 * <li>Nested invocation Timed bean invokes another TimedBean.</li>
 * <li>Annotated class.</li>
 * <li>Annotated method on a class.</li>
 * <li>Class implementing annotated interface.</li>
 * <li>Class implementing an Interface with an annotated method</li>
 * </ul>
 * 
 * <p>
 * Calls to timed methods will be passed though
 * {@link TimerContext#runThrowable(String, TimerContext.ThrowableSupplier)}
 * </p>
 * 
 * 
 * <strong>Important Notes and Limitations</strong>
 * 
 * <ul>
 * <li>This will only work with Spring beans as its using spring own advising
 * mechanism.</li>
 * <li>This will only work with public method invocations as with all of springs
 * proxies.</li>
 * <li>This will not work for self calls.</li>
 * </ul>
 * <p>
 * The limitations are described in further details in the <a href=
 * "https://docs.spring.io/spring/docs/3.2.4.RELEASE/spring-framework-reference/html/aop.html#aop-proxying">spring
 * manual</a>.
 * 
 */
public class TimingAdvisor extends AbstractPointcutAdvisor {

    private static final long serialVersionUID = 1L;

    private final MethodInterceptor interceptor;
    private final StaticMethodMatcherPointcut pointcut = new TimingAnnotationOnClassOrInheritedInterfacePointcut();

    /**
     * Constructor.
     * 
     * @param timerContext
     *            The context where the timing will be run on.
     */
    public TimingAdvisor(TimerContext timerContext) {
        super();
        this.interceptor = (MethodInvocation invocation) -> timerContext.runThrowable(invocation.getMethod().getName(),
                invocation::proceed);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.aop.PointcutAdvisor#getPointcut()
     */
    @Override
    public Pointcut getPointcut() {
        return this.pointcut;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.aop.Advisor#getAdvice()
     */
    @Override
    public Advice getAdvice() {
        return this.interceptor;
    }

    /**
     * A matcher that matches:
     * <ul>
     * <li>A method on a class annotated with Timed.</li>
     * <li>A method on a class extending another class annotated with
     * Timed.</li>
     * <li>A method on a class implementing an interface annotated with
     * Timed.</li>
     * <li>A method implementing a method in a interface annotated with
     * Timed.</li>
     * </ul>
     * 
     * <p>
     * <strong>Note:</strong> this uses springs utils to find the annotation and will not be
     * portable outside the spring environment.
     * </p>
     */
    private final class TimingAnnotationOnClassOrInheritedInterfacePointcut extends StaticMethodMatcherPointcut {
        @Override
        public boolean matches(Method method, Class<?> targetClass) {
            if (AnnotationUtils.findAnnotation(method, Timed.class) != null) {
                return true;
            }
            return AnnotationUtils.findAnnotation(targetClass, Timed.class) != null;
        }
    }
}
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TimerContextTest.ContextConfig.class)
public class TimerContextTest {

    @Autowired
    private TimedClassA timedClass;

    @Autowired
    private RecordingGaugeService gaugeService;

    @Autowired
    private ClassWithTimedMethod partiallyTimed;
    
    @Autowired
    private TimedInterface timedInterface;
    
    @Autowired
    private PartiallyTimedInterface partiallyTimedInterface;

    @Before
    public void setup() {
        gaugeService.clear();
    }

    @Test
    public void mustRetainHirachy() {
        timedClass.outer();
        assertThat(gaugeService.entries()).hasSize(2).contains("timer.outer", "timer.outer.inner");
    }

    @Test
    public void mustNotBeInvokedOnPrivateMethods() {
        timedClass.somethingPrivate();
        assertThat(gaugeService.entries()).isEmpty();
    }
    

    @Test
    public void mustBeInvokedForMethodsAnnotatedWithTimed() {

        String untimed = partiallyTimed.untimed();
        assertThat(untimed).isEqualTo("untimed result");
        assertThat(gaugeService.entries()).isEmpty();

        String timed = partiallyTimed.timed();
        assertThat(timed).isEqualTo("timed result");
        assertThat(gaugeService.entries()).containsExactly("timer.timed");

        assertThatThrownBy(() -> {
            partiallyTimed.timedExceptionThrower();
        }).hasMessage("timedExceptionThrower");
        assertThat(gaugeService.entries()).containsExactly("timer.timed", "timer.timedExceptionThrower");

    }

    @Test
    public void mustBeInvokedAsTopLevelMoreThanOnce() {
        partiallyTimed.timed();
        partiallyTimed.timed();
        assertThat(gaugeService.entries()).containsExactly("timer.timed", "timer.timed");
    }
    
    
    @Test
    public void mustTimeInterfaceImplementations() {
        timedInterface.interfaceMethod();
        assertThat(gaugeService.entries()).containsExactly("timer.interfaceMethod");
    }
    
    @Test
    public void mustTimeAnnotatedInterfaceMethods() {
        partiallyTimedInterface.timedMethod();
        partiallyTimedInterface.untimedMethod();
        partiallyTimedInterface.timedDefaultMethod();
        partiallyTimedInterface.untimedDefaultMethod();
        assertThat(gaugeService.entries()).containsExactly("timer.timedMethod", "timer.timedDefaultMethod");
    }
    
    //////////////////////////////
    // Configuration and Helpers
    //////////////////////////////
    @Configuration
    @EnableAspectJAutoProxy
    public static class ContextConfig {

        @Bean
        public GaugeService gaugeService() {
            return new RecordingGaugeService();
        }

        @Bean
        public TimerContext timerContext(GaugeService gaugeService) {
            return new TimerContext(gaugeService);
        }

        @Bean
        public TimedClassB inner() {
            return new TimedClassB();
        }

        @Bean
        public TimedClassA outer(TimedClassB inner) {
            return new TimedClassA(inner);
        }

        @Bean
        public TimingAdvisor timingAdvisor(TimerContext ctx) {
            return new TimingAdvisor(ctx);
        }

        @Bean
        public ClassWithTimedMethod partiallyTimed() {
            return new ClassWithTimedMethod();
        }
        
        @Bean
        public TimedInterface timedInterface() {
            return new TimedInterfaceImplementation();
        }
        
        @Bean
        public PartiallyTimedInterface partiallyTimedInterface() {
            return new ClassImplementingPartiallyTimedInterface();
        }
        

    }

    @Timed
    public static class TimedClassA {

        private TimedClassB inner;

        public TimedClassA(TimedClassB inner) {
            this.inner = inner;
        }

        public String outer() {
            return this.inner.inner();
        }

        private String somethingPrivate() {
            return "private";
        }
    }

    @Timed
    public static class TimedClassB {

        public String inner() {
            return "inner";
        }
    }
    
    @Timed
    public static interface TimedInterface {
        public void interfaceMethod();
    }
    
    
    public static class TimedInterfaceImplementation implements TimedInterface {

        @Override
        public void interfaceMethod() {
            //NO-OP
        }
        
    }
    
    public static interface PartiallyTimedInterface {
        @Timed public void timedMethod();
        public void untimedMethod();
        
        @Timed public default void timedDefaultMethod() {}
        public default void untimedDefaultMethod() {}
    }
    
    public static class ClassImplementingPartiallyTimedInterface implements PartiallyTimedInterface {

        @Override
        public void timedMethod() {
            // NO-OP
        }

        @Override
        public void untimedMethod() {
            // NO-OP
        }
        
    }

    public static class ClassWithTimedMethod {

        public String untimed() {
            return "untimed result";
        }

        @Timed
        public String timed() {
            return "timed result";
        }

        @Timed
        public String timedExceptionThrower() {
            throw new IllegalStateException("timedExceptionThrower");
        }
    }

    private static class RecordingGaugeService implements GaugeService {

        private List<String> recordedMetrics = new ArrayList<>();

        @Override
        public void submit(String metricName, double value) {
            this.recordedMetrics.add(metricName);
            System.out.println(metricName);
        }

        public void clear() {
            recordedMetrics = new ArrayList<>();
        }

        public List<String> entries() {
            return recordedMetrics;
        };

    }

}