docs/Spring全家桶/Spring/Spring中的资源管理.md
Ա URL ʻƣSpring org.springframework.core.io.Resource ӿڳ˶ԵײԴķʽӿڣṩһõķʷʽ
public interface Resource extends InputStreamSource {
boolean exists();
boolean isReadable();
boolean isOpen();
boolean isFile();
URL getURL() throws IOException;
URI getURI() throws IOException;
File getFile() throws IOException;
ReadableByteChannel readableChannel() throws IOException;
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String relativePath) throws IOException;
String getFilename();
String getDescription();
}
Resource ӿڵĶʾչ InputStreamSource ӿڡResource ĵķ£
getInputStream() - λҴǰԴصǰԴ InputStreamÿεö᷵һµ InputStreamҪرexists() - жϵǰԴǷĴڡisOpen() - жϵǰԴǷһѴ InputStreamΪ true InputStream ܱζȡֻȡһȻرԱԴй©гԴʵַ falseInputStreamResource ⡣getDescription() - صǰԴԴʱԴڴϢһ˵ԴһȫļƣǵǰԴʵ URLSpring Դӿڣ
| ӿ | |
|---|---|
org.springframework.core.io.InputStreamSource | |
| ֻԴ | org.springframework.core.io.Resource |
| дԴ | org.springframework.core.io.WritableResource |
| Դ | org.springframework.core.io.support.EncodedResource |
| Դ | org.springframework.core.io.ContextResource |
Spring õ Resource ʵ֣
| ԴԴ | ǰ | ˵ |
|---|---|---|
UrlResource(opens new window) | file:``https:``ftp: | UrlResource װһ java.net.URL ڷʿͨ URL ʵκζļHTTPS ĿꡢFTP Ŀȡ URL ַͨʽʾ˿ʹʵıǰָʾһ URL һ URL ͵ fileڷļϵͳ·httpsͨ HTTPS ЭԴftpͨ FTP Դȵȡ |
ClassPathResource(opens new window) | classpath: | ClassPathResource ·ϼԴʹ߳ļָ class еһԴ |
FileSystemResource(opens new window) | file: | FileSystemResource ** java.io.File Դʵ**֧ java.nio.file.Path Ӧ Spring ıַ·תFileSystemResource ֽ֧Ϊļ URL |
PathResource(opens new window) | PathResource java.nio.file.Path Դʵ֡ | |
ServletContextResource(opens new window) | ServletContextResource ** ServletContext Դʵ**ʾӦ Web ӦóĿ¼е· | |
InputStreamResource(opens new window) | InputStreamResource ָ InputStream Դʵע⣺ InputStream ѱԶζȡ | |
ByteArrayResource(opens new window) | ByteArrayResource ָĶԴʵ֡Ϊֽ鴴һ ByteArrayInputStream |
ResourceLoader ӿڼ Resource 䶨£
public interface ResourceLoader {
Resource getResource(String location);
ClassLoader getClassLoader();
}
Spring Ҫ ResourceLoader ʵ֣
Spring Уе ApplicationContext ʵ ResourceLoader ӿڡˣ ApplicationContext ͨ getResource() ȡ Resource ʵ
ʾ
// ûָԴǰSpring ᳢ԷغʵԴ
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");
// ָ classpath: ǰSpring ǿʹ ClassPathResource
Resource template = ctx.getResource("classpath:some/resource/path/myTemplate.txt");
// ָ file:http URL ǰSpring ǿʹ UrlResource
Resource template = ctx.getResource("file:///some/resource/path/myTemplate.txt");
Resource template = ctx.getResource("http://myhost.com/resource/path/myTemplate.txt");
±о Spring ݸλ·ԴIJԣ
| ǰ | ˵ | |
|---|---|---|
classpath: | classpath:com/myapp/config.xml | · |
file: | file:///data/config.xml | URL ʽļϵͳ |
http: | http://myserver/logo.png | URL ʽ |
/data/config.xml | ɵײ ApplicationContext ʵ־ |
ResourcePatternResolver ӿ ResourceLoader ӿڵչǶԣλģʽ Resource
public interface ResourcePatternResolver extends ResourceLoader {
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
Resource[] getResources(String locationPattern) throws IOException;
}
PathMatchingResourcePatternResolver һʵ֣ ApplicationContext ֮ʹãҲԱ ResourceArrayPropertyEditor Resource[] bean ԡPathMatchingResourcePatternResolver ָܹԴλ·Ϊһƥ Resource
ע⣺κα
ApplicationContextеĬResourceLoaderʵPathMatchingResourcePatternResolverһʵʵResourcePatternResolverӿڡ
ResourceLoaderAware ӿһĻصӿڣṩ ResourceLoader õĶResourceLoaderAware ӿڶ£
public interface ResourceLoaderAware {
void setResourceLoader(ResourceLoader resourceLoader);
}
һʵ ResourceLoaderAware ӦóУΪ Spring beanʱᱻӦóʶΪ ResourceLoaderAwareȻӦóĻ setResourceLoader(ResourceLoader)ΪṩסSpring еӦóĶʵ ResourceLoader ӿڣ
ApplicationContext һ ResourceLoader bean ʵ ApplicationContextAware ӿڲֱʹṩӦóԴ ǣһ˵ֻҪЩʹרŵ ResourceLoader ӿڡ ô뽫ϵԴؽӿڣԱΪʵóӿڣϵ Spring ApplicationContext ӿڡ
ӦóУʹ ResourceLoader ԶװΪʵ ResourceLoaderAware ӿڵͳĹ캯 byType ԶװģʽֱܹΪ캯 setter ṩ ResourceLoader Ϊ˻øԣԶװֶκͶ뿼ʹûעԶװ书ܡ £ResourceLoader ԶӵҪ ResourceLoader ͵ֶΡ캯УֻҪֶΡ캯 @Autowired ע⼴ɡ
bean Ҫͨijֶ̬ȷṩԴ·ô bean ʹ ResourceLoader ResourcePatternResolver ӿԴ 磬Ǽijģ壬ضԴȡûĽɫ ԴǾ̬ģȫ ResourceLoader ӿڣ ResourcePatternResolver ӿڣʹã bean Ҫ Resource ԣעġ
ʹעЩԱüԭӦóĶעᲢʹһ JavaBeans PropertyEditorԽ String ·תΪ Resource 磬 MyBean һ Resource ͵ģԡ
ʾ
<bean id="myBean" class="example.MyBean">
<property name="template" value="some/resource/path/myTemplate.txt"/>
</bean>
ע⣬õģԴ·ûǰΪӦóı ResourceLoaderԴҪͨ ClassPathResource``FileSystemResource ServletContextResource أȡĵȷ͡
ҪǿʹضԴͣʹǰ ʾʾǿʹ ClassPathResource UrlResourceڷļϵͳļ
<property name="template" value="classpath:some/resource/path/myTemplate.txt">
<property name="template" value="file:///some/resource/path/myTemplate.txt"/>
ͨ @Value עԴļ myTemplate.txtʾ£
@Component
public class MyBean {
private final Resource template;
public MyBean(@Value("${template.path}") Resource template) {
this.template = template;
}
// ...
}
Spring PropertyEditor Դļ·ַ Resource ע뵽 MyBean Ĺ췽
ҪضԴļʹ classpath*: ǰ磺classpath*:/config/templates/*.txt
@Component
public class MyBean {
private final Resource[] templates;
public MyBean(@Value("${templates.path}") Resource[] templates) {
this.templates = templates;
}
// ...
}
ӦĹ캯ضӦַַͣͨΪԴλ·繹Ķ XML ļ
ʾ
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:conf/appContext.xml");
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {"services.xml", "daos.xml"}, MessengerService.class);
ApplicationContext еԴ·ǵһ·һһӳ䵽ĿԴҲͨʽɰ classpath*Ҳǰ ant ʽʹ spring PathMatcher ƥ䣩
ʾ
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");
ʹ classpath* ʾ·ƥļƵԴᱻȡ(Ͼǵ ClassLoader.getResources() ŽȡԴװյӦġ
λ·ಿ֣classpath*: ǰ PathMatcher ʹã磺classpath*:META-INF/*-beans.xml
Spring ԴЩͣ
https://www.w3cschool.cn/wkspring https://www.runoob.com/w3cnote/basic-knowledge-summary-of-spring.html http://codepub.cn/2015/06/21/Basic-knowledge-summary-of-Spring https://dunwu.github.io/spring-tutorial https://mszlu.com/java/spring http://c.biancheng.net/spring/aop-module.html