如何访问 Java 特性中指定的参数?
How do I access parameters specified in a Java Aspect?
我有一个名为“@ProgressCheck”的注释,我们可以将其放在控制器上以检查应用程序的进度。如果应用程序已经提交或延迟,则它会将用户带到适合该情况的页面。
注解界面为:
@Retention(RetentionPolicy.RUNTIME)
public @interface ProgressCheck {
}
该注释的 "implementation" 类似于:
@Around("execution(* *(..)) && args(session,..) && @annotation(com.foo.aspects.progress.ProgressCheck)")
public Object progressCheck(ProceedingJoinPoint pjp, HttpSession session) throws Throwable {
Applicant applicant = this.applicationSessionUtils.getApplicant(session);
Application application = this.applicationSessionUtils.getApplication(session);
switch (this.applicantService.getProgress(applicant)) {
case SUBMITTED:
return this.modelAndViewFactory.gotoStatusPage(applicant, application);
case LATE:
return this.modelAndViewFactory.gotoDeadlineMissed(applicant, application);
default:
case IN_PROGRESS:
return pjp.proceed();
}
}
基于会话和数据库中的值,注解的 "implementation" 允许用户继续进入控制器,或根据需要将它们重定向到另一个 ModelAndView。
我想为注释提供参数,然后在此 "implementation" 逻辑中,使用这些参数进一步调整决策。根据这个逻辑,在不知道应用注解的位置的情况下,我如何访问这些参数?或者我应该使用另一种方法吗?
诀窍是查询方面的 JoinPoint 对象以获取有关被注释的方法(或 class 或其他)的注释信息。那是我想不出来的胶水。
举个例子。首先,方面的接口。此注释将仅应用于方法。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DayOfWeek {
public String[] names();
}
现实施方面:
@Aspect
public class DayOfWeekAspect {
public DayOfWeekAspect() {
}
@Around("execution(* *(..)) && args(session,..) && @annotation(edu.berkeley.jazztwo.aspect.ams.roles.DayOfWeek)")
public Object dayOfWeek(ProceedingJoinPoint joinPoint, HttpSession session) throws Throwable {
String[] names = this.getNames(this.getAnnotatedMethod(joinPoint));
for (String name : names) {
System.out.println("DayOfWeek: " + name);
}
// Do whatever you want with the aspect parameters
if (checkSomething(names)) {
// in some cases, go to a different view/controller
return new ModelAndView(...);
}
// Just proceed into the annotated method
return joinPoint.proceed();
}
private Method getAnnotatedMethod(JoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
return methodSignature.getMethod();
}
private String[] getNames(Method method) {
DayOfWeek aspect = method.getAnnotation(DayOfWeek.class);
if (aspect != null) {
return aspect.names();
}
return new String[0]; // no annotation, return an empty array
}
}
然后将方面应用到某个方法
@DayOfWeek(names = {"Monday", "Wednesday", "Friday"})
@RequestMapping(value = "foo", method = RequestMethod.GET)
public ModelAndView foo(...) {
...
}
然后,如果您像我一样,会挠头想知道为什么它不起作用。您终于记得(如果您使用的是 XML 配置)您必须在配置中的某处实例化 bean:
<bean id="dayOfWeekAspect" class="com.foo.bar.DayOfWeekAspect" />
然后您可以调用 foo,它应该在星期一、星期三和星期五打印出来。
我有一个名为“@ProgressCheck”的注释,我们可以将其放在控制器上以检查应用程序的进度。如果应用程序已经提交或延迟,则它会将用户带到适合该情况的页面。
注解界面为:
@Retention(RetentionPolicy.RUNTIME)
public @interface ProgressCheck {
}
该注释的 "implementation" 类似于:
@Around("execution(* *(..)) && args(session,..) && @annotation(com.foo.aspects.progress.ProgressCheck)")
public Object progressCheck(ProceedingJoinPoint pjp, HttpSession session) throws Throwable {
Applicant applicant = this.applicationSessionUtils.getApplicant(session);
Application application = this.applicationSessionUtils.getApplication(session);
switch (this.applicantService.getProgress(applicant)) {
case SUBMITTED:
return this.modelAndViewFactory.gotoStatusPage(applicant, application);
case LATE:
return this.modelAndViewFactory.gotoDeadlineMissed(applicant, application);
default:
case IN_PROGRESS:
return pjp.proceed();
}
}
基于会话和数据库中的值,注解的 "implementation" 允许用户继续进入控制器,或根据需要将它们重定向到另一个 ModelAndView。
我想为注释提供参数,然后在此 "implementation" 逻辑中,使用这些参数进一步调整决策。根据这个逻辑,在不知道应用注解的位置的情况下,我如何访问这些参数?或者我应该使用另一种方法吗?
诀窍是查询方面的 JoinPoint 对象以获取有关被注释的方法(或 class 或其他)的注释信息。那是我想不出来的胶水。
举个例子。首先,方面的接口。此注释将仅应用于方法。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DayOfWeek {
public String[] names();
}
现实施方面:
@Aspect
public class DayOfWeekAspect {
public DayOfWeekAspect() {
}
@Around("execution(* *(..)) && args(session,..) && @annotation(edu.berkeley.jazztwo.aspect.ams.roles.DayOfWeek)")
public Object dayOfWeek(ProceedingJoinPoint joinPoint, HttpSession session) throws Throwable {
String[] names = this.getNames(this.getAnnotatedMethod(joinPoint));
for (String name : names) {
System.out.println("DayOfWeek: " + name);
}
// Do whatever you want with the aspect parameters
if (checkSomething(names)) {
// in some cases, go to a different view/controller
return new ModelAndView(...);
}
// Just proceed into the annotated method
return joinPoint.proceed();
}
private Method getAnnotatedMethod(JoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
return methodSignature.getMethod();
}
private String[] getNames(Method method) {
DayOfWeek aspect = method.getAnnotation(DayOfWeek.class);
if (aspect != null) {
return aspect.names();
}
return new String[0]; // no annotation, return an empty array
}
}
然后将方面应用到某个方法
@DayOfWeek(names = {"Monday", "Wednesday", "Friday"})
@RequestMapping(value = "foo", method = RequestMethod.GET)
public ModelAndView foo(...) {
...
}
然后,如果您像我一样,会挠头想知道为什么它不起作用。您终于记得(如果您使用的是 XML 配置)您必须在配置中的某处实例化 bean:
<bean id="dayOfWeekAspect" class="com.foo.bar.DayOfWeekAspect" />
然后您可以调用 foo,它应该在星期一、星期三和星期五打印出来。