How Java Annotaions work in Spring Framework [duplicate]


How Java Annotaions work in Spring Framework [duplicate]



This question already has an answer here:



When I work with Spring Framework, I use Java annotations to mark a class to be a controller, methods with @RequestMapping() and so on. I'm confused as to which class read these Anotations and what technique is used. I'm thinking of Java Reflection. Is that right?



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Annotations are way to associating meta data with class which is extracted by the underlined framework docs.oracle.com/javase/tutorial/java/annotations
– Rajat Verma
Jun 30 at 7:14






Yes, Spring uses reflection to find all annotations on classes and methods and act accordingly.
– JB Nizet
Jun 30 at 7:16





@JBNizet Do you know that class name, I have clone spring source to research but I can't find that classes
– Trần Văn Thành
Jun 30 at 7:20





No. And there are probably many of them: the core module doesn't deal with web-specific stuff, for example. If you want to learn how annotations work, you'd better read the annotation tutorial, rather than trying to understand how Spring uses them.
– JB Nizet
Jun 30 at 7:21




3 Answers
3



Yes, Spring uses Java Reflection to evaluate the information which you provide in the annoations and takes care of proper configuration. In the case of @RequestMapping Spring MVC and Spring WebFlux, both come with support for this annotation (see here).


@RequestMapping



Excerpt:



Both Spring MVC and Spring WebFlux support this annotation through a RequestMappingHandlerMapping and RequestMappingHandlerAdapter in their respective modules and package structure ...



Yes and not, Spring of course use reflection in order to use the data in your annotation but the real magic is the role of HandlerMapping and HandlerAdapter that provide mapping from your url and a controller (HandlerMapping) and adapt the specific servlet request and response to your controller method. In partucular you have a RequestMappingHandlerMapping and a RequestMappingHandlerAdapter automaticaly registred in your spring boot app or in your lagacy spring app if you use @EnableWebMvc or . Those are the bean that via reflection give at your the magic of sprign. It works even in Spring WebFlux even if I suggest to use endpoint declared in functional programming



In a nutshell, Spring has a concept called BeanPostProcessor which is responsible (implicitly or explicitly) for processing annotations or in general any work/modification of spring beans.



There are many annotations supported by spring framework, some are for web only, others for caching or scheduling, spring itself is comprised of many frameworks that help in different areas.



When application context gets loaded, these bean postprocessors also get recognized by spring (technically they're just like other spring beans, but since they implement org.springframework.beans.factory.config.BeanPostProcessor interface spring treats them differently, although also puts them onto application context.


org.springframework.beans.factory.config.BeanPostProcessor



So these BPP are called for each bean and allow modification of the bean.



Usually, each BPP does one of the following:



Wraps the initial bean into proxy to provide additional functionality. Proxies are usually done with java.lang.reflect.Proxy or cglib. Or in some bean post processors a programmatic access to spring aop is used to achieve the same effect. Since the proxy is not created for each bean, the bean is supposed to be "analyzed" and only if certain annotation presents on the bean or its method, the bean gets processed by BPP. This analysis is done by each Bean Post Processor.


java.lang.reflect.Proxy


cglib



Modify the Bean, set up its dependencies, etc. For example @Autowired works this way.


@Autowired



Deploys other code only because the bean contains annotations, for example if you put @Scheduled annotation, spring will create a timer that will, in different thread, call periodically the method marked with this annotation.


@Scheduled



You can read much more about this in spring documentation or in this question

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux