Back to Javatutorial

Spring中的资源管理

docs/Spring全家桶/Spring/Spring中的资源管理.md

1.0.09.9 KB
Original Source

Resource ӿ

Ա׼ 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() - صǰԴԴʱԴڴϢһ˵Դһȫ޶ļƣǵǰԴʵ URL

Spring Դӿڣ

ӿ
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

#õ Resource ʵ

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 ӿ

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.xmlURL ʽļϵͳ
http:http://myserver/logo.pngURL ʽ
/data/config.xmlɵײ ApplicationContext ʵ־

#ResourcePatternResolver ӿ

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 ӿ

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 ԴЩͣ

  • XML Դ
  • Properties Դ
  • YAML Դ

#ο

ο

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