zoukankan      html  css  js  c++  java
  • 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")

  • 相关阅读:
    从ReentrantLock的实现看AQS的原理及应用
    Java 守护线程
    js静态文件编辑器显示操作,但网页显示中文乱码 解决方案
    springmvc 4.3版本集成 Caffeine缓存系统
    chrome浏览器如何设置黑色背景
    电脑型号19 1200 固态SSD
    电脑型号18 1200 固态SSD
    geohash st_distance st_distance_sphere 关系
    Git自动输入账户名密码。明文及SSH私钥2种方式
    一步快速获取 iOS 设备的 UDID
  • 原文地址:https://www.cnblogs.com/beijingstruggle/p/5634444.html
Copyright © 2011-2022 走看看