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 重写方法 ...
随机推荐
- 十大要避免的Ext JS开发方法
原文地址:http://www.sencha.com/blog/top-10-ext-js-development-practices-to-avoid/ 作者:Sean LanktreeSean i ...
- android开发(33) 让 actionbar 透明2
让 actionbar 的背景 透明 我需要一个 透明的actionbar ,找到如下方法实现: 1. 首先,设置ActionBar 浮动到主界面上来. 2. 然后,设置ActionBar的背景色,透 ...
- CSS(三):引入样式和优先级
CSS的引入样式总共有三种:行内样式(内联样式表).内部样式表.外部样式表.下面分别来介绍这三种样式. 一.行内样式 行内样式也叫内联样式,使用style属性引入CSS样式.看下面的示例: <! ...
- tabs 标签样式
http://www.jq22.com/jquery-info17973 http://www.jq22.com/demo/elementTab201801262311/ 插件描述:基于element ...
- 解决IDEA 中git 无法自动push 提交问题 Push failed: Failed with error: Could not read from remote repository.
Push failed: Failed with error: Could not read from remote repository.
- PCL点云配准(2)
(1)正态分布变换进行配准(normal Distributions Transform) 介绍关于如何使用正态分布算法来确定两个大型点云之间的刚体变换,正态分布变换算法是一个配准算法,它应用于三维点 ...
- R语言常用基础知识(入门)
data.frame 动态确定列名称 var <- "mpg" #Doesn't work mtcars$var #These both work, but note tha ...
- ITSM运营实施案例 - 汽车行业
M公司是在汽车行业知名TOP企业,在日常运营过程中有如下问题: ******************************************************************** ...
- 情商UP:不遵守八小时工作制,你就能富起来??
从工作第一天起,我们就默认了八小时工作制,但从没有想过,这是不是最高效的工作时间. 事实上,八小时工作制并没有科学依据,不过是多年来约定俗成的习惯罢了. 那些坚守八小时工作制的人和敢于打破它的人,都获 ...
- Java如何中断正在运行的线程?
在Java编程中,如何中断正在运行的线程? 以下示例演示如何使用Thread类的interrupt()方法中断正在运行线程,并检查线程是否使用isInterrupted()方法中断. package ...