Spring annotations automatically inject beans

Source: Internet
Author: User

We know that when using spring annotations, the configuration is as follows:

[HTML]View Plaincopy
    1. <context:annotation-config />
    2. <context:component-scan base-package="cn.itkt"></context:component-scan >

In this case, all classes under the COM package and all of its sub-packages will automatically be included in the spring container if they contain annotations such as @component, @Controller, @Service, @Repository, but each class is annotated. Sometimes it's tedious, but spring also gives us the ability to add annotations automatically to the class. The configuration is as follows:

[HTML]View Plaincopy
  1. <context:component-scan base-package="cn.itkt" use-default-filters="false">
  2. <context:include-filter type="regex" expression="cn.itkt.*.service.*.*" />
  3. <context:exclude-filter type="annotation" expression=" Org.springframework.stereotype.Controller " />
  4. </context:component-scan>

We can see that the context:include-filter tag and the context:exclude-filter tag are added.

Context:include-filter

The meaning of this tag is that all of the classes it scans are automatically annotated and incorporated into the spring container, such as a class that

[Java]View Plaincopy
    1. Public class Studentservice implements Istudentservice {
    2. }

Then the tag is used to add @component annotations to the Studentservice class, and the Bean's ID is studentservice.

[Java]View Plaincopy
    1. @Component ("Studentservice")
    2. Public class Studentservice implements Istudentservice {
    3. }

Context:exclude-filter

The implication of this tag is that all classes scanned are excluded from the spring container.

However, it is important to note that with automatic injection, the class name cannot be the same (even if the package name is different) because the ID is the same as the class name when auto-injection, so if the two class names are the same, the bean ID will be the same.

If the class name must be the same, it can only be one of the classes, manually annotated and changed to a different name.

Spring annotations automatically inject beans

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.