• 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")

  • 相关阅读:
    TrendMicro PcCillin 2005卸载密码忘记。
    解决Exchange OWA用户登陆后有的用户文件夹显示中文有的显示英文。
    注册表解锁
    修改Linux Fedora Grup的默认启动系统.
    OfficeXP升级到SP3后outlook出现的恼人的提示。
    在 Outlook 中单击超链接时收到错误信息
    windows sbs 2003的功能限制和其他用法
    今天打了加强型乙肝疫苗。
    WSUS服务器的详细配置和部署转载
    当邮箱存储数据库达到 16 GB 限制时,Exchange Server 2003 邮箱存储无法装入
  • 原文地址:https://www.cnblogs.com/beijingstruggle/p/5634444.html
Copyright © 2020-2023  润新知