это аннотации Код:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface PreVisit {
String value();
}
это использование в контроллере @PreVisit("@pv.hasAccess('xxxxxx')")
@PreVisit("@pv.hasAccess('xxxxxx')")
@RequestMapping(value = "getUser")
public User getUser(Integer userId) {...some code...}
это код pv.hasAccess('xxxxxx'):
@Service("pv")
public class PageVisit{
public boolean hasAccess(String par){
//return false or true;
}
}
Мой вопрос:
В Aspect как получить методы в параметрах аннотации и получить результат выполнения
это код файла Aspect:
@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 [email protected]('xxxxxx'); pvResult=false or true
//Do something use the pvResult
}
}
Вы можете использовать второй тип аргумента 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 [email protected]('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 [email protected]('xxxxxx');
//Do something use the pvResult
String value = preVisit.value();
boolean hasAccess = pv.hasAccess(value);
}
}
Вы можете внедрить PageVisit
в аспект и вызвать метод hasAccess.
``` @PreVisit("@serviceA.methodA('xxxxxx')") @PreVisit("@serviceB.methodB('xxxxxx')") @PreVisit("@serviceC.methodC('xxxxxx')") `` ` Если бы было три или больше способов, я бы написал три или больше Аспектов.
Я спрашиваю
Я решил это, используя отражение 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 Vaithilingam
Строковое значение = preVisit.value(); значение представляет собой строку, значение = "@pv.hasAccess('xxxxxx')" Я надеюсь, что для выполнения pv.hasAccess('xxxxxx') этот результат будет логическим, ложным или истинным