Java注释以确定是否执行带注释的方法
在我的方法中,我所有的代码都在一个if块中,测试特定的条件Java注释以确定是否执行带注释的方法,java,spring,annotations,Java,Spring,Annotations,在我的方法中,我所有的代码都在一个if块中,测试特定的条件 public void myMethod() { if (/* some condition */) { //do something } } 我希望通过注释来实现这一点——这意味着注释将执行一些代码,这些代码将“决定”是否应该调用该方法 @AllowInvokeMethod(/* some parameters to decide */) public void myMethod() { /
public void myMethod() {
if (/* some condition */) {
//do something
}
}
我希望通过注释来实现这一点——这意味着注释将执行一些代码,这些代码将“决定”是否应该调用该方法
@AllowInvokeMethod(/* some parameters to decide */)
public void myMethod() {
//do something (if annotation allows invokation)
}
这可能吗 你可以试试这个,我上面的文档。
此注释在调用方法时显示,请参见的文档 莫特 如果放置此结构,则在安装时无法查看文档 调用某个方法
//descripcion of the method
public void myMethod(String value) {
//do something (if annotation allows invokation)
}
在我的情况下,它是有效的,我希望这对你有效你可以试试这个,我上面的文档。
此注释在调用方法时显示,请参见的文档 莫特 如果放置此结构,则在安装时无法查看文档 调用某个方法
//descripcion of the method
public void myMethod(String value) {
//do something (if annotation allows invokation)
}
在我的例子中,它是有效的,我希望这对您有用。您可以使用SpringAOP创建一个方面,以建议对自定义注释进行注释的方法 例如,创建要在方法上指定的FilteredExecution注释
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FilteredExecution{
Class<? extends ExecutionFilter> value();
}
然后是方面
@Aspect
@Component
public class FilteredExceutionAspect{
@Around("@annotion(filterAnnotation)")
public void filter(ProceedingJoinPoint pjp , FilteredExecution filterAnnotation){
boolean shouldExecute = checkShouldExecute(filterAnnotation);
if(shouldExecute){
pjp.proceed();
}
}
private boolean checkShouldExecute(FilteredExecution filterAnnotation){
//use reflection to invoke the ExecutionFilter specified on filterAnnotatoon
}
您需要设置上下文,以便通过在配置类上使用@enableSpectJautProxy自动代理带有自定义注释的bean您可以使用Spring AOP创建一个方面来建议对自定义注释进行注释的方法 例如,创建要在方法上指定的FilteredExecution注释
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FilteredExecution{
Class<? extends ExecutionFilter> value();
}
然后是方面
@Aspect
@Component
public class FilteredExceutionAspect{
@Around("@annotion(filterAnnotation)")
public void filter(ProceedingJoinPoint pjp , FilteredExecution filterAnnotation){
boolean shouldExecute = checkShouldExecute(filterAnnotation);
if(shouldExecute){
pjp.proceed();
}
}
private boolean checkShouldExecute(FilteredExecution filterAnnotation){
//use reflection to invoke the ExecutionFilter specified on filterAnnotatoon
}
您需要设置您的上下文,以便通过在配置类上使用@enableSpectJautoproxy自动代理带有自定义注释的bean“调用方法时显示此注释,请参阅meothd的文档”不是真的。您可以基于javadoc注释生成html页面,但在“调用方法”时它们不会“显示”。你是说IDE的工具提示?你的答案并没有解决OP的问题,“这个注释是在调用方法时显示的,请参阅meothd的文档”并不是真的。您可以基于javadoc注释生成html页面,但在“调用方法”时它们不会“显示”。你是说IDE的工具提示?你的回答没有回答OP的问题。