Spring and Spring MVC package scan

Source: Internet
Author: User

In the core concept of the spring overall framework, the container is the core idea, which is used to manage the bean's entire life cycle, and in a project, the container may not have only one, spring can include multiple containers, and the container has the upper and lower layer relationships, One of the most common scenarios is the introduction of the spring and SPRINGMVC frameworks in a project, which is actually two containers, spring is the parent container, and SPRINGMVC is its child container. and the bean registered in the Spring parent container is visible to the SPRINGMVC container, and the bean registered in the SPRINGMVC container is invisible to the spring parent container, that is, the child container can see the registered bean in the parent container, and vice versa.

Both the SPRINGMVC configuration file and the spring configuration file can be configured with a package scan, as follows:

<context:component-scan base-package= "Com.xxx"/>

From the reference manuals provided in spring, we learned that the functionality of this configuration is to scan all classes of @component annotations under the configured Base-package package, and automatically register them in the container, while also scanning @controller, @Service, @ Respository these three annotations, as they are inherited from @component.

In the project we often see the following configuration, in fact, with the above configuration, this can be omitted, because the above configuration will open the following configuration by default. The following configuration declares annotations such as @required, @Autowired, @PostConstruct, @PersistenceContext, @Resource, @PreDestroy, and so on by default.

<context:annotation-config/>

In addition, there is a SPRINGMVC related to the following configuration, verified that this is SPRINGMVC must be configured, because it declares @requestmapping, @RequestBody, @ResponseBody and so on. Also, the configuration defaults to loading a lot of parameter binding methods, such as JSON conversion parser.

<mvc:annotation-driven/>

Let's take a detail of the reason why the spring and SPRINGMVC container conflict is there?

We have a total of two containers for spring and SPRINGMVC, and their profiles are applicationcontext.xml and Applicationcontext-mvc.xml respectively.

1. <context:component-scan base-package= "Com.hafiz.www"/> is configured in Applicationcontext.xml, which is responsible for scanning and registering all the beans that need to be registered.

2. Configure <mvc:annotation-driven/> in Applicationcontext-mvc.xml, responsible for the use of SPRINGMVC related annotations.

3. Start the project we found that SPRINGMVC could not jump, set log print level to debug, and found that the request in the SPRINGMVC container did not appear to be mapped to a specific controller.

4. Configure <context:component-scan base-package= "Com.hafiz.www"/> in Applicationcontext-mvc.xml, reboot, verify success, SPRINGMVC Jump is valid.

Let's look at the specific reasons, look at the source code, starting from Springmvc Dispatcherservlet, we found that SPRINGMVC initialization, we will look for all the SPRINGMVC containers using @controller annotations of the Bean, To determine if it is a handler. 1, 22 step configuration so that the current SPRINGMVC container does not register the bean with the @controller annotation, but all the beans with @controller annotations are registered in the parent container of spring, so SPRINGMVC cannot find the processor , the jump cannot be made.

According to the official recommendation based on different business modules to partition different containers register different types of bean:spring the parent container is responsible for registering all other non-@controller annotations of the bean, and SPRINGMVC is only responsible for the bean registration of @controller annotations, Make them accountability and clear the border. Configure as follows

1. Configure in Applicationcontext.xml:

< register non-@controller annotations in the spring container----
<context:component-scan base-package= "Com.hafiz.www" > <context:exclude-filter type= "annotation" expression= "Org.springframework.stereotype.Controller"/></context:component-scan>

Configuration in 2.applicationcontext-mvc.xml

<!--only beans with @controller annotations are registered in the SPRINGMVC container--
<context:component-scan base-package= "Com.hafiz.www" use-default-filters= "false" > <context: Include-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/></context: Component-scan>

Do not allow two scans to overlap. There are several reasons for this:

    1. Scan classes increase, project start-up time increases
    2. The method of @PostConstruct annotation callout is executed 2 times
    3. Will invalidate the transaction

What is the reason for the 3rd to invalidate things?
Using both SPRINGMVC and spring, there will be two containers in the project.
Spring is the parent container, which is initialized first; SPRINGMVC is a child container, which is initialized after. The child container can access the bean of the parent container, and the parent container cannot access the child container's bean.
After Springmvc, the service object is recreated and re-injected, and SPRINGMVC does not read the spring configuration file when the service object is created again, so there is no way to know that the service layer needs to create a proxy object. So the service created by SPRINGMVC is an ordinary object, not a dynamic proxy object.

How to resolve overlapping scans

    • Mode 1

That is, the SPRINGMVC mentioned above only scans the controller's package, and spring scans the other components.
The SPRINGMVC configuration is as follows:

SPRINGMVC only scans the class under the controller package under any directory or subdirectory under COM.XXX

Spring is configured as follows:

<context:component-scan base-package= "Com.xxx.**.service.impl,com.xxx.**.dao"/>
    • Mode 2

Specify or exclude certain classes with <context:include-filter/> or <context:exclude-filter/>
Suppose the SPRINGMVC scan is as follows:

The spring scan is as follows:

<context:component-scan base-package= "Com.xxx"/>

Then overlap, SPRINGMVC scans the service and DAO, so you can modify the configuration of the SPRINGMVC:

<context:component-scan base-package= "com.xxx" >    <context:exclude-filter type= "annotation" expression = "Org.springframework.stereotype.Service"/>    <context:exclude-filter type= "annotation" expression= " Org.springframework.stereotype.Repository "/>  

The class of @service and @Repository two annotation annotations is excluded with <context:exclude-filter/>, meaning to tell SPRINGMVC if the class that touches the two annotation annotations is ignored directly when scanning, do not create and inject objects.

Way 1 is better than the way 2, scan the class less, the way 2 is just an example, looks a little superfluous. Deliberately letting SPRINGMVC scan the service and DAO packages, and telling him to ignore service and DAO while scanning, I just wanted to elicit <context:include-filter/> and <context: Exclude-filter/> this configuration. This configuration is only available for some special scenarios.

Resources

Http://www.cnblogs.com/hafiz/p/5875740.html

Spring and Spring MVC package scan

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.