如何编写注释/方面以不输入方法但如果给定条件为假则返回null? [英] How to write an annotation/aspect to not enter a method but return null if a given condition is false?

查看:127
本文介绍了如何编写注释/方面以不输入方法但如果给定条件为假则返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个要求,如果给定条件为false,我需要从100个方法返回null。我正在考虑使用Java Annotations或Spring Aspects,这样我就不必在任何地方编写if-else代码块。有关如何使用Java Annotations或Spring Aspects执行此操作的任何想法?

I currently have a requirement where I need to return null from 100s of methods if a given condition is false. I was thinking of using Java Annotations or Spring Aspects for this so that I don't have to write that if-else block of code everywhere. Any idea as to how we can do this using Java Annotations or Spring Aspects?

任何指针都可能有所帮助。

Any pointers might be helpful.

推荐答案

没有春天,你可以使用纯AspectJ:

Without spring you could use pure AspectJ:

Demo.java:我们想要修改的方法示例

Demo.java : example of the method we want to modify

public class Demo {
    public String boom(String base) {
        return base;
    }
}

DemoAspect.aj:配置文件。 使用入门。在AspectJ中,切入点选择某些加入程序流程中的要点。为了实际实现横切行为,我们使用建议。 建议汇集切入点(选择) out join points)和一组代码(在每个连接点上运行):before,around,after ...

DemoAspect.aj : configuration file. Getting started. In AspectJ, pointcuts pick out certain join points in the program flow. To actually implement crosscutting behavior, we use advice. Advice brings together a pointcut (to pick out join points) and a body of code (to run at each of those join points): before, around, after...

public aspect DemoAspect {
    pointcut callBoom(String base, Demo demo) : 
            call(String Demo.boom(String)) && args(base) && target(demo);

    String around(String base, Demo demo) : callBoom(base, demo){
        if ("detonate".equals(base)) {
            return "boom!";
        } else {
            return proceed(base, demo);
        }
    }
}

DemoTest.java

DemoTest.java

public class DemoTest {
    @Test
    void name() {
        Demo demo = new Demo();
        assertEquals("boom!", demo.boom("detonate"));
        assertEquals("fake", demo.boom("fake"));
    }
}

将依赖项添加到 pom。 xml

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.13</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>

和插件一样,注意版本。例如1.11插件需要1.8.13库。

And plugin, pay attention to the versions. For example 1.11 plugin expects 1.8.13 libraries.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.11</version>
    <configuration>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <showWeaveInfo>true</showWeaveInfo>
        <verbose>true</verbose>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8 </encoding>
    </configuration>
    <executions>
        <execution>
            <goals>
                <!-- use this goal to weave all your main classes -->
                <goal>compile</goal>
                <!-- use this goal to weave all your test classes -->
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

有更多细节的好例子, Baeldung 官方文档

Good example with more details, Baeldung. Official documentation.

这篇关于如何编写注释/方面以不输入方法但如果给定条件为假则返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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