應用上下文構造器方法的資源路徑可以是簡單的路徑(如上所示),其具有到目標資源的一對一映射,或者可以包含特殊的`classpath *`:前綴和/或內部Ant- 樣式正則表達式(使用Spring的PathMatcher實用程序進行匹配)。 后兩者都是有效的通配符.
此機制的一個用途是在執行組件式應用程序組裝時。 所有組件都可以將上下文定義片段“發布”到眾所周知的位置路徑,并且當最終應用程序上下文使用通過`classpath *`:前綴的相同路徑創建時,所有組件片段將自動被選取。
請注意,此通配符特定于在應用程序上下文構造方法的資源路徑(或直接使用`PathMatcher`工具類),并在構建時解析。 它與`Resource `類型本身無關。 無法使用統配`classpath *`:前綴來構造實際的`Resource`,因為`Resource`一次只能指向一個資源。
## Ant-style Patterns
當路徑位置包含Ant樣式時,例如:
~~~
/WEB-INF/*-context.xml
com/mycompany/**/applicationContext.xml
file:C:/some/path/*-context.xml
classpath:com/mycompany/**/applicationContext.xml
~~~
處理統配路徑過程非常復雜但遵循一定規則,首先把通配符之前的路徑生產一個資源,從中獲取URL.如果URL不是`jar:`,或容器的特點變量,(如weblogic的`zip`或websphare的`wsjar`),則從中獲取`java.io.File`,并通過遍歷文件系統來解析通配符。在`jar` URL的情況下,解析器要么從中獲取`java.net.JarURLConnection`,要么手動解析`jar` URL,然后遍歷jar文件的內容來解析通配符。
## Implications on portability
如果指定的路徑已經是文件URL(無論是顯式還是隱式,因為基本`ResourceLoader`是文件系統的一種),那么通配符將保證以完全便攜的方式工作。
如果指定的路徑是類路徑位置,則解析器必須通過`Classloader.getResource()`調用獲取最后一個非通配符路徑段URL。 因為這只是路徑的一個節點(不是最后的文件),所以它實際上是未定義的(在`ClassLoader` javadoc中),在這種情況下,究竟返回了什么樣的URL。 實際上,它總是一個表示目錄的`java.io.File`,類路徑資源解析為文件系統位置,或者某種類型的jar URL,其中classpath資源解析為jar位置。 但是,這個操作仍然存在可移植性問題。
如果為最后一個非通配符段獲取了一個jar URL,解析器必須能夠從中獲取一個`java.net.JarURLConnection`,或者手動解析該jar URL,以便能夠遍歷該jar的內容,并解析 通配符。 這可以在大多數環境中使用,但在其他環境中會失敗,強烈建議您在依賴特定環境之前徹底測試來自jars的資源的通配符解決方案。
## he classpath*: prefix
在構建基于XML的應用程序上下文時,位置字符串可能會使用特殊的類路徑*:前綴:
~~~
ApplicationContext ctx =
new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");
~~~
此特殊前綴指定必須獲取與給定名稱匹配的所有類路徑資源(內部實質上這是通過`ClassLoader.getResources(...)`調用發生的),然后進行合并以形成最終的應用程序上下文定義。
通配符類路徑依賴于底層類加載器的`getResources()`方法。 由于現在大多數應用程序服務器都提供它們自己的類加載器實現,所以在處理jar文件時行為可能會有所不同。 檢查`classpath *`是否工作的簡單測試是使用classloader從classpath中的jar中加載文件:`getClass().getClassLoader().getResources(“<someFileInsideTheJar>”)`。 使用具有相同名稱但位于兩個不同位置的文件進行此測試。 如果返回的結果不正確,請檢查應用程序服務器文檔以了解可能影響類加載器行為的設置。
`classpath *`:前綴也可以與其他位置路徑中的`PathMatcher`模式組合使用,例如`classpath *:META-INF / * - beans.xml`。 在這種情況下,解決策略非常簡單:在最后一個非通配符路徑段上使用`ClassLoader.getResources()`調用來獲取類加載器層次結構中的所有匹配資源,然后再如上所述遍歷資源文件.
## Other notes relating to wildcards
請注意,除非實際目標文件駐留在文件系統中,否則`classpath *`:與Ant樣式模式結合使用時,必須在模式啟動之前至少有一個根目錄可靠地運行。 這意味著`classpath*:*.xml`這樣的模式可能不會從jar文件的根文件中檢索文件,而只能從擴展目錄的根文件中檢索文件。
Spring檢索類路徑條目的能力來源于JDK的`ClassLoader.getResources()`方法,該方法傳入空字符串時返回文件系統位置(指示要搜索的潛在根)。 Spring會評估`URLClassLoader`運行時配置和jar文件中的“`java.class.path”`清單,但這并不保證會導致可移植行為。
使用`classpath:`的Ant樣式模式:如果類路徑中多個根包,則無法保證找到匹配的資源。 這是因為資源如下
~~~
com/mycompany/package1/service-context.xml
~~~
可能只在一個位置,但是當一條路徑如
~~~
classpath:com/mycompany/**/service-context.xml
~~~
嘗試處理,首先`getResource("com/mycompany")`如果此包節點存在于多個類加載程序位置中,但選中的一個可能實際的最終資源可能不在其下。因此使用`classpath*:`搜選所有的根包,才能保證找到資源.
- 正確打開本書的姿勢
- 第一部分 Core
- 1. Ioc container
- 1.1. Introduction to the Spring IoC container and beans
- 1.2. Container overview
- 1.2.1. Configuration metadata
- 1.2.2. Instantiating a container
- 1.2.3. Using the container
- 1.3. Bean overview
- 1.3.1. Naming beans
- 1.3.2. Instantiating beans
- 1.4. Dependencies
- 1.4.1. Dependency Injection
- 1.4.2. Dependencies and configuration in detail
- 1.4.3. Using depends-on
- 1.4.4. Lazy-initialized beans
- 1.4.5. Autowiring collaborators
- 1.4.6. Method injection
- 1.5 Bean Scopes
- 1.6. Customizing the nature of a bean TODO
- 1.7. Bean definition inheritance TODO
- 1.8. Container Extension Points TODO
- 1.9. Annotation-based container configuration
- 1.9.1. @Required
- 1.9.2. @Autowired
- 1.9.3. Fine-tuning annotation-based autowiring with @Primary
- 1.9.4. Fine-tuning annotation-based autowiring with qualifiers TODO
- 1.9.5. Using generics as autowiring qualifiers TODO
- 1.9.6. CustomAutowireConfigurer TODO
- 1.10. Classpath scanning and managed components
- 1.10.1. @Component and further stereotype annotations
- 1.11. Using JSR 330 Standard Annotations TODO
- 1.12. Java-based container configuration
- 1.12.1. Basic concepts: @Bean and @Configuration
- 1.12.2. Instantiating the Spring container using AnnotationConfigApplicationContext
- 2. Resources
- 2.1. Introduction
- 2.2. The Resource interface
- 2.3. Built-in Resource implementations
- 2.3.1. UrlResource
- 2.3.2. ClassPathResource
- 2.3.3. FileSystemResource
- 2.3.4. ServletContextResource
- 2.3.5. InputStreamResource
- 2.3.6. ByteArrayResource
- 2.4. The ResourceLoader
- 2.5. The ResourceLoaderAware interface
- 2.6. Resources as dependencies
- 2.7. Application contexts and Resource paths
- 2.7.1. Constructing application contexts
- 2.7.2. Wildcards in application context constructor resource paths
- 2.7.3. FileSystemResource caveats
- 3. Validation, Data Binding, and Type Conversion
- 4. Spring Expression Language (SpEL)
- 5. Aspect Oriented Programming with Spring
- 5.1. Introduction
- 5.1.1. AOP concepts
- 5.1.2. Spring AOP capabilities and goals
- 5.1.3. AOP Proxies
- 5.2. @AspectJ support
- 5.2.1. Enabling @AspectJ Support
- 5.2.2. Declaring an aspect
- 5.2.3. Declaring a pointcut
- 5.2.4. Declaring advice
- 5.2.5. Introductions TODO
- 5.2.6. Aspect instantiation models TODO
- 5.2.7. Example
- 5.3. Schema-based AOP support TODO
- 5.4. Choosing which AOP declaration style to use TODO
- 5.5. Mixing aspect types TODO
- 5.6. Proxying mechanisms
- 5.6.1. Understanding AOP proxies
- 5.7. Programmatic creation of @AspectJ Proxies
- 5.8. Using AspectJ with Spring applications
- 5.8.1. Using AspectJ to dependency inject domain objects with Spring
- 5.8.2. Other Spring aspects for AspectJ
- 第二部分 Testing
- 第三部分 Data Access
- 1. Transaction Management
- 1.1. Introduction to Spring Framework transaction management
- 1.2 Advantages of the Spring Framework’s transaction support model
- 1.2.1. Global transactions
- 1.2.2. Local transactions
- 1.2.3. Spring Framework’s consistent programming model
- 1.3. Understanding the Spring Framework transaction abstraction
- 1.4. Synchronizing resources with transactions
- 1.4.1. High-level synchronization approach
- 1.4.2. Low-level synchronization approach
- 1.4.3. TransactionAwareDataSourceProxy
- 1.5. Declarative transaction management
- 1.5.1. Understanding the Spring Framework’s declarative transaction implementation
- 1.5.2. Example of declarative transaction implementation
- 1.5.3. Rolling back a declarative transaction
- 1.5.4. Configuring different transactional semantics for different beans
- 1.5.5. tx:advice元素的 settings
- 1.5.6. Using @Transactional
- 1.5.7. Transaction propagation
- 1.5.8. Advising transactional operations
- 1.5.9. Using @Transactional with AspectJ TODO
- 第四部分 web servlet
- 第五部分 Web Reactive
- 第六部分 Integration
- 第七部分 Languages