Spring入门学习——加载外部资源
来源:互联网 发布:软件系统验收标准 编辑:程序博客网 时间:2024/06/06 16:45
这个横幅存储在文本文件banner.txt中(该文件就在项目根目录下),文件放在了当前应用程序的路径下。*********************
*Welcome to my shop!*
*********************
配置文件内容如下:package com.cgy.springrecipes.shop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
//这里实现作用域较小的ResourceLoaderAware而不是ApplicationContext接口
public class BannerLoader implements ResourceLoaderAware{
private ResourceLoader resourceLoader;
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public void showBanner() throws IOException {
Resource banner = resourceLoader.getResource("file:banner.txt");
InputStream in = banner.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while(true) {
String line = reader.readLine();
if(line == null) {
break;
}
System.out.println(line);
}
reader.close();
}
}
配置文件调用了bean的初始化属性init-method来执行showBanner()方法,这样子只要实例化后就能立刻在控制台打印横幅。<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="banner" class="com.cgy.springrecipes.shop.BannerLoader" init-method="showBanner"/>
</beans>
运行后可在控制台看见横幅内容package com.cgy.springrecipes.shop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("beans_two.xml");//后来亲测,下面这句代码没有也是可以出现横幅的,因为Bean已经被容器实例化了
BannerLoader banner = (BannerLoader) context.getBean("banner");
}
}
这种相对路径还可以使用绝对路径,如file:banner.txt
当然,如果资源位于classpath时,必须使用classpath前缀,如file:c:/shop/banner.txt
如果是maven项目结构,classpath就是放在src/main/resources中classpath:banner.txt
除了文件系统路径和classpath之外,还可以指定一个URL加载classpath:com/springrecipes/shop/banner.txt
如果资源路径中没有指定前缀,则要根据应用上下文从一个位置加载,比如你使用FileSystemXmlApplicationContext,那么资源将从文件系统加载。使用ClassPathXmlApplicationContext,那么资源将从classpath加载。http://example.com/banner.txt
配置文件修改如下:package com.cgy.springrecipes.shop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class BannerLoader{
private Resource banner;
public void setBanner(Resource banner) {
this.banner = banner;
}
public void showBanner() throws IOException {
InputStream in = banner.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while(true) {
String line = reader.readLine();
if(line == null) {
break;
}
System.out.println(line);
}
reader.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="banner" class="com.cgy.springrecipes.shop.BannerLoader" init-method="showBanner">
<property name="banner">
<value>classpath:banner.txt</value>
</property>
</bean>
</beans>
- Spring入门学习——加载外部资源
- spring笔记——加载外部资源
- spring加载外部资源
- Spring入门学习——外部化Bean配置
- Spring攻略笔记-5 加载外部资源
- Spring IoC之高级特性--加载外部资源
- spring (三) 加载外部资源文件/配置jdbc数据源
- Spring中加载外部资源文件的几种方式
- Unity3D加载外部资源
- 加载外部资源
- 浅谈spring——Spring资源加载(十二)
- Duilib学习笔记《07》— 资源加载
- Duilib学习笔记《07》— 资源加载
- Duilib学习笔记《07》— 资源加载
- 外部加载资源来画图
- AS3 加载外部SWF资源
- spark加载外部资源方式
- [Spring入门学习笔记][静态资源]
- 用SysML做的编程技巧总结图
- Spring入门学习——自定义Bean初始化和析构
- 社科方面,许多人觉得直觉比数据研…
- Spring入门学习——使用Bean感知容器
- 从意思准度和重叠性角度看待中文、…
- Spring入门学习——加载外部资源
- c# --sql-模糊查询
- 3D打印Arduino单片机盒子
- Spring入门学习——创建Bean后处理器
- VR入门笔记一
- Blender开启动画录制后,再关掉的…
- Spring入门学习——外部化Bean配置
- 迭代器Iterable和Iterator
- 初次购入真·4K蓝光碟,可惜东芝的4…