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作为属性的更多相关文章

  1. Spring Boot 环境变量读取 和 属性对象的绑定

    网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...

  2. Spring(3.2.3) - Beans(12): 属性占位符

    使用属性占位符可以将 Spring 配置文件中的部分元数据放在属性文件中设置,这样可以将相似的配置(如 JDBC 的参数配置)放在特定的属性文件中,如果只需要修改这部分配置,则无需修改 Spring ...

  3. Spring 声明式事务,propagation属性列表及isolation(隔离级别)

    Spring 声明式事务,propagation属性列表 TransactionDefinition接口中定义,共有7种选项可用: PROPAGATION_REQUIRED:支持当前事务,如果当前没有 ...

  4. Spring中事务的5种属性总结

    Sping的事务 和 数据库的事务是不同的概念,数据库的事务一般称为底层事务 Spring的事务是对这种事务的抽象 我称之为逻辑事务 Spring对事务的功能进行了扩展,除了基本的Isolation之 ...

  5. 十五、Spring Boot 环境变量读取 和 属性对象的绑定

    凡是被spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. 如: @ ...

  6. 【转】Spring Boot干货系列:常用属性汇总

    转自Spring Boot干货系列:常用属性汇总 附录A.常用应用程序属性 摘自:http://docs.spring.io/spring-boot/docs/current/reference/ht ...

  7. spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" />

    spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...

  8. [转]spring property标签中的 ref属性和ref 标签有什么不同

    spring property标签中的 ref属性和ref 标签有什么不同? 如下:<property name="a" ref="b" /> sp ...

  9. 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069509 凡是被spring管理的类,实现接口EnvironmentAware 重写方法 ...

随机推荐

  1. hbase 学习(十三)集群间备份原理

    集群建备份,它是master/slaves结构式的备份,由master推送,这样更容易跟踪现在备份到哪里了,况且region server是都有自己的WAL 和HLog日志,它就像mysql的主从备份 ...

  2. 【javascript】设为首页——setHome

    原生 js 编写,兼容 ie,火狐和谷歌. 函数如下: function setHome(obj,url){ try{ obj.style.behavior = 'url(#default#homep ...

  3. jQuery(八):属性操作

    一.获取或设置元素的属性值 attr()获取或设置匹配元素的属性值,语法如下: 获取元素属性值示例: <!DOCTYPE html> <html lang="en" ...

  4. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  5. excel数据批量导入

    1.  html <form id="form_search" action="@Url.Action("UpLoadFile")" ...

  6. n个括号对的所有可能情况

    所有可能情况的数量为卡特兰数.故求所有可能的出栈情况与此类似. 思路: 若左括号没全插入,则插入左括号: 若已插入左括号数比已插入右括号数多,则插入右括号: #include<stdio.h&g ...

  7. Development Tools

    Introduction Even Chris created his article of Useful Reference Books ages ago I just bumped into it ...

  8. nano 命令 linux

    用途说明 nano是一个字符终端的文本编辑器,有点像DOS下的editor程序.它比vi/vim要简单得多,比较适合Linux初学者使用.某些Linux发行版的默认编辑器就是nano.(nano - ...

  9. 关于Unity的NGUI

    NGUI是严格遵循KISS原则并用C#编写的Unity(适用于专业版和免费版)插件,提供强大的UI系统和事件通知框架 KISS原则:Keep It Simple,Stupid NGUI实例 1.创建U ...

  10. Linux 安装 MongoDB数据库

    1下载: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.7.tgz (下载较慢) 2.安装: mv mongodb-li ...