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?
-
1Annotations are way to associating meta data with class which is extracted by the underlined framework https://docs.oracle.com/javase/tutorial/java/annotations/ – Rajat Jun 30 '18 at 07:14
-
2Yes, Spring uses reflection to find all annotations on classes and methods and act accordingly. – JB Nizet Jun 30 '18 at 07: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 '18 at 07: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 '18 at 07:21
3 Answers
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).
Excerpt:
Both Spring MVC and Spring WebFlux support this annotation through a RequestMappingHandlerMapping and RequestMappingHandlerAdapter in their respective modules and package structure ...

- 2,872
- 7
- 36
- 57
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

- 4,199
- 2
- 24
- 23
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.
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
orcglib
. 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.Modify the Bean, set up its dependencies, etc. For example
@Autowired
works this way.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.
You can read much more about this in spring documentation or in this question

- 39,963
- 4
- 57
- 97