spring-Resource-01
2018-11-18 19:40 crow! 阅读(187) 评论(0) 编辑 收藏 举报1、读取不同资源
字节码
public class ByteArrayResourceDemo { public static void main(String[] args) throws IOException { // 设置了内存的输入资源 Resource resource = new ByteArrayResource("Hello World".getBytes()) ; if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter("\n") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } } }
从文件读取
public static void main(String[] args) throws IOException { File file = new File("D:" + File.separator + "msg.txt") ; // 设置了内存的输入资源 Resource resource = new FileSystemResource(file) ; if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter("\n") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } }
classpath读取
public static void main(String[] args) throws IOException { // 设置了内存的输入资源 Resource resource = new ClassPathResource("applicationContext.xml") ; if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter("\n") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } }
DefaultResourceLoader
http 通过网络读取
file通过文件读取
classpath通过classpath读取
public static void main(String[] args) throws IOException { ResourceLoader loader = new DefaultResourceLoader() ; Resource resource = loader.getResource("file:d:\\msg.txt"); if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter("\n") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } }
public static void main(String[] args) throws IOException { ResourceLoader loader = new DefaultResourceLoader() ; Resource resource = loader.getResource("classpath:applicationContext.xml"); if (resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource.getInputStream()); scan.useDelimiter("\n") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } }
Resource自动装配
->MyResource
public class MyResource { private Resource resource ; public Resource getResource() { return resource; } public void setResource(Resource resource) { this.resource = resource; } public void print() throws IOException { if (this.resource.exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(this.resource.getInputStream()); scan.useDelimiter("\n") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } } }
->applicationContext.xml
<bean id="myres" class="cn.mldn.util.MyResource">
<property name="resource" value="file:d:\\msg.txt" />
</bean>
->test
public static void main(String[] args) throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); MyResource mr = ctx.getBean("myres",MyResource.class); mr.print(); }
注入多个Resource
public static void main(String[] args) throws Exception { ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver() ; // ? : 匹配一个 // * : 匹配多个 // ** : 匹配多个路径 Resource[] resource = rpr.getResources("classpath:cn/mldn/**/applicationContext.xml"); for(int x = 0 ; x < resource.length ; x++) { if (resource[x].exists()) { //现在资源存在 //如果要想进行资源的读取,肯定要通过Resource父接口InputStreamResource完成 Scanner scan = new Scanner(resource[x].getInputStream()); scan.useDelimiter("\n") ; while(scan.hasNext()) { System.out.println(scan.next()); } scan.close(); } } }
自动注入多个Resource
<bean id="rarr" class="cn.mldn.util.MyResourceArray">
<property name="resource">
<array>
<value>classpath*:overview.html</value>
<value>classpath*:notice.txt</value>
</array>
</property>
</bean>
public class MyResourceArray { private Resource [] resource ; public Resource[] getResource() { return resource; } public void setResource(Resource[] resource) { this.resource = resource; }
}