将注释工作的结果传递给带注释的方法

时间:2016-04-02 12:09:45

标签: java spring annotations aop aspect

注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Multipart {

    Class acceptClass();

}

带注释的方法:

@Multipart (acceptClass = SomeClass.class)
public void someMethod(SomeClass a){
  //do stuff..
}

MultipartAspect:

@Aspect
public class MultipartAspect {

    @Autowired(required=true)
    private HttpServletRequest request;

    @Pointcut(value = "@annotation(Multipart)", argNames = "multipart")
    public void before(JoinPoint jp, Multipart multipart) {}

    @Before("before()")
    public SomeClass doStuffBeforeThing() {
       SomeClass sc = new SomeClass(); //object of passed class
       //do something..
       return sc;                      //return this to annotated method(somemethod)
    }

}

我想在方法工作之前执行注释,创建传递类的对象(SomeClass)以及此类的传递对象到注释方法。我可以这样做吗?

1 个答案:

答案 0 :(得分:0)

您应该使用@Around建议而不是@Before。