- 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"/>
- Python概率統(tǒng)計
- Qt 5 and OpenCV 4 Computer Vision Projects
- C#程序設(shè)計實(shí)訓(xùn)指導(dǎo)書
- 大學(xué)計算機(jī)應(yīng)用基礎(chǔ)實(shí)踐教程
- 數(shù)據(jù)結(jié)構(gòu)與算法JavaScript描述
- MATLAB實(shí)用教程
- 琢石成器:Windows環(huán)境下32位匯編語言程序設(shè)計
- Mastering Apache Maven 3
- Python大學(xué)實(shí)用教程
- Flink技術(shù)內(nèi)幕:架構(gòu)設(shè)計與實(shí)現(xiàn)原理
- C語言程序設(shè)計教程
- 樹莓派開發(fā)從零開始學(xué):超好玩的智能小硬件制作書
- jQuery Essentials
- Scratch少兒編程高手的7個好習(xí)慣
- Clojure High Performance Programming