- 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"/>
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- JavaScript百煉成仙
- Spring Cloud Alibaba核心技術與實戰案例
- Responsive Web Design with HTML5 and CSS3
- Getting Started with CreateJS
- TradeStation交易應用實踐:量化方法構建贏家策略(原書第2版)
- Python完全自學教程
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(入門與提高篇)
- WordPress 4.0 Site Blueprints(Second Edition)
- Natural Language Processing with Java and LingPipe Cookbook
- Modern C++ Programming Cookbook
- Instant jQuery Boilerplate for Plugins
- Android智能手機APP界面設計實戰教程
- FusionCharts Beginner’s Guide:The Official Guide for FusionCharts Suite
- Java程序設計入門(第2版)