8 -- 深入使用Spring -- 3...3 使用Resouce作为属性
8.3.3 使用Resouce作为属性
当应用程序中的Bean实例需要访问资源时,Spring可以直接利用依赖注入。
如果Bean实例需要访问资源,有如下两种解决方案:
⊙ 在代码中获取Resource实例。
⊙ 使用依赖注入。
在代码中获取Resource实例:当程序获取Resource实例时,总需要提供Resource所在的位置,不管通过FileSystemResource创建实例,还是通过ClassPathResource创建实例,或者通过ApplicationContext的getResource()方法获取实例,都需要提供资源位置。这意味着:资源所在的物理位置将被耦合到代码中,如果资源位置放生改变,则必须改写程序。
使用依赖注入:让Spring为Bean实例依赖注入资源。
Class : TestBean
package edu.pri.lime._8_3_3.bean.impl;
import org.springframework.core.io.Resource;
public class TestBean {
private Resource res;
public void setRes(Resource res) {
this.res = res;
}
public void parse(){
System.out.println(res.getFilename());
System.out.println(res.getDescription());
}
public Resource getRes() {
return res;
}
}
XML :
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:P="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id="testBean" class="edu.pri.lime._8_3_3.bean.impl.TestBean" >
<!-- 可以使用file:、http:、ftp:等前缀强制Spring采用对应的资源访问策略 -->
<!-- 如果不采用任何前缀,则Spring将采用与该ApplicationContext相同的资源访问策略来访问资源 -->
<property name="res" value="classpath:book.xml"/>
</bean> </beans>
Class : SpringTest
package edu.pri.lime._8_3_3.bean.main; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import edu.pri.lime._8_3_3.bean.impl.TestBean; public class SpringTest { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("app_8_3_3.xml");
TestBean testBean = ctx.getBean("testBean",TestBean.class);
testBean.parse();
}
}
采用依赖注入,允许动态配置资源文件位置,无须将资源文件位置写在代码中,当资源文件位置发生变化时,无须改写程序,直接修改配置未见即可。
啦啦啦
啦啦啦
8 -- 深入使用Spring -- 3...3 使用Resouce作为属性的更多相关文章
- Spring Boot 环境变量读取 和 属性对象的绑定
网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...
- Spring(3.2.3) - Beans(12): 属性占位符
使用属性占位符可以将 Spring 配置文件中的部分元数据放在属性文件中设置,这样可以将相似的配置(如 JDBC 的参数配置)放在特定的属性文件中,如果只需要修改这部分配置,则无需修改 Spring ...
- Spring 声明式事务,propagation属性列表及isolation(隔离级别)
Spring 声明式事务,propagation属性列表 TransactionDefinition接口中定义,共有7种选项可用: PROPAGATION_REQUIRED:支持当前事务,如果当前没有 ...
- Spring中事务的5种属性总结
Sping的事务 和 数据库的事务是不同的概念,数据库的事务一般称为底层事务 Spring的事务是对这种事务的抽象 我称之为逻辑事务 Spring对事务的功能进行了扩展,除了基本的Isolation之 ...
- 十五、Spring Boot 环境变量读取 和 属性对象的绑定
凡是被spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. 如: @ ...
- 【转】Spring Boot干货系列:常用属性汇总
转自Spring Boot干货系列:常用属性汇总 附录A.常用应用程序属性 摘自:http://docs.spring.io/spring-boot/docs/current/reference/ht ...
- spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />
spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...
- [转]spring property标签中的 ref属性和ref 标签有什么不同
spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...
- 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069509 凡是被spring管理的类,实现接口EnvironmentAware 重写方法 ...
随机推荐
- [算法]从一道题引出variable-precision SWAR算法
苏君君出了一道题,是牛客网上面的: 输入一个int型整数,输出该数二进制表示中1的个数.其中负数用补码表示. 其实这道题并不难,大家很容易想到的解法是转成字符串的思路,即如下所示: public st ...
- sqoop 常用命令整理(一)
这些内容是从sqoop的官网整理出来的,是1.4.3版本的Document,如果有错误,希望大家指正. 1.使用sqoop导入数据 sqoop import --connect jdbc:mysql: ...
- VidLoc: A Deep Spatio-Temporal Model for 6-DoF Video-Clip Relocalization
用双向lstm对序列图片做相机定位.
- Ubuntu下Ruby的下载和编译源码安装
1.Ruby的下载 Ruby可以在Ruby 官网上下载,如果想获取更多的Ruby版本,可以到淘宝镜像网站下载. 2.Ruby的编译源码安装 解压 首先把下载下来的源码压缩包解压到自己指定的目录 编译安 ...
- GLSL着色语言学习。橙皮书第一个例子GLSL+OpenTK+F#的实现。
Opengl红皮书有选择的看了一些,最后的讲着色语言GLSL的部分看的甚为不理解,然后找到Opengl橙皮书,然后就容易理解多了. 在前面,我们或多或少接触到Opengl的处理过程,只说前面一些处理, ...
- Windows 自带的截屏功能
有时没登陆QQ,又急需截图,系统自带的截图功能就可以派上用场了. 1.按下键盘上的Print Screen 按钮 2.打开系统自带的画图工具,点击粘贴就可以了.
- e808. 建立菜单栏,菜单,菜单项
When the user selects a menu item, it fires an action event. // Create the menu bar JMenuBar menuBar ...
- memcached -- 运行memcached
Memcached 运行 Memcached命令的运行: $ /usr/local/memcached/bin/memcached -h 注意:如果使用自动安装, memcached 命令位于 /us ...
- ubuntu -- 安装最新版的nodejs
1.安装最新的nodejs和npm # apt-get update # apt-get install -y python-software-properties software-properti ...
- Python——eventlet.websocket
使用该模块可以方便地创建 websocket 服务器,要创建一个websocket服务器,只需要将一个句柄函数用装饰器 WebSocketWSGI 装饰即可,然后这个函数就可以当做一个WSGI应用: ...