java—如何在方面的注解参数中获取方法并获取方法执行的结果

pqwbnv8z  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(525)

这是注解代码:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface PreVisit {
    String value();
}

在控制器@previsit(“@pv.hasaccess('')”中使用

@PreVisit("@pv.hasAccess('xxxxxx')")
@RequestMapping(value = "getUser")
public User getUser(Integer  userId) {...some code...}

这是pv.hasaccess('')代码:

@Service("pv")
public class  PageVisit{
    public boolean hasAccess(String par){        
        //return false or true;
    }
}

我的问题是:
在方面,如何在注解参数中获取方法并获取执行结果
这是方面文件代码:

@Aspect
@Component
public class PreVisitAspect {
    @Around("@annotation(PreVisit)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //How do I get the result of the method execution in the annotation parameter here
        //boolean  pvResult=@pv.hasAccess('xxxxxx');  pvResult=false or true
        //Do something use the pvResult
    }
}
0md85ypi

0md85ypi1#

我是提问者
我使用java反射解决了这个问题,如下所示:

@Aspect
@Component
public class PreVisitAspect {

    @Autowired
    private PageVisit pv;

    @Around("@annotation(preVisit)")
    public Object around(ProceedingJoinPoint joinPoint, PreVisit preVisit) throws Throwable {       
        String value = preVisit.value();
        //String value="@pas.hasAccess('xxxxxx')";
        if(value.startsWith("@")){
            String beanName=value.substring(value.indexOf("@")+1,value.indexOf("."));
            String methodName=value.substring(value.indexOf(".")+1,value.indexOf("("));
            String paramsStr=value.substring(value.indexOf("(")+2,value.lastIndexOf(")")-1);
            Object[] paramsArr=paramsStr.split("','");

            logger.info("beanName:"+beanName);
            logger.info("methodName:"+methodName);
            logger.info("paramsStr:"+paramsStr);

            ServletContext servletContext = request.getSession().getServletContext();
            ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            Method method = ReflectionUtils.findMethod(appContext.getBean(beanName).getClass(), methodName,new Class[]{String.class} );
            Boolean result = (Boolean)ReflectionUtils.invokeMethod(method,  appContext.getBean(beanName),paramsArr);
            logger.info(result.toString());
        }
        .....other Code.....
    }
}

tks@karthikeyan vaithlingam公司

qmb5sa22

qmb5sa222#

您可以选择第二个参数类型 PreVisit 您可以访问方法中的注解值。

@Aspect
@Component
public class PreVisitAspect {
    @Around("@annotation(PreVisit)")
    public Object around(ProceedingJoinPoint joinPoint, PreVisit preVisit) throws Throwable {
        //How do I get the result of the method execution in the annotation parameter here
        //boolean  pvResult=@pv.hasAccess('xxxxxx');
        //Do something use the pvResult
        String value = preVisit.value();
    }
}

如果你想使用 PageVisit#hasAccess(String) ,然后注入 PageVisit 并调用方法。
为此,必须修改控制器方法,如下所示。

@PreVisit("xxxxxx")
@RequestMapping(value = "getUser")
public User getUser(Integer  userId) {...some code...}

你的容貌会很好。

@Aspect
@Component
public class PreVisitAspect {

    @Autowired
    private PageVisit pv;

    @Around("@annotation(PreVisit)")
    public Object around(ProceedingJoinPoint joinPoint, PreVisit preVisit) throws Throwable {
        //How do I get the result of the method execution in the annotation parameter here
        //boolean  pvResult=@pv.hasAccess('xxxxxx');
        //Do something use the pvResult
        String value = preVisit.value();
        boolean hasAccess = pv.hasAccess(value);
    }
}

相关问题