[转自] http://unmi.cc/spring-injection-system-properties-env/

在 Spring 中为 javabean 注入属性文件中的属性值一般人都知道的,可以通过 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 引入一个属性文件,然后给 bean 指定属性的时候就可以用 ${jdbc.url} 方式赋值了。比如在 Spring 中是这样的配置:

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
</bean>

只是有时候我们需要给 bean 赋上系统属性(System.getProperties() ) 中的值或环境变量(System.getenv() ) 中的值,根据程序所处的环境产生不同的行为,这样我们无法事先在某个 properties 文件预先设定好值的。

这种需求也是不怕做不到就怕想不到,基于此,既然上面是用 PropertyPlaceholderConfigurer 来读取属性文件,那么有没有像 EnvironmentPlaceholderConfigurer 类似的东西呢,查下 api 没找到相关的,只是看下它有两个子类:

org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer

org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer

上面那两个类估计以后也是用得着,前面那个应该是从系统配置或注册表里要属性,后面的可以从 ServletContext 取相关属性值。

上面的思路应该是这样的,但实际上只要仔细阅读下 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 的 JavaDoc API 就会发现,它不光能从属性文件里取值,也能从系统属性,甚至是环境变量中取值。

Default property values can be defined via "properties", to make overriding definitions in properties files optional. A configurer will also check against system properties (e.g. "user.dir") if it cannot resolve a placeholder with any of the specified properties.
This can be customized via "systemPropertiesMode".

再往下看:

public void setSystemPropertiesMode(int systemPropertiesMode)

Set how to check system properties: as fallback, as override, or never. For example, will resolve ${user.dir} to the "user.dir" system property.

The default is "fallback": If not being able to resolve a placeholder with the specified properties, a system property will be tried. "override" will check for a system property first, before trying the specified properties. "never" will not check system properties
at all.

---------------------------------------------------------------------

public void setSearchSystemEnvironment(boolean searchSystemEnvironment)

Set whether to search for a matching system environment variable if no matching system property has been found. Only applied when "systemPropertyMode" is active (i.e. "fallback" or "override"), right after checking JVM system properties.

Default is "true". Switch this setting off to never resolve placeholders against system environment variables. Note that it is generally recommended to pass external values in as JVM system properties: This can easily be achieved in a startup script, even for
existing environment variables.

NOTE: Access to environment variables does not work on the Sun VM 1.4, where the corresponding System.getenv(java.lang.String) support was disabled - before it eventually got re-enabled for the Sun VM 1.5. Please upgrade to 1.5 (or higher) if you intend
to rely on the environment variable support.

说明了这个类可以从三个地方取属性值,并且可以设置覆盖关系,覆盖关系在此就不细究了。现在可以例子说明如何从系统属性或环境变量中取值了。演示一下从把系统属性 user.dir 和环境变量 COMPUTERNAME 注入到 javabean。

一. 要被注入系统属性和环境变量的 Bean

package cc.unmi.spring;

/**
* 通过 Spring 来向其中注入系统属性 workDir(${user.dir}, System.getProperty(""))
* 和环境变量 hostName(${COMPUTERNAME}, System.getenv().get("COMPUTERNAME"));
* @author Unmi
*
*/
public class Environments {
private String workDir;
private String hostName; public String getHostName() {
return hostName;
} public void setHostName(String hostName) {
this.hostName = hostName;
} public String getWorkDir() {
return workDir;
} public void setWorkDir(String workDir) {
this.workDir = workDir;
}
}

二. Spring 配置文件

<?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.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> <bean id="env" class="cc.unmi.spring.Environments">
<property name="workDir" value="${user.dir}"/>
<property name="hostName" value="${COMPUTERNAME}"/>
</bean>
</beans>

三. 测试客户端代码

package cc.unmi.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cc.unmi.spring.Environments; /**
* 测试客户端
* @author Unmi
*/
public class TestClient { public static void main(String[] args) {
BeanFactory context = new ClassPathXmlApplicationContext("applicationContext.xml");
Environments env = context.getBean(Environments.class);
System.out.println("Working directory: " + env.getWorkDir());
System.out.println("Host name: " + env.getHostName());
}
}

四. 执行上面的 TestClient 后控制台下输出的结果是:

Working directory: D:\workspaces\j2ee\TestSpring

Host name: Unmi-Computer

这显示了 Spring 已成功向 Environments 注入了系统属性 user.dir 和环境变量 COMPUTERNAME。

注:你可以用 System.getProperties().list(System.out) 打印出系统属性,还不知道系统中可用哪个命令查得到其中的值:显示出来是这样的:

-- listing properties --

java.runtime.name=Java(TM) SE Runtime Environment

sun.boot.library.path=C:\Program Files\Java\jre6\bin

java.vm.version=17.1-b03

java.vm.vendor=Sun Microsystems Inc.

java.vendor.url=http://java.sun.com/

path.separator=;

java.vm.name=Java HotSpot(TM) Client VM

file.encoding.pkg=sun.io

user.country=US

...................................

环境变量可以用 System.out.print(System.getenv()) 打印出来,对应着 Windows 中的 set 命令或 Linux  下的 env 输出的值,System.getenv() 是个 Map,所以打印出这样子的:

{USERPROFILE=C:\Documents and Settings\Unmi, JAVA_HOME=C:\Program Files\Java\jdk1.6.0_20, TEMP=C:\DOCUME~1\uqiu\LOCALS~1\Temp, OS=Windows_NT, COMPUTERNAME=Unmi-Computer, .......}

Spring中如何向 Bean注入系统属性或环境变量的更多相关文章

  1. java中获取系统属性以及环境变量

    java中获取系统属性以及环境变量 System.getEnv()和System.getProperties()的差别 从概念上讲,系统属性 和环境变量 都是名称与值之间的映射.两种机制都能用来将用户 ...

  2. Java获取系统属性及环境变量

    当程序中需要使用与操作系统相关的变量(例如:文件分隔符.换行符)时,Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回 ...

  3. JVM系统属性 OS环境变量 JVM启动参数

    JVM系统属性(System Properties) 1.不支持通过文件查看和设置系统属性 2.可以通过JDK自带的工具jvisulavm.exe查看 3.可以在Java程序中使用API来查看系统属性 ...

  4. maven中可以直接引用的java系统属性和环境变量属性

    一.查看命令: 1 mvn help :system 二.引用 在pom文件中通过 ${变量名}来引用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  5. Spring中常见的bean创建异常

    Spring中常见的bean创建异常 1. 概述     本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...

  6. 半夜思考之查漏补缺, 在 Spring中, 所有的 bean 都是 Spring 创建的吗 ?

    Spring 是一个 bean 容器, 负责 bean 的创建, 那么所有的 bean对象都是 Spring 容器创建的吗 ? 答案是否定的. 但是乍一想, 好像所有的对象都是 Spring 容器负责 ...

  7. 九、Spring中使用@Value和@PropertySource为属性赋值

    首先回顾下在xml中我们是如何为spring的bean进行属性赋值呢? 大体是这样的 <bean id="person" class="com.atguigu.be ...

  8. Windows 如何在cmd命令行中查看、修改、删除与添加环境变量

    转自:http://www.cnblogs.com/saptechnique/archive/2013/02/17/2914222.html 首先明确一点: 所有的在cmd命令行下对环境变量的修改只对 ...

  9. 如何在cmd命令行中查看、修改、删除与添加环境变量,语法格式例子:set path;echo %APPDATA%

    如何在cmd命令行中查看.修改.删除与添加环境变量 首先明确一点: 所有的在cmd命令行下对环境变量的修改只对当前窗口有效,不是永久性的修改.也就是说当关闭此cmd命令行窗口后,将不再起作用.永久性修 ...

随机推荐

  1. APP前端开发时应注意的一些问题

    在做APP前端开发时应注意的一些问题 在整个app开发流程中,app前端开发是一个必不可少的环节,也是一个在app开发过程中重量级的角色.说到这,那么在app应用的前端开发中,又要注意什么问题呢?一. ...

  2. 36.LEN() 函数

    LEN() 函数 LEN 函数返回文本字段中值的长度. SQL LEN() 语法 SELECT LEN(column_name) FROM table_name SQL LEN() 实例 我们拥有下面 ...

  3. win32的计数增减操作的原子操作--InterLockedIncrement和InterlockedDecrement

    InterLockedIncrement and InterLockedDecrement 实现数的原子性加减. 什么是原子性的加减呢? 举个例子:如果一个变量 Long value =0; 首先说一 ...

  4. 关于Lucene分页标准

    public IEnumerable<SearchResult> Search(string keyword, string[] fieldNames, int pageSize, int ...

  5. Time - Time-interval Measurements

    public class TimeHelper { private long _start, _stop, _elapsed; /// <summary> /// 获取初始时间戳 /// ...

  6. 小程序上传多图片多附件多视频 c#后端

    前言: 最近在研究微信小程序,本人自己是C#写后端的;感觉小程序挺好玩的,就自己研究了一下:刚好今天又给我需求,通过小程序上传多图 然后C#后端保存到服务器: 用NET明白 前端上传需要用到流,然后就 ...

  7. C#多线程编程实战1.5检测线程状态

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  8. SQL Server 根据关键字和结束符提取字符串子串

    /* @info-待截取的字符串 @indexStr-截取子串的起始字符串 @split-截取子串的结束符号 列入依次传入 胸片:正常.心电图:异常,需要注意.血常规检查:正常. 心电图 '.' 返回 ...

  9. Ubuntu 安装后的配置及美化(二)

    Ubuntu安装后的配置与美化(二) 上篇文章讲了安装ubuntu后的一系列基础的配置,已经可以满足日常的使用了,这篇文章讲一下安装 IDE 及一些其他的配置. 1.安装 SSR 下载 SSR 客户端 ...

  10. np.random.seed(0)的作用:作用:使得随机数据可预测。

    >>>> numpy.random.seed(0) ; numpy.random.rand(4) array([ 0.55,  0.72,  0.6 ,  0.54]) > ...