是否存在Spring AOP批注,仅当该批注返回true时,该批注才允许我们进入方法内部? [英] Is there a Spring AOP annotation which lets us go inside a method only if that annotation returns 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()方法&中.打印Hello World&返回字符串"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.
推荐答案
要在Spring中使用AOP,应将其添加到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"));```` doesn't satisfy the condition and will be executed as expected.
logger.info(echo.echo("skip")));会被截获,因为结果将出现特殊的日志记录消息,并且由于该方法具有返回类型,因此记录器将打印为空.
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
具有更多详细信息的好例子,链接
Good example with more details, link
这篇关于是否存在Spring AOP批注,仅当该批注返回true时,该批注才允许我们进入方法内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!