<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 4.10 類路徑掃描和管理的組件 本章中的大多數示例都使用了 XML 來指定配置元數據在 Spring 的容器中生產每一個 BeanDefinition。之前的章節(4.9 節,“基于注解的容器配置”)表述了如何通過代碼級 的注解來提供大量的配置信息。盡管在那些示例中,“基礎的”bean 的定義都是在 XML 文 件中來明確定義的,而注解僅僅進行依賴注入。本節來說明另外一種通過掃描類路徑的方式 來隱式檢測候選組件。候選組件是匹配過濾條件的類庫,并有在容器中注冊的對應的 bean 的定義。這就可 以不用 XML 來執行 bea n 的注冊了 ,那么你就 可以使用注 解(比如 @Component),AspectJ 風格的表達式,或者是你子定義的過濾條件來選擇那些類會有在容 器中注冊的 bean。 > ![](img/note.gif) > 注意 > 從 Spring 3.0 開始,很多由 [Spring JavaConfig](http://www.springsource.org/javaconfig) [項目](http://www.springsource.org/javaconfig)提供的特性作為 Spring Framework 核心 的一部分了。這就允許你使用 Java 而不是傳統的 XML 文件來定義 bean 了。看一看 @Configuration,@Bean,@Import 和@DependsOn 注解的例子來使用它們的新特性。 ### 4.10.1 @Component 和更多典型注解 在 Spring 2.0 版之后,@Repository 注解是任意滿足它的角色或典型(比如熟知的數 據訪問對象,DAO)庫的類的標記。在這個標記的使用中,就是在 14.2.2 節,“表達式翻譯” 中描述的表達式自動翻譯。 Spring 2\. 5 引 入 了 更 多 的 注 解 : @Component , @Service 和 @Controller 。 @Component 是對 Spring 任意管理 組件的通用刻 板。@Repository ,@Service 和 @Controller 是對更多的特定用例@Component 的專業化,比如,在持久層,服務層和 表現層。因此,你可以使用@Component 注解你的組件類,但是使用@Repository, @Service 或@Controller 注解來替代的話,那么你的類更合適由工具來處理或和不同的方面相關聯。比如,這些刻板注解使得理想化的目標稱為切入點。而且@Repository, @Service 和@Controller 也可以在將來 Spring Framework 的發布中攜帶更多的語義。因此,如果對于服務層,你在@Component 或@Service 中間選擇的話,那么@Service 無 疑是更好的選擇。相似地,正如上面提到的,在持久層中,@Repository 已經作為自動異 常翻譯的表示所被支持 ### 4.10.2 自動檢測類和 bean 的注冊 Spring 可 以 自 動 檢 測 刻 板 類 并 在 ApplicationContext 中 注 冊 對 應 的BeanDefinition。比如,下面的兩個類就是自動檢測的例子: ``` @Service public class SimpleMovieLister { private MovieFinder movieFinder; @Autowired public SimpleMovieLister(MovieFinder movieFinder) { this.movieFinder = movieFinder; } } @Repository public class JpaMovieFinder implements MovieFinder { //為了清晰省略了實現 } ``` 要 自 動檢 測這 些類 并注 冊對 應的 bean ,你 需要 包含 如下 的 XML 元素 ,其 中 的 base-package 元素通常是這兩個類的的父包。(也就是說,你可以指定以逗號分隔的列表來 為每個類引入父包。) ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema -instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring -beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="org.example"/> </beans> ``` > ![](img/note.gif) > 注意 > 對類路徑包的掃描需要存在類路徑下對應的目錄。當你使用 Ant 來構建 JAR 包時,要保 證你沒有激活 JAR 目標中的 files-only 開關。 此 外 , 當 你 使 用 component-scan ( 組 件 - 掃 描 , 譯 者 注 ) 時 , AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor 二者都是隱式包含的。這就意味著兩個組件 被自動檢測之后就裝配在一起了-而不需要在 XML 中提供其它任何 bean 的配置元數據。 > ![](img/note.gif) > 注意 > 你 可 以 將 annotation-config 屬 性 置 為 false 來 關 閉 AutowiredAnnotationBeanPostProcessor 的 CommonAnnotationBeanPostProcessor 注冊。 ### 4.10.3 使用過濾器來自定義掃描 默認情況下,使用@Component,@Repository,@Service,@Controller 注解 或使用了進行自定義的@Component 注解的類本身僅僅檢測候選組件。你可以修改并擴展 這 種 行 為, 僅僅 應 用自 定 義的 過 濾器 就 可以 了。 在 component-scan 元素中添加 include-filter 或 exclude-filter 子元素就可以了。每個過濾器元素需要 type 和 expression 屬性。下面的表格描述了過濾選項。 表 4.5 過濾器類型 | 過濾器類型 | 表達式示例 | 描述 | | annotation(注解) | org.example.SomeAnnotation | 在目標組件的類型層表示的注解 | | assignable(分配) | org.example.SomeClass | 目標組件分配去(擴展/實現)的類(接口) | | aspectj | org.example..*Service+ | AspectJ 類型表達式來匹配目標組件 | | regex(正則表達式) | org\.example\.Default.* | 正則表達式來匹配目標組件類的名稱 | | custom(自定義) | org.example.MyTypeFilter | 自 定 義 org.springframework.core.type.TypeFilter 接口的實現類 | 下面的示例代碼展示了 XML 配置忽略所有@Repository 注解并使用“sub”庫來替代。 ``` <beans> <context:component-scan base-package="org.example"> <context:include-filter type="regex" expression=".*Stub.*Repository"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository "/> </context:component-scan> </beans> ``` > ![](img/note.gif) > 注意 > 你可以使用`<component-scan/>`元素中的 use-default-filter="false"屬性來關閉默認的過濾 器。這會導致關閉使用@Component,@Repository,@Service 或@Controller 注解 的類的自動檢測。 ### 4.10.4 使用組件定義 bean 的元數據 Spring 組件可以為容器提供 bea n 定義的元數據。你可以使用用于定義 bea n 元數據的@Configuration 注解的類的@Bean 注解。這里有一個示例: ``` @Component public class FactoryMethodComponent { @Bean @Qualifier("public") public TestBean publicInstance() { return new TestBean("publicInstance"); } public void doWork() { //忽略組件方法的實現 } } ``` 這個類在 Spring 的組件中有包含在它的 doWork()方法中的特定應用代碼。它也提供 了 bean 的定義并且有工廠方法來指向 publicInstance()方法。@Bean 注解標識了工廠 方法和其它 bean 定義的屬性,比如通過@Qualifier 注解表示的限定符。其它方法級的注 解可以用于特定的是@Scope,@Lazy 和自定義限定符注解。自動裝配字段和方法也是支持 的,這在之前討論過,而且還有對自動裝配@Bean 方法的支持: ``` @Component public class FactoryMethodComponent { private static int i; @Bean @Qualifier("public") public TestBean publicInstance() { return new TestBean("publicInstance"); } // 使用自定義標識符并自動裝配方法參數 @Bean protected TestBean protectedInstance(@Qualifier( "public") TestBean spouse,@Value("#{privateInstance.age}") String country) { TestBean tb = new TestBean("protectedInstance", 1); tb.setSpouse(tb); tb.setCountry(country); return tb; } @Bean @Scope(BeanDefinition.SCOPE_SINGLETON) private TestBean privateInstance() { return new TestBean("privateInstance", i++); } @Bean @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS) public TestBean requestScopedInstance() { return new TestBean("requestScopedInstance", 3); } } ``` 這個示例為另外一個名為 privateInstance 的 bea n 的 Age 屬性自動裝配了 String 方法參數 country。Spring 的表達式語言元素通過`#{<expression>}`表示定義了屬性的 值。對于@Value 注解,當解析表達式文本時,表達式解析器會預先配置來查看 bean 的名 稱。 Spring 組件中的@Bean 方法會被不同方式來處理,而不會像 Spring 中@Configuration 的類那樣。不同的是@Component 類沒有使用 CGLIB 來加強并攔截字段和方法的調用。CGLIB 代理是調用@Configuration 類和創建 bean 元數據引用協作對象的@Bean 方法調用的手 段。方法沒有使用通常的 Java 語義來調用。相比之下,使用@Component 類@Bean 方法來 調用方法或字段有標準的 Java 語義。 ### 4.10.5 命名自動檢測組件 當 組 件 被 自 動 檢 測 作 為 掃 描 進 程 的 一 部 分 時 , 它 的 bean 名 稱 是 由 BeanNameGenerator 策略來生成并告知掃描器的。默認情況下, Spring 的刻板注解 (@Component,@Repository,@Service 和@Controller)包含 name 值從而提供對應 bean 定義的名稱。 注意 JSR 330 的@Named 注解可以被用于檢測組件和為它們提供名稱的手段。如果在類路徑 中有 JSR 330 的 JAR 包的話,這種行為會被自動開啟。 如果注解包含 name 值或者對于其它任意被檢測的組件(比如那些被自定義過濾器發現 的),默認的 bean 的名稱生成器返回未大寫的非限定符類名。比如,如果下面的兩個組件被檢測到了,那么名稱可能是 myMovieLister 和 movieFinderImpl: ``` @Service("myMovieLister") public class SimpleMovieLister { // ... } @Repository public class MovieFinderImpl implements MovieFinder { // ... } ``` > ![](img/note.gif) > 注意 > 如果你不想使用默認的 bean 命名策略,你可以提供自定義的 bean 命名策略。首先, 實現 [BeanNameGenerator](http://static.springframework.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/support/BeanNameGenerator.html) 接口,要保證包含默認的沒有參數的構造方法。之后,在配置 掃描器時,要提供類的完全限定名。 ``` <beans> <context:component-scan base-package="org.example" name-generator="org.example.MyNameGenerator" /> </beans> ``` 作為通用的規則,要考慮使用注解指定名稱時,其它組件可能會有對它的明確的引用。 另一方面,當容器負責裝配時,自動生成名稱是可行的。 ### 4.10.6 為自動檢測組件提供范圍 一般情況下,Spring 管理的組件,自動檢測組件的默認和最多使用的范圍是單例范圍。 然而,有事你需要其它范圍,Spring 2.5 提供了一個新的@Scope 注解。僅僅使用注解提供 范圍的名稱: ``` @Scope("prototype") @Repository public class MovieFinderImpl implements MovieFinder { // ... } ``` > ![](img/note.gif) > 注意 > 為范圍決策提供自定義策略而不是基于注解的方式,實現 [ScopeMetadataResolver](http://static.springframework.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/annotation/ScopeMetadataResolver.html) 接口,并保證包含了默認的無參構造方法。之后,當配置掃描器時,要提供類的完全限定名: ``` <beans> <context:component-scan base-package="org.example" scope-resolver="org.example.MyScopeResolver" /> </beans> ``` 當使用特定反而非單例范圍時,它可能必須要為有范圍的對象生成代理。這個原因在 4.5.4.5 節,“各種范圍的 bean 作為依賴”中描述過了。出于這樣的目的,在 component-scan 元素中可以使用 scoped-proxy 屬性。三種可能的值是:no(無),interface(接口)和 targetClass(目標類)。比如,下面的配置就會啟動標準的 JDK 動態代理: ``` <beans> <context:component-scan base-package="org.example" scoped-proxy="interfaces" /> </beans> ``` ### 4.10.7 使用注解提供限定符元數據 @Qualifier 注解在 4.9.3 節,“使用限定符來微調基于注解的自動裝配”中討論過了。 那部分中的示例說明了@Qualifier 注解的使用和當你需要處理自動裝配候選者時,自定 義限定符注解來提供微調控制。因為那些示例是基于 XML 的 bean 定義的,限定符元數據在 候選者 bean 定義中提供,并使用了 XML 中的 bean 元素的 qualifier 和 meta 子元素。 當對自動檢測組件使用基于類路徑掃描時,你可以在候選者類中使用類型-級的注解提供限 定符元數據。下面的三個示例就展示了這個技術: ``` @Component @Qualifier("Action") public class ActionMovieCatalog implements MovieCatalog { // ... } @Component @Genre("Action") public class ActionMovieCatalog implements MovieCatalog { // ... } @Component @Offline public class CachingMovieCatalog implements MovieCatalog { // ... } ``` > ![](img/note.gif) > 注意 > 對于大多數基于注解的方式,要記得注解元數據會綁定到類定義本身中去,而使用 XML 就允許對多個相同類型的 bean 在它們的限定符元數據中提供變化,因為那些元數據是對于 每個實例而不是每個類提供的。
                  <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>

                              哎呀哎呀视频在线观看