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();
    }
}

        采用依赖注入,允许动态配置资源文件位置,无须将资源文件位置写在代码中,当资源文件位置发生变化时,无须改写程序,直接修改配置未见即可。

啦啦啦

啦啦啦

时间: 2024-08-04 22:14:37

8 -- 深入使用Spring -- 3...3 使用Resouce作为属性的相关文章

spring学习一——基本搭建,属性注入的两种方式

今天用spring 3.2.5搭建了基本的环境,spring出的太快了,前段时间才3.2.5,今儿个一瞧已经上了4的版本了,稍后给出spring的jar下载地址,毕竟现在官网上找不到了啊. 废话少说了,spring 3.2.5已经将所有的依赖包都放在了dist的lib下面,并且都有doc包和源码包,很是方便.先导入所需的jar包:core,context,beans,expression 四个jar包,除此之外,还需导入commons-logging. 下一步,新建xml文件,建议名称为 app

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

使用属性占位符可以将 Spring 配置文件中的部分元数据放在属性文件中设置,这样可以将相似的配置(如 JDBC 的参数配置)放在特定的属性文件中,如果只需要修改这部分配置,则无需修改 Spring 配置文件,修改属性文件即可.下面是一个属性占位符的示例. Spring  配置: <!-- 将配置值具体化到一个属性文件中,并且使用属性文件的key名作为占位符 --> <context:property-placeholder location="classpath:jdbc.p

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

Spring 声明式事务,propagation属性列表 TransactionDefinition接口中定义,共有7种选项可用: PROPAGATION_REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择.PROPAGATION_SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行.PROPAGATION_MANDATORY:支持当前事务,如果当前没有事务,就抛出异常.PROPAGATION_REQUIRES_NEW:新建事务,如果当前存在事务,

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

凡是被Spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. 如: @Configuration public class MyWebAppConfigurer implements EnvironmentAware { private static final Logger logger = LoggerFactory.getLogger(MyWebAppConfi

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

凡是被spring管理的类,实现接口 EnvironmentAware 重写方法 setEnvironment 可以在工程启动时,获取到系统环境变量和application配置文件中的变量. 如: @Configuration public class MyWebAppConfigurer implements EnvironmentAware { private static final Logger logger = LoggerFactory.getLogger(MyWebAppConfi

spring 的配置 bean>>property>>name属性

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.or

spring boot中通用应用程序属性

可以在application.properties文件内部application.yml,文件内部或命令行开关中指定各种属性.本附录提供了常见的Spring Boot属性列表以及对使用它们的基础类的引用. 核心属性: 键 默认值 描述 debug false 启用调试日志. info.*   要添加到信息端点的任意属性. logging.config   日志记录配置文件的位置.例如,用于logback的`classpath:logback.xml`. logging.exception-con

spring利用注解@Value获取properties属性为null

原因可能有好几种 1.spring与spring mvc是两个容器,调用时候要分清哪个容器 2.@Component 并且 context:component-scan 3.获取配置文件http://www.henryxi.com/read-values-from-properties-file-in-spring

Spring中 bean定义的parent属性机制的实现分析

在XML中配置bean元素的时候,我们常常要用到parent属性,这个用起来很方便就可以让一个bean获得parent的所有属性 在spring中,这种机制是如何实现的?     对于这种情况 transactionProxy01的parent属性是transactionProxy1 此时我们要获取transactionProxy01的实例 spring应该如何处理呢? <bean id="transactionProxy01" parent="transactionP