- 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"/>
- Web程序設(shè)計(jì)及應(yīng)用
- .NET 4.0面向?qū)ο缶幊搪劊夯A(chǔ)篇
- Python金融數(shù)據(jù)分析
- Linux環(huán)境編程:從應(yīng)用到內(nèi)核
- Hands-On Reinforcement Learning with Python
- Mastering Android Development with Kotlin
- Active Directory with PowerShell
- 軟件項(xiàng)目管理實(shí)用教程
- Node.js從入門到精通
- C# 7.1 and .NET Core 2.0:Modern Cross-Platform Development(Third Edition)
- Python面試通關(guān)寶典
- DevOps 精要:業(yè)務(wù)視角
- 軟件測(cè)試技術(shù)
- Eclipse開(kāi)發(fā)(學(xué)習(xí)筆記)
- 面向?qū)ο蠓治雠c設(shè)計(jì)(第3版)