spring 的 PropertyPlaceholderConfigurer读取的属性怎么访问 (java访问方式,不是xml中的占位符哦)及此类的应用
一、1.占位符的应用:(@Autowired注解方式,不需要建立set与get方法了,xml注入也不需要写了)
http://www.cnblogs.com/susuyu/archive/2012/09/10/2678458.html
二、
2、java方式访问(已经验证过好用)
1、通过spring配置properties文件
[java] <bean id="propertyConfigurer" class="com.tjsoft.base.util.CustomizedPropertyPlaceholderConfigurer"> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>/WEB-INF/config/jdbc.properties</value> <value>/WEB-INF/config/mail.properties</value> <value>/WEB-INF/config/system.properties</value> </list> </property> </bean>
其中class为自己定义的类
2、自定义类CustomizedPropertyPlaceholderConfigurer
[java] import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /** * 自定义PropertyPlaceholderConfigurer返回properties内容 * * @author LHY 2012-02-24 * */ public class CustomizedPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private static Map<String, Object> ctxPropertiesMap; @Override protected void processProperties( ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropertiesMap = new HashMap<String, Object>(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } www.2cto.com } public static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); } }
这样就可以通过CustomizedPropertyPlaceholderConfigurer类来获取properties属性文件中的内容了
3、如何获取属性文件的内容
String host = (String) CustomizedPropertyPlaceholderConfigurer.getContextProperty("mail.smtp.host");
三、未验证,不知道好用不
action也是spring配置的bean吗?
是的话,直接注入就行了。
<bean id="actionname" class="com.MyAction">
<property name="property1" value="${configed.property1}"/>
</bean>
如果不是,可以将这个property注入到System.properties中去
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
depends-on="PlaceholderConfigurer">
<property name="targetClass">
<value>java.lang.System</value>
</property>
<property name="targetMethod">
<value>setProperty</value>
</property>
<property name="arguments">
<list>
<value>property1</value>
<value>${configed.property1}"</value>
</list>
</property>
</bean
然后在action中使用System.getProperty("property1")
spring 的 PropertyPlaceholderConfigurer读取的属性怎么访问 (java访问方式,不是xml中的占位符哦)及此类的应用的更多相关文章
- Spring属性占位符 PropertyPlaceholderConfigurer
http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html PropertyPlaceholderConfigurer是个bean工厂后 ...
- mybatis与spring整合时读取properties问题的解决
在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: <context:property-placehold ...
- Android (cocos2dx 网络访问)访问权限设置
Android开发应用程序时,如果应用程序需要访问网络权限,需要在 AndroidManifest.xml 中加入以下代码: 同样的如果用到其它的权限,也需要作出声明,部分权限列表如下: androi ...
- Spring拓展接口之BeanFactoryPostProcessor,占位符与敏感信息解密原理
前言 开心一刻 一只被二哈带偏了的柴犬,我只想弄死隔壁的二哈 what:是什么 BeanFactoryPostProcessor接口很简单,只包含一个方法 /** * 通过BeanFactoryPos ...
- Spring PropertyResolver 占位符解析(一)API 介绍
Spring PropertyResolver 占位符解析(一)API 介绍 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html ...
- Spring中手动增加配置文件中占位符引用的变量
在项目中遇到一个这样的需求,项目的配置文件由外部传入,这时spring配置文件那些占位符变量该如何取值呢? 解决这个问题的做法有几种,我想到的大概有以下三种: 1.通过系统属性来实现,把外部传入的配置 ...
- Spring Cloud Finchley.SR1 版本的坑:placeholer占位符无法解析!
接入nacos 之后,想把所有的配置丢上去. 启动程序是: @EnableDiscoveryClient @RestController @ComponentScan(basePackages = { ...
- SPRING多个占位符配置文件解析源码研究--转
原文地址:http://www.cnphp6.com/archives/85639 Spring配置文件: <context:property-placeholder location=&quo ...
- spring占位符解析器---PropertyPlaceholderHelper
一.PropertyPlaceholderHelper 职责 扮演者占位符解析器的角色,专门用来负责解析路劲中or名字中的占位符的字符,并替换上具体的值 二.例子 public class Prope ...
随机推荐
- loadrunner调用plink,远程linux执行shell命令
loadrunner调用plink,远程linux执行shell命令 脚本: Action() { char* cmd; cmd = lr_eval_string("C:\\\&qu ...
- MVC视图中ViewStart/RenderSection/Layout/Partial
1.母板页_Layout.cshtml 2.部分视图 3.默认Layout引用的使用(_ViewStart.cshtml) Layout = "~/Views/Shared/_Layout. ...
- Android SlidingMenu 滑出侧边栏
最近有个项目需要使用侧边栏,而且希望是左右两侧都能够滑出侧边菜单,在网上查找实现方式时,发现大家用的最多的还是大神jfeinstein10的SlidingMenu库,地址https://github. ...
- openSuSE12.1 zypper LAMP
LAMP是由Apache MySQL PHP组成的,是在Linux下最受欢迎的软件组合之一,目前互联网上有很多网站运行在LAMP服务器上. Linux - 是富有情味的开源操作系统:Apache - ...
- Lesson: Introduction to JAXP
The Java API for XML Processing (JAXP) is for processing XML data using applications written in the ...
- Maven source jar get
Maven作为项目管理的包,同时也能方便的将source-jar下载的本地,省去了每次到网上分别下载的问题. Maven下载Jar包同时下载源文件和文档 示例,在maven生成的工程里面:mvn ec ...
- radix树
今天在学Linux内核页高速缓存时,学到了一种新的数据结构radix树(基数),经过分析,感觉此数据结构有点鸡肋,有可能是我理解不到位吧. 先来张图,给大家以直观的印象 当有一个key-value型的 ...
- easyui在当前tab中直接加载url
var tab = $('#mainTabs').tabs('getSelected'); // get selected panel $('#mainTabs').tabs('update', { ...
- Jmeter聚合报告分析
Label:每个 JMeter 的 element(例如 HTTP Request)都有一个 Name 属性,这里显示的就是 Name 属性的值 Average:平均响应时间--默认情况下是单个 Re ...
- Android开发:如何安全的中止一个自定义线程Thread
http://blog.csdn.net/yanzi1225627/article/details/8582078 经研究,我推荐这种写法: /*自定义线程*/ class MyThread impl ...