Spring Resources接口

来源:互联网 发布:软件测试策略 编辑:程序博客网 时间:2024/05/16 11:30

要获取Resource对象需要继承ResourceLoader接口

ApplicationContext继承了ResourceLoader接口,可以直接使用

esource template = ctx.getResource("some/resource/path/myTemplate.txt");

Table 6.1. Resource strings   的类型

PrefixExampleExplanation

classpath:

classpath:com/myapp/config.xml

Loaded from the classpath.

file:

file:///data/config.xml

Loaded as a URL, from the filesystem. [1]

http:

http://myserver/logo.png

Loaded as a URL.

(none)

/data/config.xml

Depends on the underlying ApplicationContext.

[1]But see also Section 6.7.3, “FileSystemResource caveats”.


Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
Resource template = ctx.getResource("file:///some/resource/path/myTemplate.txt");
Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");


获取ApplicationContext   对象可以继承 ApplicationContextAware      接口



Aware接口

public interface ApplicationContextAware {    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;}
实现Aware接口即可在对象中使用相对应的资源,例如applicationContext.getBean("...");

Table 5.4. Aware interfaces

NameInjected DependencyExplained in…

ApplicationContextAware

Declaring ApplicationContext

Section 5.6.2, “ApplicationContextAware and BeanNameAware”

ApplicationEventPublisherAware

Event publisher of the enclosing ApplicationContext

Section 5.15, “Additional Capabilities of the ApplicationContext”

BeanClassLoaderAware

Class loader used to load the bean classes.

Section 5.3.2, “Instantiating beans”

BeanFactoryAware

Declaring BeanFactory

Section 5.6.2, “ApplicationContextAware and BeanNameAware”

BeanNameAware

Name of the declaring bean

Section 5.6.2, “ApplicationContextAware and BeanNameAware”

BootstrapContextAware

Resource adapter BootstrapContext the container runs in. Typically available only in JCA awareApplicationContexts

Chapter 26,JCA CCI

LoadTimeWeaverAware

Defined weaver for processing class definition at load time

Section 9.8.4, “Load-time weaving with AspectJ in the Spring Framework”

MessageSourceAware

Configured strategy for resolving messages (with support for parametrization and internationalization)

Section 5.15, “Additional Capabilities of the ApplicationContext”

NotificationPublisherAware

Spring JMX notification publisher

Section 25.7, “Notifications”

PortletConfigAware

Current PortletConfig the container runs in. Valid only in a web-aware SpringApplicationContext

Chapter 20,Portlet MVC Framework

PortletContextAware

Current PortletContext the container runs in. Valid only in a web-aware SpringApplicationContext

Chapter 20,Portlet MVC Framework

ResourceLoaderAware

Configured loader for low-level access to resources

Chapter 6,Resources

ServletConfigAware

Current ServletConfig the container runs in. Valid only in a web-aware SpringApplicationContext

Chapter 17,Web MVC framework

ServletContextAware

Current ServletContext the container runs in. Valid only in a web-aware SpringApplicationContext

Chapter 17,Web MVC framework



0 0