Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pass method argument in custom annotation [SPR-13601] #18179

Closed
spring-issuemaster opened this issue Oct 22, 2015 · 2 comments
Closed

Cannot pass method argument in custom annotation [SPR-13601] #18179

spring-issuemaster opened this issue Oct 22, 2015 · 2 comments

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Oct 22, 2015

Chris Korakidis opened SPR-13601 and commented

I'm trying to use something similar to org.springframework.cache.annotation.Cacheable :

Custom annotation:

@Target(ElementType.METHOD)
     @Retention(RetentionPolicy.RUNTIME)
     @Documented
     public @interface CheckEntity {
         String message() default "Check entity msg";
         String key() default "";
     }

Aspect:

@Component
@Aspect
public class CheckEntityAspect {
    @Before("execution(* *.*(..)) && @annotation(checkEntity)")
    public void checkEntity(JoinPoint joinPoint, CheckEntitty checkEntity) {
        System.out.println("running entity check: " + joinPoint.getSignature().getName());
    }
}

Service:

@Service
@Transactional
public class EntityServiceImpl implements EntityService {
    @CheckEntity(key = "#id")
    public Entity getEntity(Long id) {
        return new Entity(id);
    }
}    

My IDE (IntelliJ) doesn't see anything special with the key = "#id" usage in contrast to similar usages for Cacheable where it's shown with different color than plain text. I'm mentioning the IDE part just as a hint in case it helps, it looks like the IDE is aware in advance about these annotations or it just realizes some connection which doesn't exist in my example.

The value in the checkEntity.key is '#id' instead of an expected number.
I tried using ExpressionParser but possibly not in the right way.

The only way to get parameter value inside the checkEntity annotation is by accessing the arguments array which is not what I want because this annotation could be used also in methods with more than one argument.

Any idea?


No further details from SPR-13601

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 23, 2015

Stéphane Nicoll commented

There is nothing that indicates this to be a bug in the Spring Framework. Questions have to be raised on stackoverflow, see https://spring.io/questions

A few hints nevertheless, IJ won't add syntax highlight because there is nothing that states this String of yours is a SpEL expression. You have to parse the expression (as the caching support does). You can find a bit more context in ExpressionEvaluator

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 23, 2015

Chris Korakidis commented

I already asked that in SO already (http://stackoverflow.com/questions/33024977/pass-method-argument-in-aspect-of-custom-annotation)
The only answer I got was regarding the implementation in Cache. The problem with that to my point of view is that the expression paring in Cache is too much coupled with Cache and I'm not sure how this would apply in my case (e.g. not sure what's the target object in my case e.t.c.).
I didn't find any example regarding custom annotations and el and wasn't sure if this is supported or not. My general assumption after reading a few resources regarding spel was that this is supposed to be supported, this is why I marked it as a bug instead of improvement.

P.S. It was easier being supported by SpringSource people when the forum was active, I have a feeling that cases with not much support from the dev community on SO (for various reasons, e.g. not so common cases), should be supported by SpringSource people given that they have the ability to provide answers even on questions the community is not familiar enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.