We know that when using spring annotations, the configuration is as follows:
[HTML]View Plaincopy
- <context:annotation-config />
- <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
- <context:component-scan base-package="cn.itkt" use-default-filters="false">
- <context:include-filter type="regex" expression="cn.itkt.*.service.*.*" />
- <context:exclude-filter type="annotation" expression=" Org.springframework.stereotype.Controller " />
- </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
- Public class Studentservice implements Istudentservice {
- }
Then the tag is used to add @component annotations to the Studentservice class, and the Bean's ID is studentservice.
[Java]View Plaincopy
- @Component ("Studentservice")
- Public class Studentservice implements Istudentservice {
- }
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