1

I am reviewing open source spring projects. I am confused about the use of annotations around here. I want to ask to clarify this.

@Target(ElementType.METHOD)
@Retention(RUNTIME)
@Bean
public @interface Merge {

   
    @AliasFor("targetRef")
    String value() default "";
   
    @AliasFor("value")
    String targetRef() default "";

    Placement placement() default Placement.APPEND;
   
    int position() default 0;
    
    Class<MergeBeanStatusProvider> statusProvider() default MergeBeanStatusProvider.class;

    boolean early() default false;

}

An annotation has been created here named Merge. It has different parameters and default values.

@Configuration
public class LocalConfiguration {

    @Merge(targetRef = "mergedList", early = true)
    public List<String> blLocalMerge() {
        return Arrays.asList("local-config1", "local-config2");
    }
}

And this is usage of @Merge annotation in any class I choosed randomly.

When I examined the code, I could not find any class related to the implementation of Merge annotation. By the way, this problem I'm having isn't just about this annotation. Almost all the annotations I have examined are used without being implemented in any way. I think I will understand the others if we start from this annotation. What does this anotation do? What kind of message does it give to the place where it is used. How does the application understand what that annotation does in runtime without being implemented anywhere.

Thanks.

1 Answer 1

4

Annotations don't have implementations. They are processed by external classes or tools depending on the RetentionPolicy. In this case, the Merge annotation has Runtime retention so it will be available via reflection once the class is loaded. At runtime any interested party (in this case I assume the Spring Framework) can use getAnnotations on your LocalConfiguration class to detect the Merge annotation and take whatever action that needs to be taken. The possibilities are really up to the framework that defined the annotation. A lot of Spring injection works like this with annotations but they are also used by many other frameworks such as Hibernate, Jersey, etc. The main idea is that annotations act as markers on specific code points to be used by an external entity at a later point.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Simeon G. I understand what you said, but still not clear.Do parameters such as placement, position, early that I have defined in annotation make sense for the spring framework?. Who uses these parameters, where and how, I cannot find the answer.
Did you create the Merge annotation? If you did then it most likely makes no sense to the spring framework directly since Spring won't know about it.
Yes, I created. It's not depends ant other frameworks.
Ah. So then you will be processing that annotation. The Configuration annotation on your LocalConfiguration class is handled by Spring though. Spring will just ignore your Merge annotation.
When I looked at this opensource project, I couldn't see that the @Merge annotation and many other custom annotations were being processed in any way. Actually I don't know what works without being processed. Or I don't know how it is processed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.