Spring AOP +自定义注释+ SpEL

时间:2020-10-28 21:41:58

标签: annotations aop spring-aop spring-annotations spring-el

我正在尝试以与@Cachable工具类似的方式在自定义注释中实现SpEL。

@Cacheable(cacheNames="books", key="#isbn.rawNumber")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

这是我的代码示例:

自定义注释:

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface StoreRequest {
    
    public boolean enable() default true;
    
    public String name() default "" ;

}

方面:

@Before("@annotation(storeRequest)")
public void saveRequest(JoinPoint ijoinPoint, StoreRequest storeRequest) {
    if (storeRequest.enable()) {
        System.out.println("Aspect in enabled for "+ storeRequest.name());
    } else {
            System.out.println("Aspect in disabled for "+ storeRequest.name();
    }

}

服务:

@Override
    @StoreRequest(name = "#name")
    public void aspectCheck(String name){
    ....
    ...
    ...
}

比方说,aspectCheck方法的参数值是BOB,这段代码显示为“为#name启用了方面”。我期望的是“启用BOB方面”。

现在,我知道Spel不能神奇地工作,我需要为自定义注释编写额外的代码行以实现SpEL。如果有人可以帮助我,我将不胜感激。

0 个答案:

没有答案