这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下:

In spring wiring ,placeholder values are property names wrapped with ${...},as an exampl,you can resolve the constructor arguments for a BlankDisc in xml like this :

<bean id="sgtPeppers"

    class="soundsystem.BlankDisc"

    c:_titile="${disc.title}"

    c:_artist="${disc.artist}"/>

下面这个案例用${...}从配置文件读取内容,

记得一定要在配置文件写上下面的语句:

   <!-- 加载.properties文件-->
<context:property-placeholder location="classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties"/>

案例的目录结构如下图所示:

案例的代码如下:

Student类的代码如下:

 package com.advancedWiring.ambiguityIniAutowiring2;

 import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext; /**
* Created by ${秦林森} on 2017/6/9.
*/
public class Student implements BeanNameAware{
private String name;
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public void setBeanName(String name) {
System.out.println("the Student bean name is :"+name);
} /**
* write this constructor mainly to inject external property value.
* @param name the student's name
* @param age the student's age
*/
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
}

student.properties文件的代码如下:

 #用配置文件的形式,避免注入属性值的硬代码化。
name=AbrahamLincoln
age=21

ambiguity.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:annotation-config/>
<!-- 加载.properties文件-->
<context:property-placeholder location="classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties"/>
<bean id="student"
class="com.advancedWiring.ambiguityIniAutowiring2.Student"
c:name="${name}"
c:age="${age}">
</bean>
</beans>

测试类Test的代码如下:

 package com.advancedWiring.ambiguityIniAutowiring2;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by ${秦林森} on 2017/6/9.
*/
public class Test {
public static void main(String[] args) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/advancedWiring/ambiguityIniAutowiring2/ambiguity.xml");
Student student = ac.getBean(Student.class);
/**
* 输出结果是:the student name is: AbrahamLincoln and age is :19
* 可以看出他把配置文件的值给取出来了。
*/
System.out.println("the student name is: "+student.getName()+" and age is :"+student.getAge());
}
}

spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化的更多相关文章

  1. spring in action 学习十二:property placeholder 注解的方式实现避免注入外部属性硬代码化

    这里的注解是指@PropertySource这个注解.用@PropertySource这个注解加载.properties文件. 案例的目录结构如下: student.properties的代码如下: ...

  2. spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入

    一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...

  3. spring in action 学习笔记十三:SpEL语言(Spring Expression Language)

    SpEl语言的目的之一是防止注入外部属性的代码硬代码化.如@Value("#{student.name}")这个注解的意思是把Student类的name的属性值注入进去.其中stu ...

  4. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

  5. Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...

  6. spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。

    在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...

  7. spring in action 学习笔记十四:用纯注解的方式实现spring mvc

    在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...

  8. Spring in Action 学习笔记一

    Spring 核心       Spring的主要特性仅仅是 依赖注入DI和面向切面编程AOP       JavaBean 1996.12 Javav 规范针对Java定义了软件组件模型,是简单的J ...

  9. spring in action 学习笔记十:用@PropertySource避免注入外部属性的值硬代码化

    @PropertySource的写法为:@PropertySource("classpath:某个.properties文件的类路径") 首先来看一下这个案例的目录结构,重点看带红 ...

随机推荐

  1. sql server几种Join的区别测试方法与union表的合并

    /* sql server几种Join的区别测试方法 主要来介绍下Inner Join , Full Out Join , Cross Join , Left Join , Right Join的区别 ...

  2. PL/sql中如何声明变量,常量,控制语句及for,loop,while和顺序控制的使用

    pl/sql 什么是PL/SQL PL/SQL是结合oracle过程语言和机构化查询运行(SQL) 的一种扩展语言.使用PL/SQL可以编写具有很多高级功能的程序,有以下优点 PL/SOL可以采用过程 ...

  3. ajax原生js及readystate/status

    菜鸟教程 ←← GET: <script> function  ajaxGet(){ var  xmlhttp; if(window.XMLHttpRequest){ //TE7+  Fi ...

  4. tcl之文件操作

  5. C语言进阶——类型转换04

    C语言内可以进行类型转换: 强制类型转换 隐式类型转换 强制类型转换的语法: (tpye)value (type)value_name 强制类型转换的结果: 目标类型可以容纳目标值:结果不变 目标值不 ...

  6. Maven系列之快速入门

    文章结构 唯快不破---Maven快速入门 稳打稳扎---Maven核心知识 实用为先---Maven如何建立Web项目  1   唯快不破---Maven快速入门       1.1 Maven项目 ...

  7. JS 金钱格式化

    JavaScript Money Format(用prototype对Number进行扩展) Number.prototype.formatMoney = function (places, symb ...

  8. 一个python的文件对比脚本

    脚本主要用来给游戏客户端做热更的. 处理方式就是针对每个文件求其MD5值,再根据文件的目录和名字对比两个版本的MD5值,如果不一样,则这次热更就需要更新这个文件. 用法很简单. 1,生成MD5码列表 ...

  9. ICG-智能代码生成器.(权限控制.融入平台).(表单引擎).(最低兼容IE8)

    请下拉滚动条... 代码生成器.附带客户端代码 个人平台:www.10086bank.com 界面: 1--首先是server制作界面(BS结构).直接上图:   2--点击提交生成一下文件: 各个代 ...

  10. Python 3基础教程4-变量

    本文介绍变量,什么是变量呢,可以这样理解:变量是一个容器,这个容器可以用来存储值,而且可以被其他对象引用. 看看下面的demo.py # 这里介绍 变量 # 变量可以是数字var1 = 5print( ...