Java 如果给定条件为false,如何编写注释/方面以不输入方法而是返回null?
我目前有一个要求,如果给定的条件为false,我需要从100个方法中返回null。我正在考虑为此使用Java注释或Spring方面,这样我就不必到处编写代码块。关于如何使用Java注释或Spring方面实现这一点,您有什么想法吗Java 如果给定条件为false,如何编写注释/方面以不输入方法而是返回null?,java,annotations,aop,spring-aop,aspect,Java,Annotations,Aop,Spring Aop,Aspect,我目前有一个要求,如果给定的条件为false,我需要从100个方法中返回null。我正在考虑为此使用Java注释或Spring方面,这样我就不必到处编写代码块。关于如何使用Java注释或Spring方面实现这一点,您有什么想法吗 任何指针都可能有帮助。如果我没弄错的话,Spring@Conditional annotation就是您想要的。 您创建了一些实现Spring条件接口的公共类: public class Test implements Condition { ... } 然后使用前面
任何指针都可能有帮助。如果我没弄错的话,Spring@Conditional annotation就是您想要的。 您创建了一些实现Spring条件接口的公共类:
public class Test implements Condition {
...
}
然后使用前面提到的注释和参数,该参数作为公共类的参数
@Conditional(Test.class)
public Object someMethod(boolean context){
/*and so do some logics; if the value of 'context' variable is false, you can return
null; otherwise, just return like this*/
return new someMethodImpl1();
}
希望我能帮忙。我很高兴任何形式的纠正。干杯 如果没有spring,您可以使用纯AspectJ: java:我们要修改的方法的示例
public class Demo {
public String boom(String base) {
return base;
}
}
DemoAspect.aj:配置文件。在AspectJ中,选择程序流中的某些连接点。为了实际实现横切行为,我们使用建议。汇集了一个切入点来挑选连接点,以及在每个连接点上运行的代码体:前、后、前后
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);
}
}
}
demost.java
public class DemoTest {
@Test
void name() {
Demo demo = new Demo();
assertEquals("boom!", demo.boom("detonate"));
assertEquals("fake", demo.boom("fake"));
}
}
将依赖项添加到pom.xml中
和插件,注意版本。例如,1.11插件需要1.8.13库
<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>
有更多细节的好例子 AOP是一种选择,但如果没有进一步的信息,答案只能是猜测。您希望从中返回的方法有哪些共同点?如果您想编写AOP切入点,这是必要的知识。Ist是一个方法签名、包名、类名模式、基类/接口、公共注释、其他东西还是它们的组合?此外,为了决定是使用SpringAOP还是AspectJ:所讨论的方法都是公共的吗?请显示示例代码,最好是一个。@kriegaex,感谢您的回复,关于您希望从中返回的方法有哪些共同点?->所有方法基本上都检查一个条件,如果该条件为真,它们应该返回null,而不是进一步处理任何内容。我已经知道了,但这并不能回答我的问题。我问这些方法在技术上有什么共同点,以便能够通过AOP识别它们,而不是关于方面应该添加到这些方法中的行为。@kriegaex,只是如果条件检查对它们来说是共同的,就没有别的了。此外,它们在同一个java类中,否则其他所有内容都不同-它们的参数、返回类型、方法签名等一切都不同。看看我的新问题,如果这能让你更清楚-如果你需要一个spring AOP的例子,请留下评论。是的@uli,我正在寻找一个spring AOP的例子。谢谢。如果这有助于你更好地理解regd,请检查我的这个问题。我的问题-我在你的第二个问题下添加了spring示例。嗨@dima_mayd,你能举个例子吗?关于测试中应该包含什么,例如,我的用例的matches方法的一个示例实现?我不想注册任何组件/bean,只想执行一个基于布尔值的方法。看看我的新问题,如果这能让你更清楚-
<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>