- Spring Essentials
- Shameer Kunjumohamed Hamidreza Sattari
- 214字
- 2021-07-16 13:05:48
Handling resources
Spring Framework provides excellent support for accessing low-level resources, thus solving many limitations of Java's standard java.net.URL
and standard handlers. The org.springframework.core.io.Resource
package and its many concrete implementations form a solid foundation for Spring Framework's robust resource handling.
Resource abstraction is used extensively in Spring itself, inside many implementations of ApplicationContext
—it's actually very useful to use as a general utility class by itself in your own code in order to access resources. You will find the following resource implementations that come supplied right out of the box in Spring:
Generally, you do not directly instantiate any of these resources; rather, you use a ResourceLoader
interface to do that job for you. All ApplicationContext
implement a ResourceLoader
interface; therefore, any ApplicationContext
can be used to obtain resource instances. The code for this is as follows:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"application-context.xml"}); Resource classPathResource = ctx.getResource("classpath:scripts/tasks-schema.sql"); Resource fileResource = ctx.getResource("file:///scripts/master-data.sql"); Resource urlResource = ctx.getResource("http://country.io/names.json");
You can inject resources into your beans by simply passing the filename or URL of your resource as an argument, as shown here. ApplicationContext
, which is a ResourceLoader
interface, will create an instance of an appropriate resource implementation based on the URL you supply:
@Value("http://country.io/names.json") private Resource countriesResource;
Here is the XML version of injecting a resource:
<property name="countriesResource" value="http://country.io/names.json"/>
- 軟件測試項目實戰之性能測試篇
- 游戲程序設計教程
- Windows Phone 7.5:Building Location-aware Applications
- 編寫高質量代碼:改善Objective-C程序的61個建議
- Secret Recipes of the Python Ninja
- Learning Image Processing with OpenCV
- Kotlin進階實戰
- Groovy 2 Cookbook
- Java 9:Building Robust Modular Applications
- Python趣味創意編程
- Mastering JavaScript Promises
- Python實戰指南:手把手教你掌握300個精彩案例
- Scratch 3.0少兒積木式編程(6~10歲)
- Python編程零基礎入門
- Python從入門到項目實踐(超值版)