<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring MVC:`<context:annotation-config>`與`<context:component-scan>` > 原文:[https://howtodoinjava.com/spring-mvc/spring-mvc-difference-between-contextannotation-config-vs-contextcomponent-scan/](https://howtodoinjava.com/spring-mvc/spring-mvc-difference-between-contextannotation-config-vs-contextcomponent-scan/) 在先前的文章中,我們在 [**Spring MVC**](https://howtodoinjava.com) 中學到的東西很少。 在這些教程中,我確實使用了`<context:annotation-config>`或`<context:component-scan>`之類的標簽,但是我沒有對這些標簽進行詳細說明。 我正在寫這篇文章,專門列出標簽`<context:annotation-config>`和`<context:component-scan>`之間的區別,以便將來使用它們時,您將知道自己在做什么。 1)兩個標簽之間的第一個大區別是`<context:annotation-config>`是**用于在應用程序上下文**中激活已注冊 bean 中的應用注解。 請注意,bean 是否通過哪種機制注冊都沒有關系,例如使用`<context:component-scan>`或在`application-context.xml`文件本身中定義。 2)第二差異是由第一差異本身驅動的。 它確實**在上下文中注冊了 bean,并且還掃描了 bean 內部的注解并激活了它們**。 所以`<context:component-scan>`; 做`<context:annotation-config>`的工作,但另外它會掃描軟件包并在應用程序上下文中注冊 bean。 ## `<context:component-scan>` vs `<context:annotation-config>`使用示例 我將通過一些示例詳細說明這兩個標簽,這些示例對我們更有意義。 為了使示例簡單,我僅創建 3 個 bean,然后嘗試以各種方式在配置文件中對其進行配置,然后我們將看到控制臺中各種配置之間的區別,其中將輸出輸出。 供參考,以下是 3 個 bean。 `BeanA`另外引用了`BeanB`和`BeanC`。 ```java package com.howtodoinjava.beans; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @SuppressWarnings("unused") @Component public class BeanA { private BeanB beanB; private BeanC beanC; public BeanA(){ System.out.println("Creating bean BeanA"); } @Autowired public void setBeanB(BeanB beanB) { System.out.println("Setting bean reference for BeanB"); this.beanB = beanB; } @Autowired public void setBeanC(BeanC beanC) { System.out.println("Setting bean reference for BeanC"); this.beanC = beanC; } } //Bean B package com.howtodoinjava.beans; import org.springframework.stereotype.Component; @Component public class BeanB { public BeanB(){ System.out.println("Creating bean BeanB"); } } //Bean C package com.howtodoinjava.beans; import org.springframework.stereotype.Component; @Component public class BeanC { public BeanC(){ System.out.println("Creating bean BeanC"); } } ``` `BeanDemo`類用于加載和初始化應用程序上下文。 ```java package com.howtodoinjava.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanDemo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml"); } } ``` 現在,我們開始編寫包含變體的配置文件`"beans.xml"`。 在下面的示例中,我將省略模式聲明,以繼續關注配置本身。 #### a)僅定義`bean`標簽 ```java <bean id="beanA" class="com.howtodoinjava.beans.BeanA"></bean> <bean id="beanB" class="com.howtodoinjava.beans.BeanB"></bean> <bean id="beanC" class="com.howtodoinjava.beans.BeanC"></bean> Output: Creating bean BeanA Creating bean BeanB Creating bean BeanC ``` 在這種情況下,因為我們沒有使用任何屬性/引用屬性,所以創建了所有 3 個 bean,并且沒有在`BeanA`中注入任何依賴項。 #### b)定義`bean`標簽和`ref`屬性 ```java <bean id="beanA" class="com.howtodoinjava.beans.BeanA"> <property name="beanB" ref="beanB"></property> <property name="beanC" ref="beanC"></property> </bean> <bean id="beanB" class="com.howtodoinjava.beans.BeanB"></bean> <bean id="beanC" class="com.howtodoinjava.beans.BeanC"></bean> Output: Creating bean BeanA Creating bean BeanB Creating bean BeanC Setting bean reference for BeanB Setting bean reference for BeanC ``` 現在,還創建并注入了 bean。 難怪。 #### c)僅使用`<context:annotation-config/>` ```java <context:annotation-config /> //No Output ``` 正如我已經說過的,`<context:annotation-config />`僅在已經發現并注冊的 bean 上激活注解。 在這里,我們沒有發現任何 Bean,因此什么也沒發生。 #### d)將`<context:annotation-config>`與`bean`聲明一起使用 ```java <context:annotation-config /> <bean id="beanA" class="com.howtodoinjava.beans.BeanA"></bean> <bean id="beanB" class="com.howtodoinjava.beans.BeanB"></bean> <bean id="beanC" class="com.howtodoinjava.beans.BeanC"></bean> Output: Creating bean BeanA Creating bean BeanB Setting bean reference for BeanB Creating bean BeanC Setting bean reference for BeanC ``` 在上面的配置中,我們使用`<bean>`標簽發現了 bean。 現在,當我們使用`<context:annotation-config />`時,它只需激活`@Autowired`注解,就會在`BeanA`內部進行 bean 注入。 #### e)僅使用`<context:component-scan/>` ```java <context:component-scan base-package="com.howtodoinjava.beans" /> Output: Creating bean BeanA Creating bean BeanB Setting bean reference for BeanB Creating bean BeanC Setting bean reference for BeanC ``` 上面的配置完成了我之前在文章開頭提到的兩件事。 它會進行 Bean 發現(在基本包中搜索`@Component`注解),然后激活其他注解(例如`Autowired`)。 #### f)同時使用`<context:component-scan>`和`<context:annotation-config>` ```java <context:annotation-config /> <context:component-scan base-package="com.howtodoinjava.beans" /> <bean id="beanA" class="com.howtodoinjava.beans.BeanA"></bean> <bean id="beanB" class="com.howtodoinjava.beans.BeanB"></bean> <bean id="beanC" class="com.howtodoinjava.beans.BeanC"></bean> Output: Creating bean BeanA Creating bean BeanB Setting bean reference for BeanB Creating bean BeanC Setting bean reference for BeanC ``` 奇怪! 通過上述配置,我們兩次發現了 bean,并且兩次激活了注釋。 但是輸出只打印了一次。 為什么? 因為 spring 具有足夠的智能,如果使用相同或不同的方式多次注冊任何配置處理,則僅注冊一次。酷! 因此,在這里,我將結束本教程,并希望當您在下一個項目中使用這些標簽時,它們將為您提供更清晰的畫面。 隨意下載源代碼并使用它。 [下載源碼](https://drive.google.com/file/d/0B7yo2HclmjI4dHFyeU9JVjFsa2s/edit?usp=sharing) **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看