是否有一个 Spring AOP 注释让我们只有在该注释返回 true 时才能进入方法内部? [英] Is there a Spring AOP annotation which lets us go inside a method only if that annotation returns true?

查看:23
本文介绍了是否有一个 Spring AOP 注释让我们只有在该注释返回 true 时才能进入方法内部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个 Spring AOP 注释可以让我们仅在该注释返回 true 时才能进入方法内部?

Is there a Spring AOP annotation which lets us go inside a method only if that annotation returns true?

我想要这样的东西:

@CustomAnnotation
public String foo(){
System.out.println("Hello World");
return "foo";
}

所以现在只有当@CustomAnnotation 返回 true 时,我们才会进入 foo() 方法 &打印你好世界 &返回字符串 "foo" ,但是当 @CustomAnnotation 返回 false 时 - 我们根本不会进入 foo() 方法.

So now only when the @CustomAnnotation returns true is when we will go inside the foo() method & print Hello World & return the String "foo" , but when @CustomAnnotation return false - we won't go inside the foo() method at all.

推荐答案

要将 AOP 与 spring 一起使用,您应该添加到 pom.xml

To use AOP with spring you should add to pom.xml

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>5.0.1.RELEASE</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.12</version>
  <scope>compile</scope>
</dependency>

或者对于 spring boot 项目只有一个依赖

or only one dependency for spring boot projects

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

要创建自定义注释,我们需要 SkipOnCondition.java

To create custom annotation we need SkipOnCondition.java

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SkipOnCondition {
}

我们需要配置我们的方面 EchoAspect.java.为了能够跳过方法,我们选择了Around 拦截点.拦截的触发器将被指定注解.ProceedingJoinPoint 可以提供有关拦截方法的所有详细信息.例如:参数、签名、...

We need to configure our aspect EchoAspect.java. To be able to skip method we selected Around interception point. The trigger for the interception will be specified annotation. ProceedingJoinPoint can provide all details about intercepted method. For example: arguments, signeture, ...

@Aspect
@Configuration
public class EchoAspect {
    private Logger logger = LoggerFactory.getLogger(EchoAspect.class);

    @Around("@annotation(com.example.demo.aop.SkipOnCondition)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        if ("skip".equals((String) joinPoint.getArgs()[0])){
            logger.info("Condition is true the method will be skipped.");
            return null;
        }
        return joinPoint.proceed();
    }
}

被测对象 Echo.java 具有 void 和返回类型的方法.

An object under test Echo.java has void and method with return type.

@Component
public class Echo {

    private Logger logger = LoggerFactory.getLogger(Echo.class);

    @SkipOnCondition
    public String echo (String s) {
        return s;
    }

    @SkipOnCondition
    public void blindEcho (String s) {
        logger.info(s);
    }
}

和概念证明 EchoTest.java

And proof of concept EchoTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class EchoTest {
    private Logger logger = LoggerFactory.getLogger(EchoTest.class);

    @Autowired
    private Echo echo;

    @Test
    public void testEcho() {
        echo.blindEcho("boom");
        echo.blindEcho("skip");
        logger.info(echo.echo("boom"));
        logger.info(echo.echo("skip"));
    }
}

输出将向我们展示它是如何工作的.echo.blindEcho("boom"); 不满足条件,将按预期执行.echo.blindEcho("skip"); 将被拦截,因为结果只会出现特殊的日志消息.logger.info(echo.echo("boom"));```` 不满足条件,将按预期执行.logger.info(echo.echo("skip"));``` 将被拦截,因为结果会出现特殊的日志消息,并且由于该方法具有返回类型,因此记录器将打印 null.

The output will show us how it works. echo.blindEcho("boom"); doesn't satisfy the condition and will be executed as expected. echo.blindEcho("skip"); will be intercepted as result special logging message will only appear. logger.info(echo.echo("boom"));```` doesn't satisfy the condition and will be executed as expected.logger.info(echo.echo("skip"));``` will be intercepted as result special logging message will appear and because the method has a return type the logger will print null.

com.example.demo.aop.Echo                : boom
com.example.demo.aop.EchoAspect          : Condition is true the method will be skipped.
com.example.demo.aop.EchoTest            : boom
com.example.demo.aop.EchoAspect          : Condition is true the method will be skipped.
com.example.demo.aop.EchoTest            : null

有更多细节的好例子,link

这篇关于是否有一个 Spring AOP 注释让我们只有在该注释返回 true 时才能进入方法内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆