Spring container and spring MVC container
1. Question: why not use spring to manage all classes?
In our configuration of spring MVC, why does the controller not be directly managed by spring, but the spring MVC container should be managed separately
2. The relationship between spring container and spring MVC
Spring container is a parent-child relationship with spring MVC container. The child container can access the objects of the parent container, but the parent container can’t access the classes of the child container. If we scan all classes directly in the spring MVC configuration file and hand over service, Dao and controller to spring MVC for management, but if spring is used to manage contoller, it can’t access this class, because Co The ntoller is in the spring MVC container. If you configure spring to scan all classes directly, including controller, but do not configure spring MVC, the request sent by the server will have a 404 problem, because it can’t find the controller and spring can’t inject the controller
Example.
Configure in applicationContext-service.
<! -- Scan package to load Service implementation classes -->
<context:component-scan base-package="com.easybuy"></context:component-scan>
will scan for @Controller, @Service, @Repository, @Compnent
Springmvc.Xml does not scan.
Conclusion: springmvc. cannot provide services because there is no controller object in the springmvc subcontainer.
3. Why not directly SpringMVC.xml Scan all classes in?
In principle, we can leave the service, Dao and controller to spring MVC for management, and just let it scan all packages directly in the spring MVC configuration file. However, for the sake of future expansion, spring and spring MVC are configured separately, and spring manages the service, which is conducive to later expansion. Even if multiple struct2 are added in the future, the original configuration will not be affected
Read More:
- Can’t Dubbo’s @ service be injected into the spring container?
- When the spring MVC project is running on idea, an error is reported when the controller is a null pointer
- File “manage.py“, line 17 ) from exc ^ SyntaxError: invalid syntax
- Error: path not specified when the docker 1.7 file is uploaded to the container in CentOS
- Why do we not need to write implementation classes in mybatis to get the objects we need
- Spring boot prompt could not resolve placeholder in string value
- Error running docker container: starting container process caused “exec: \“python\“: executable file
- When Python Django creates a database, can’t open file ‘ manage.py ‘: [Errno 2] No such file or directory
- Why fan error appears in ThinkPad Lenovo IBM notebook
- Get connection timeout retry: 1 MySQL errorcode 0, state 08s01 docker container accessing MySQL container is very slow and sometimes interrupted
- The 406 status code can not find acceptable representation is returned in spring MVC
- Uncaught Error: _registerComponent(…): Target container is not a DOM element
- Why must microservices have gateways?
- Sata3.0 host controller IP
- Not registered via @EnableConfigurationProperties or marked as Spring component
- Spring initializer error: initialization failed for ‘ https://start.spring.io ‘
- An error occurred when starting Tomcat in Eclipse: the sub container failed to start
- Why interview requires reading the source code
- Installing opencv Python in docker container
- Spring MVC error: could not find acceptable representation