`

读取spring加载的properties的值

 
阅读更多
代码来自网络,测试有效
package com.cqjk.util;  
import java.lang.reflect.Method;
import java.util.Properties;

import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.PropertyResourceConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.support.PropertiesLoaderSupport;
/**
 * 
 * @author Administrator
* @ClassName: SpringPropertyResourceReader 
* @Description: TODO(获取spring加载的资源文件的属性值) 
* @author A18ccms a18ccms_gmail_com 
* @date 2014-7-7 上午10:04:01
 */
public class SpringPropertyResourceReader {  
    private static ApplicationContext applicationContext=new   
    ClassPathXmlApplicationContext("com/cqjk/config/applicationContext.xml");  
    private static AbstractApplicationContext abstractContext =  
    (AbstractApplicationContext) applicationContext;  
    private static Properties properties=new Properties();  
    static{  
        try{  
            // get the names of BeanFactoryPostProcessor  
            String[] postProcessorNames = abstractContext  
                    .getBeanNamesForType(BeanFactoryPostProcessor.class,true,true);  
              
            for (String ppName : postProcessorNames) {  
                // get the specified BeanFactoryPostProcessor  
                BeanFactoryPostProcessor beanProcessor=  
                (BeanFactoryPostProcessor) abstractContext.getBean(ppName, BeanFactoryPostProcessor.class);  
                // check whether the beanFactoryPostProcessor is   
                // instance of the PropertyResourceConfigurer  
                // if it is yes then do the process otherwise continue  
                if(beanProcessor instanceof PropertyResourceConfigurer){  
                    PropertyResourceConfigurer propertyResourceConfigurer=  
                            (PropertyResourceConfigurer) beanProcessor;  
                      
                    // get the method mergeProperties   
                    // in class PropertiesLoaderSupport  
                    Method mergeProperties=PropertiesLoaderSupport.class.  
                        getDeclaredMethod("mergeProperties");  
                    // get the props  
                    mergeProperties.setAccessible(true);  
                    Properties props=(Properties) mergeProperties.  
                    invoke(propertyResourceConfigurer);  
                      
                    // get the method convertProperties   
                    // in class PropertyResourceConfigurer  
                    Method convertProperties=PropertyResourceConfigurer.class.  
                    getDeclaredMethod("convertProperties", Properties.class);  
                    // convert properties  
                    convertProperties.setAccessible(true);  
                    convertProperties.invoke(propertyResourceConfigurer, props);  
                      
                    properties.putAll(props);  
                }  
            }  
              
        }catch(Exception e){  
            throw new RuntimeException(e);  
        }  
    }  
      
    public static String getProperty(String propertyName){  
        return properties.getProperty(propertyName);  
    }  
}  

 

分享到:
评论

相关推荐

    Spring加载配置和读取多个Properties文件的讲解

    今天小编就为大家分享一篇关于Spring加载配置和读取多个Properties文件的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    spring无法读取properties文件数据问题详解

    主要介绍了spring无法读取properties文件数据问题详解,需要的朋友可以参考下

    从零开始学Spring Boot

    1.26 Spring Boot启动加载数据CommandLineRunner 1.27 Spring Boot环境变量读取和属性对象的绑定 1.28 Spring Boot使用自定义的properties 1.29 改变自动扫描的包 1.30 Spring Boot Junit单元测试 1.31 SpringBoot...

    spring boot源码

    2. 创建Spring容器对象ApplicationContext,加载各种配置 3. 在容器创建前,通过监听器机制,应对不同阶段加载数据、更新数据的需求 4. 容器初始化过程中追加各种功能,例如统计时间、输出日志等 监听器类型 1. 在...

    Spring中文帮助文档

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    Spring API

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    mina2.0+spring

    2.系统读取mina-spring.xml。如果想把该工程导成jar则需要处理下配置文件的路径 3.结合自己系统时(导成jar集成进自己系统)需要继承与serverHandler,实现里面doService方法 4.本系统支持HTTP以及TCP链接,这个是...

    00-myspring.war

    初始化阶段:重写Servlet的init方法,实现加载配置文件,读取配置信息, 扫描所有相关的类, 初始化相关类的实例并保存到IOC容器, 从IOC容器取出对应的实例给字段赋值,即依赖注入, 最后将url和Method进行关联。...

    SpringBoot启动过程-mind版.md

    1. **加载配置:** Spring Boot会读取项目中的配置文件(如`application.properties`或`application.yml`),并将其中的配置信息加载到内存中,以供后续使用。 2. **初始化应用上下文:** Spring Boot会创建一个...

    ssh(structs,spring,hibernate)框架中的上传下载

    这是Hibernate3引入的新特性,对于包含重量级大数据的表字段,这种抽取方式提高了对大字段操作的灵活性,否则加载Tfile对象的结果集时如果总是返回fileContent,这种批量的数据抽取将可以引起数据库的"洪泛效应"。...

    Java加载资源文件的两种方法

    处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道。Java将配置文件当作一种资源(resource)来处理,并且提供了两个类来读取这些资源,一个是Class类,另...

    SSH的jar包.rar

    SSH(struts+spring+hibernate)的jar包 SSH 通常指的是 Struts2 做前端控制器,Spring ...配置文件加载顺序为:default.properties -> struts-default.xml -> struts-plugins.xml -> struts.xml -> struts.locale。

    SpringBoot的配置文件

    SpringBoot默认会从Resources目录下加载application.properties或application.yml(application.yaml)文件 yml配置文件的语法 配置普通数据:语法:key:value(value之前有个空格) #普通数据的配置 name:

    java8源码-baseproject:基础项目

    java8 源码 本项目是一个分布式的App服务端快速开发框架,包含了基本的权限认证、日志处理、接口防刷、系统监控等基本功能。 此框架围绕分布式服务系统构建,能够快速扩容,迎合微服务...gradle.properties里设置profil

    Java学习笔记-个人整理的

    {1.4.5}Boolean 布尔值}{25}{subsection.1.4.5} {1.5}基本类型变量的初始值}{26}{section.1.5} {1.6}数据类型转换}{26}{section.1.6} {1.7}方法}{26}{section.1.7} {1.8}运算符}{27}{section.1.8} {1.8.1}自增...

    matchdemo:演示tamr Match API

    #Demo tamr 匹配处理应用程序 ##Overview 此应用程序...配置资源使用标准的 Spring Boot 配置加载顺序。 请参阅 ##建造 定义了一个 Spring 集成流来轮询传入目录。 传入文件启动以下流程。 将文件转换为新的 JobReq

    好用的代码生成源码

    GeneratorProperties 生成器的相关配置,用于读取generator.properties(或者是generator.xml) TableFactory用于创建Table.java对象,用于GeneratorFacade.generateByTable()使用 SqlFactory用于创建Sql.java对象,...

    isa-deployment-2020

    >“显示配置变量” ->并输入以下内容作为键从值配置文件中隐藏:值对 您可以通过单击“资源”选项卡中的Herok Postgres附加组件->设置->并单击“管理”部分中的“查看凭据...”按钮来读取连接参数。 更新...

    Java高级程序设计实战教程第三章-Java反射机制.pptx

    3.1 应用场景 在一些开源框架里,如Spring,Struts,Hibernate,MyBatis等,应用程序会提供一个配置文件如xml文件或者properties,然后在Java类里面解析xml或properties里面的内容,得到一个字符串,然后用反射机制...

    Java JDK 7学习笔记(国内第一本Java 7,前期版本累计销量5万册)

    著作:《java jdk 5.0学习笔记》、《java se 6技术手册》、《spring技术手册》等 译作:《ajax实战手册》、《jquery实战手册(第2版)》 个人网站:http://openhome.cc 目录 《java jdk 7学习笔记》 chapter1 ...

Global site tag (gtag.js) - Google Analytics