ApplicationContext类型的容器构建于BeanFactory之上,是Spring提供的更加高级的IoC Service Provider,Application Context相较于BeanFactory,提供了一些特有的特性,如国际化(I18n)信息支持,统一资源加载策略,容器内事件发布等.
1.统一资源加载策略
Spring中的资源抽象和加载策略
org.springframework.core.io.Resource与org.springframework.core.io.ResourceLoader
1.1Spring中的Resource
Spring框架内部使用org.springframework.core.io.Resource接口作为所有资源的抽象和访问接口.
package org.springframework.core.io;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import org.springframework.core.io.InputStreamSource;
public interface Resource extends InputStreamSource {
boolean exists();
boolean isReadable();
boolean isOpen();
URL getURL() throws IOException;
URI getURI() throws IOException;
File getFile() throws IOException;
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String var1) throws IOException;
String getFilename();
String getDescription();
}
1.2ResourceLoader,"更广义的URL"
package org.springframework.core.io;
import org.springframework.core.io.Resource;
public interface ResourceLoader {
String CLASSPATH_URL_PREFIX = "classpath:";
Resource getResource(String var1);
ClassLoader getClassLoader();
}
可用的ResourceLoader
(1)DefaultResourceLoader
ResourceLoader的默认实现类,该类默认的资源查找核心方法如下:
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
Iterator ex = this.protocolResolvers.iterator();
Resource resource;
do {
if(!ex.hasNext()) {
if(location.startsWith("/")) {
return this.getResourceByPath(location);
}
if(location.startsWith("classpath:")) {
return new ClassPathResource(location.substring("classpath:".length()), this.getClassLoader());
}
try {
URL ex1 = new URL(location);
return new UrlResource(ex1);
} catch (MalformedURLException var5) {
return this.getResourceByPath(location);
}
}
ProtocolResolver protocolResolver = (ProtocolResolver)ex.next();
resource = protocolResolver.resolve(location, this);
} while(resource == null);
return resource;
}
(2)FileSystemResourceLoader
从文件系统加载资源并以FileSystemResourece类型返回.该类覆写了DefaultResourceLoader的getResourceByPath(String)方法逻辑.
ResourcePatternResolver—批量查找的ResourceLoader
ResourcePatternResolver是ResourceLoader的扩展,ResourceLoader每次只能根据资源路径返回确定的单个Resource实例,而ResourcePatternResolver则可以根据指定的资源路径匹配模式,每次返回多个Resource实例.
package org.springframework.core.io.support;
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public interface ResourcePatternResolver extends ResourceLoader {
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
Resource[] getResources(String locationPattern) throws IOException;
}
ResourcePatternResolver最常用的一个实现是:PathMatchingResourcePatternResolver.
1.3ApplicationContext与ResourceLoader
ApplicationContext继承了ResourcePatternResolver,也就间接实现了ResourceLoader接口.
2.国际化信息支持
2.1 Java SE提供的国际化支持
主要涉及2个类:java.util.Local和java.util.ResourceBundle
2.2 MessageSource与ApplicationContext
package org.springframework.context;
import java.util.Locale;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
public interface MessageSource {
String getMessage(String var1, Object[] var2, String var3, Locale var4);
String getMessage(String var1, Object[] var2, Locale var3) throws NoSuchMessageException;
String getMessage(MessageSourceResolvable var1, Locale var2) throws NoSuchMessageException;
}
ApplicationContext实现了MessageSource接口.
MessageSourceAware和MessageSource的注入