The difference between spring's <context:annotation-config> and <context:component-scan>

the difference:

  • 1. <context:annotation-config>: [Autowired] is used to activate (@Autowired) beans that have been registered in the spring container (whether by xml or by package sanning).
  • 2. <context:component-scan>: [Automatic registration + assembly] In addition to the function of <context:annotation-config />, <context:component-scan> also has the function of automatically adding @component, @service, The function of registering objects annotated with @Repository in the spring container.
  • <context:component-scan/>包含了<context:annotation-config/>

<context:component-scan> scan content:

  • 1、配置:<context:component-scan base-package="com.test" />

              Effect: Scan the classes annotated with @component, @service, @Repository, etc. under the com.test package and register them in the spring container

  • 2、配置:<context:component-scan base-package="com.test"  use-default-filters="false"/>

            Effect: nothing is scanned, and no classes are registered with spring

  • 3. Configuration:

<context:component-scan base-package="com.test" use-default-filters="false">
  <!-- 扫描符合@Service的类 -->
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

              Effect: Scan the classes annotated with @service under the com.test package and register them in the spring container

  • 4. Configuration:

<context:component-scan base-package="com.test" use-default-filters="false">
 <context:include-filter type="regex" expression="com.test.entity.*" />
</context:component-scan>

              Effect: Scan all the classes under the com.test.entity package and register them in the spring container

 

  • 5. Configuration:

<context:component-scan base-package="com.test" >

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

              Effect: Scan the classes annotated with @component, @Repository, etc. under the com.test package, and register them in the spring container (note: classes that do not contain @service annotations)

  • 6. Configuration:
    <context:component-scan base-package="com.test" use-default-filters="false">
      <!-- Scan classes that conform to @Service @Repository -->
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
         <context:include-filter type= "regex" expression="com.test.entity.*"/>
    </context:component-scan>
    Effect: Scan @Service, @Repository annotated entities under the com.test package and entities under the com.test.entity package All entities [other annotations such as @component will not be scanned, because use-default-filters="false" turns off the default scanning method]
  • Summary:
     use-default-filters is true by default, that is, the @component, @service, @Repository annotations under the base-package package and subpackages will be scanned by default. If it is set to false, @component, @service, @ will not be scanned. Repository annotation
    context: include-filter takes base-package as the root directory, and customizes adding beans that need to be scanned [that is, beans that can be scanned without adding any annotations]
  • This is just a personal summary, please point out any mistakes
the difference:
  • 1. <context:annotation-config>: [Autowired] is used to activate (@Autowired) beans that have been registered in the spring container (whether by xml or by package sanning).
  • 2. <context:component-scan>: [Automatic registration + assembly] In addition to the function of <context:annotation-config />, <context:component-scan> also has the function of automatically adding @component, @service, The function of registering objects annotated with @Repository in the spring container.
  • <context:component-scan/>包含了<context:annotation-config/>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326487380&siteId=291194637