Various Spring Ads XML Annotation Ads

There seem to be several XML tags to tell Spring to use annotations:

<context:annotation-config/> <context:component-scan base-package="org.example" /> <mvc:annotation-driven /> <tx:annotation-driven transaction-manager="transactionManager" /> 

I think the first tag says that it is looking at annotations, the second says which package to scan (and possibly exclude / enable, what to look for). Then perhaps the third one says that it scans the controller classes. And the latter for scanning data access annotations.

My problem is this:

  • I would think that labels 1 and 2 can be combined into one. Therefore, I do not know why they are separated.
  • Tags 3 and 4 seem redundant for 1 and 2.

Can someone give me a breakdown of what each tag does and why they are not redundant? And if there are any other XML tag annotations, let me know.

EDIT

After further investigation, I believe that I found additional information:

  • The <context:annotation-config/> allows you to use annotations for variables, constructors, and methods (e.g. @Autowired, @Resource, etc.).
  • The <context:component-scan base-package="org.example" /> allows you to use annotations for classes (for example, @Repository, @Controller, etc.).
  • The <tx:annotation-driven transaction-manager="transactionManager" /> tag actually includes the @Transactional tag (the <context:annotation-config/> tag allows the @Transactional tag, but it doesn’t).

Still not quite sure about <mvc:annotation-driven /> . I think maybe this will add extra JSON support, etc.

+4
source share
2 answers
  • Difference between annotation-config and component-scan

    a) <context:annotation-config/> only searches for annotations on beans in the same application context in which it is defined. This means that if you put <context:annotation-config/> in the WebApplicationContext for the DispatcherServlet, it only checks @Autowired beans on your controllers, and not on your services. See Section 15.2, “DispatcherServlet” for more information.

    b) Spring provides the ability to automatically detect "stereotyped" classes and register the corresponding BeanDefinitions with the ApplicationContext. Automatically detecting these classes and registering the corresponding beans requires the inclusion of a component-scan element in XML, where basePackage will be a common parent package (or, alternatively, a comma-separated list that includes the parent package of each class can be specified).

  • tx:annotation-driven

    You provide a transaction manager directly inside the element. annotation-config and component-scan will not.

  • mvc:annotation-driven

    This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans, which are necessary for Spring MVC to send requests to @Controllers. The tag configures these two beans with reasonable defaults based on what is present in your classpath. Read more in the Spring doc .

+2
source

I would think that labels 1 and 2 can be combined into one. Therefore, I do not know why they are separated.

For backward compatibility reasons. Older Spring applications should continue to work, and merging tags (which were introduced in different versions of Spring) would break this by changing the default behavior.

Tags 3 and 4 seem redundant to 1 and 2.

See above. 4 tags make a little different, but complement each other. Yes, if Spring were designed from scratch, there would be fewer, but the functionality should remain separate.

Summarizing:

<context:annotation-config/> allows you to maintain annotation in context. This was added as part of Java 5 support, while Spring still supported Java 1.4.

<context:component-scan base-package="org.example" /> allows you to automatically scan and configure beans instead of using explicit declarations. This was added in Spring 2.5.

<mvc:annotation-driven /> is odd. This is not required to support the annotated controller (they work by default). What he does is actually disable the old style of the non-annotated controller, and also add support for things like JSON. This is necessary because older applications still use the old controller style.

<tx:annotation-driven> is required because Spring supports many different transaction demarcation styles, one of which is the annotation style. This is the most popular style, but by no means the only one.

+1
source

Source: https://habr.com/ru/post/1380020/


All Articles