如何在AspectJ中获取joinPoint.proceed()结果的参数?

时间:2015-09-24 10:19:53

标签: java aspectj

我有以下建议代码:

    @Around("annotatedMethod()")
    public Object aroundGetPanel(ProceedingJoinPoint joinPoint) throws Throwable    
    {       
    Object result = joinPoint.proceed();        
    return result;
    }

执行上述方法的方法是:

    public Person getPerson(String id){
          return new Person(1,"Maialen");
    }

    public class Person {
        private Integer id = null;
        private String name = null;
        public Person(Integer id,String name){
            this.setId(id);
            this.setName(name);
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return nombre;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

如何获得对象结果(Person)的参数? 用反射?使用注释?

1 个答案:

答案 0 :(得分:1)

我发现如何通过反思来做到这一点:

    Class<?> clazz = result.getClass();
    Field field = org.springframework.util.ReflectionUtils.findField(clazz, "name");
    org.springframework.util.ReflectionUtils.makeAccessible(field);
    String name=field.get(result).toString();

但我更喜欢用注释来做。有模式吗?