<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil"
lazy-init="false">
<property name="locations">
<list>
<value>classpath:config/sys.properties</value>
</list>
</property>
</bean>

applicationContext.xml文件中配置好sys.properties文件的路径 ↑↑↑↑

然后是springPropertiesUtil文件内容:

package com.****.framework.core;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import java.util.HashMap;
import java.util.Map;
import java.util.Properties; public class SpringPropertiesUtil extends PropertyPlaceholderConfigurer { private static Map<String, String> propertiesMap;
// Default as in PropertyPlaceholderConfigurer
private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK; @Override
public void setSystemPropertiesMode(int systemPropertiesMode) {
super.setSystemPropertiesMode(systemPropertiesMode);
springSystemPropertiesMode = systemPropertiesMode;
} @Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
super.processProperties(beanFactory, props); propertiesMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String valueStr = resolvePlaceholder(keyStr, props, springSystemPropertiesMode);
propertiesMap.put(keyStr, valueStr);
}
}
public static String getProperty(String name) {
return propertiesMap.get(name).toString();
} public static String getProperty(String name,String def) {
String ret = propertiesMap.get(name);
if(StringUtils.isBlank(ret)){
return def;
}
return ret.toString();
} }

第三步是正题:在java代码中调用这个配置:

 SpringPropertiesUtil springPropertiesUtil = (SpringPropertiesUtil) applicationContext.getBean("propertyConfigurer");
return springPropertiesUtil.getProperty(key);

Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)的更多相关文章

  1. Spring MVC框架下的第一个Hello World程序

    本程序是一个maven程序,使用maven方便管理jar包和程序,简化了操作步骤.本程序的目的是通过一个简单的程序,了解Spring MVC框架的基本工作流程,由简入繁的学习Spring MVC框架, ...

  2. 不使用spring的情况下原生java代码两种方式操作mongodb数据库

    由于更改了mongodb3.0数据库的密码,导致这几天storm组对数据进行处理的时候,一直在报mongodb数据库连接不上的异常.   主要原因实际上是和mongodb本身无关的,因为他们改的是配置 ...

  3. Spring mvc框架下使用kaptcha生成验证码

    1.下载jar包并导入. kaptcha-2.3.2.jar 2.spring 配置文件 applicationContext.xml. <bean id="captchaProduc ...

  4. Spring MVC框架下 将数据库内容前台页面显示完整版【获取数据库人员参与的事件列表】

    1.书写jsp页面包括要显示的内容[people.jsp] <!-- 此处包括三个方面内容: 1.包含 文本输入框 查询按钮  查询结果显示位置 (paging) 2.包括对按钮(button) ...

  5. Spring MVC框架下提交Date数据无法在controller直接接收

    主要有两步,controller中添加initBinder方法,再创建一个时间类型数据转换类就OK了. 1.在Controller中创建方法: // 相关包 import java.text.Date ...

  6. Spring MVC框架下 从后台读取数据库并显示在前台页面【笔记自用 不推荐作为参考】

    1.书写jsp页面  people.jsp 1.设计显示格式以及内容显示 2.设计显示内容的范围 2.书写entity实体类 PeopleFormMap.java 书写传入的参数主要包括 要引用的数据 ...

  7. Spring Boot 框架下使用MyBatis访问数据库之基于XML配置的方式

    MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...

  8. 从零开始学 Java - 搭建 Spring MVC 框架

    没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...

  9. 十七、IntelliJ IDEA 中的 Maven 项目初体验及搭建 Spring MVC 框架

    我们已经将 IntelliJ IDEA 中的 Maven 项目的框架搭建完成.接着上文,在本文中,我们更近一步,利用 Tomcat 运行我们的 Web 项目. 如上图所示,我们进一步扩展了项目的结构, ...

随机推荐

  1. ASP中可能出现的一种包含漏洞(Server.execute)

    author: bin <% Server.execute(request(“file”)) %> 与include的区别,它可以动态包含文件. 被包含文件里面可执行ASP代码,在国外的源 ...

  2. Ubuntu 14.04 使用的一些笔记

    1. 今天下载了安卓的2.3.5的源代码,解压tar的时候,发现一个错误中断了解压过程: could not create the hard link file, 看了一下基本上都是每个主目录下的 . ...

  3. 微博java SDK介绍及使用说明

    转自:作者:新浪微博 开放平台 @MUNTO_AKIRA http://open.weibo.com/blog/%E5%BE%AE%E5%8D%9Ajava-sdk%E4%BB%8B%E7%BB%8D ...

  4. Disaster recovery best practices for Symantec Endpoint Protection 12.1

    https://support.symantec.com/en_US/article.TECH160736.html

  5. 【iBoard电子学堂】【iCore双核心板】资料光盘A盘更新,版本号为A6

    _____________________________________ 深入交流QQ群: A: 204255896(1000人超级群,可加入) B: 165201798(500人超级群,满员) C ...

  6. gridview header增加排序图标

    /* add sorting icons to gridview sort links */a.asc:after, a.desc:after { position: relative; top: 1 ...

  7. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  8. PHP报错: Can't use method return value in write context

    $dp_id = $this->getParam('dpId'); if(!empty($this->getParam('dpId'))) { $this->smarty->a ...

  9. Bootstrap页面布局21 - BS对话框设计

    设计弹出层对话框: 设计一个点击登录按钮,再弹出一个登陆对话框的实例,且带有动画效果 <div class='container-fluid'> <h2 class='page-he ...

  10. Remote 的远程使用

        <script type="text/javascript">        $(function () {            //每次隐藏的时候 删除页面 ...