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 重写方法 ...
随机推荐
- 【javascript】设为首页——setHome
原生 js 编写,兼容 ie,火狐和谷歌. 函数如下: function setHome(obj,url){ try{ obj.style.behavior = 'url(#default#homep ...
- Publishing to IIS 发布到IIS
原文:https://docs.asp.net/en/latest/publishing/iis.html 发布到IIS Publishing to IIS By Rick Anderson and ...
- substance新版及问题
新版地址:https://github.com/Insubstantial,目前是7.3版 http://stackoverflow.com/questions/3657538/substance-u ...
- sps和pps的简单理解记录
最近博客写出了日记的既视感... 使用RTP传输H264的时候,需要用到sdp协议描述,其中有两项:Sequence Parameter Sets (SPS) 和Picture Parameter S ...
- ggplot ggplot2 画图
折线图-ggplot2 http://blog.163.com/yugao1986@126/blog/static/6922850820131161531421/http://blog.sina.c ...
- Loadrunner中Error-26612HTTP Status-Cod
最近在测试一系统的时候,录制脚本没有错误,回放的时候总是出现如下错误: Action.c(6): Error -26612: HTTP Status-Code=500 (Internal Server ...
- [kafka] 001_kafka起步
一.简介 Kafka is a distributed, partitioned, replicated commit log service. It provides the functionali ...
- VMware Fusion 5 正式版序列号
HV4KJ-2X10K-VZ768-DRAGP-8CU2F MY63N-D0HE2-0ZXC1-HV954-937JL
- mysql小题趣事
题一 答案: case when +条件 +then 显示什么 +else+显示另外什么+end
- Apache Commons CLI
简单的说,就是对命令的参数进行定义和解析的工具 -- 这里说的参数是我们常用的说法,而CLI里则是Option.Options,参数值(如果有)则是Option的arg(s). ## 为什么 那么,为 ...