在我的方法中,我的所有代码都在if块中,测试某些条件。
public void myMethod() {
if (/* some condition */) {
//do something
}
}
我想通过注释来做到这一点 - 这意味着注释将执行一些代码,这些代码将决定""是否应该调用该方法。
@AllowInvokeMethod(/* some parameters to decide */)
public void myMethod() {
//do something (if annotation allows invokation)
}
这可能吗?
答案 0 :(得分:4)
您可以使用Spring AOP创建ASpect来建议注释自定义注释的方法
例如,创建一个要在方法上指定的FilteredExecution注释
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FilteredExecution{
Class<? extends ExecutionFilter> value();
}
ExecutionFilter是决定是否应该执行的接口
public interface ExecutionFilter{
boolean sholudExecute();
}
然后是方面
@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
}
您需要设置上下文,以便通过在配置类上使用@EnableAspectjAutoProxy自动代理具有自定义注释的bean
答案 1 :(得分:0)
你可以试试这个,metoh上面的文档。 调用方法时显示此注释,并查看文档 meothd
/**
* descripcion of the method
* @param value , any value
*/
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)
}
在我的情况下,它有效,我希望这适合你