Spring_属性占位符

忍耐和坚持虽是痛苦的事情,但却能渐渐地为你带来好处。——奥维德

属性占位符

  Spring一直支持将属性定义到外部的属性的文件中,并使用占位符值将其插入到Spring bean中。

  占位符的形式为使用"${}"包装的属性名称,为了使用属性占位符,我们必须配置一个PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer实例,从Spring 3.0开始,推荐使用PropertySourcesPlaceholderConfigurer,因为它能够基于Spring Environment及其属性源来解析占位符。

  1.在基于Java配置中使用属性占位符注入属性

package chapter3.prctice6;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Component;

@Component
public class AppleMobile implements Mobile {

    private String color;

    private String type;

    public AppleMobile(@Value("${mobile.color}") String color, @Value("${mobile.type}") String type) {
        this.color = color;
        this.type = type;
    }

    @Bean
    public PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    public void play() {
        System.out.println(color+"-"+type);
    }

}

  2.在基于XML配置中使用占位符注入属性

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc         http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

        <bean id="appleMobile" class="chapter3.prctice6.AppleMobile"
        c:color="${moble.color}"
        c:type="${mobile.type}">
        </bean>
        <context:property-placeholder/>
</beans>

  解析外部属性能够将值的处理推迟到运行时,它的关注点在于根据名称解析来自于Spring Environment和属性源的属性。

原文地址:https://www.cnblogs.com/dandelZH/p/8859090.html

时间: 2024-08-28 02:44:12

Spring_属性占位符的相关文章

8 -- 深入使用Spring -- 1...4 属性占位符配置器

8.1.4 属性占位符配置器 PropertyPlaceholderConfigurer 是一个容器后处理器,负责读取Properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的数据. 通过使用PropertyPlaceholderConfigurer后处理器,可以将Spring配置文件中的部分数据放在属性文件中设置. XML : <?xml version="1.0" encoding="UTF-8"?> <!-- Spri

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

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

Maven 工程从一个properties 用属性占位符引用另一个properties的内容

1.pom xml 配置 <!--filter--> <build> <filters> <filter>src/main/resource/test.properties</filter><!--基础文件--> </filters> <resources> <resource> <directory>src/main/resource</directory><!--需

Spring属性占位符 PropertyPlaceholderConfigurer

http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现.PropertyPlaceholderConfigurer可以将上下文(配置文件)中的属性值放在另一个单独的标准java Properties文件中去.在XML文件中用${key}替换指定的properties文件

Spring实战(八)bean装配的运行时值注入——属性占位符和SpEL

前面涉及到依赖注入,我们一般哦都是将一个bean引用注入到另一个bean 的属性or构造器参数or Setter参数,即将为一个对象与另一个对象进行关联. bean装配的另一个方面是指将一个值注入到bean的属性or构造器参数中,通常我们可以将值硬编码在配置类中,XML中也是硬编码(写出所有值). 1.若想避免硬编码,让这些值在运行时再确定,Spring提供了两种在运行时求值的方式. 属性占位符(Property placeholder)--Spring支持将属性定义到外部的属性文件中,然后用占

Spring属性占位符PropertyPlaceholderConfigurer的使用

1.一个简单的Demo 1.1.创建conf.xml <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>    <bean id="

关于 Spring 中使用 context:property-placeholder 属性占位符设置配置文件

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 该配置文件中以键值存储,键在 spring 配置中可以用 ${} 来引用,而值在配置文件中来配置.具体键变量的生命周期,可能是当前上下文,需求证.

spring源码解析(一)---占位符解析替换

一.结构类图 ①.PropertyResolver : Environment的顶层接口,主要提供属性检索和解析带占位符的文本.bean.xml配置中的所有占位符例如${}都由它解析 ②.ConfigurablePropertyResolver : 该接口定义了如何对组件本身进行配置.如:刚刚提到获取value时可以指定任意类型,这依赖于ConversionService进行类型转换,当前接口就提供了对ConversionService的设置和获取.另外,可以配置属性占位符的格式,包括:占位符前

【Spring源码分析】.properties文件读取及占位符${...}替换源码解析

前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.properties文件中配置的参数使用占位符"${}"替换的方式读入并设置到Bean的相应参数中. 这种做法最典型的就是JDBC的配置,本文就来研究一下.properties文件读取及占位符"${}"替换的源码,首先从代码入手,定义一个DataSource,模拟一下JDB