Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The solution to the conflict between Spring and SpringMVC scanning Annotation Class

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What this article shares with you is the solution to the conflict between Spring and SpringMVC scanning annotations. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Spring and SpringMVC scan annotation classes conflict with the most correct configuration

In the applicationContext.xml in the main container, exclude the comments from Controller

In springmvc.xml, remove the Service comment

Because the context of spring is a parent-child container, conflicts will occur. The parent container is generated by ServletContextListener, and the child container is generated by springMVC. The child container Controller is equipped with an instance of @ Service annotation when it is scanned and assembled, and the instance should be initialized by the parent container to ensure enhanced transaction processing, so the original Service will be obtained at this time (no transaction enhancement processing, so there is no transaction processing capacity.

Another way is to change the service layer to xml configuration, which is also disguised so that springmvc cannot scan service and can only rely on the parent window, that is, ServletContextListener, for initialization, which is also transactional.

You can also use direct scanning.

It is easier to scan directly, but the transaction can not be processed, so you need to add annotations to declare the transaction at a specific level, such as adding @ Transactional in the dao layer and service layer

Testing of several different configurations

(1) only configure the following in applicationContext.xml

The startup is normal, but no request will be intercepted. In short, @ Controller fails with a 404 error.

(2) configure only in springmvc.xml

The startup is normal and the request is normal, but things fail, that is, they cannot be rolled back.

(3) configure both in applicationContext.xml and springmvc.xml

The startup is normal, the request is normal, and the thing is invalid and cannot be rolled back.

(4) configure in applicationContext.xml as follows

Configure in springmvc.xml as follows

Or according to the most correct configuration of applicationContext.xml, exclude the comments of Controller, springmvc.xml, and remove the comments of Service

At this point, the startup is normal, the request is normal, and things are normal.

Note phenomena in Spring and SpringMVC annotation scanning

Conflicts sometimes occur when Springmvc and Spring are set to automatically scan folders and automatically inject bean

Method

1:springmvc setting scans only controller

2:spring setting does not scan controller

Code:

Springmvc scan settings only scan controller spring scan to not scan controller to prevent repeated injection of bean management (no error occurs) these are the solutions to the conflict between Spring and SpringMVC scanning annotations. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report