Properties文件的值有等号和换行 解决Properties属性文件中的值有等号和换行的小问题

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Properties文件的值有等号和换行 解决Properties属性文件中的值有等号和换行的小问题

之健   2021-08-28 我要评论
想了解解决Properties属性文件中的值有等号和换行的小问题的相关内容吗,之健在本文为您仔细讲解Properties文件的值有等号和换行的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Properties文件的值,等号和换行,下面大家一起来学习吧。

Properties属性文件中的值有等号和换行

Spring配置Shiro的过滤器时,有个filterChainDefinitions属性,值中有等号有换行,尝试写到Properties属性文件中遇到问题

<!-- 配置shiro过滤器 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <!-- 表示现在要配置的是一个安全管理器 -->
    <property name="securityManager" ref="securityManager"/>
    <!-- 出现错误之后的跳转路径的配置 -->
    <property name="loginUrl" value="/login.html"/>
    <!-- 认证失败之后的跳转路径页面 -->
    <property name="unauthorizedUrl" value="/login.html"/>
    <!-- 登录成功之后的跳转访问路径 -->
    <property name="successUrl" value="/pages/welcome.jsp"/>
 
    <property name="filterChainDefinitions">
          <value>
             /admin=authc
             /logout=logout
             /xxxxxx=user
          </value>
    </property>
</bean>

Properties属性文件可以这样写

shiro.loginUrl=/login.html
shiro.unauthorizedUrl=/login.html
shiro.successUrl=/pages/welcome.jsp
shiro.filterChainDefinitions=/admin=authc \n\
                        /logout=logout \n\
                        /info=authc

后面的等号不需要转义,\n表示值中的换行,再加个转义符\表示值还没结束,这样就没问题了

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <property name="loginUrl" value="${shiro.loginUrl}"/>
    <property name="unauthorizedUrl" value="${shiro.unauthorizedUrl}"/>
    <property name="successUrl" value="${shiro.successUrl}"/>
 
    <property name="filterChainDefinitions">
        <value>${shiro.filterChainDefinitions}</value>
    </property>
</bean>

处理properties文件中key包含空格和等号的情况

在properties文件中都是以key=value的方式存储的,在java代码中用java.util.Properties的load方法,存储在一个map中,当key中有空格和等号的时候,要用\(斜杠)进行转义,而用xml的话,就没有转义这么麻烦了,所以推荐使用xml了。

处理方案

Spike.java

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
public class Spike {
 public static void main(String[] args) throws Exception {
  readProperties();
  System.out.println("==================================================");
  readXml();
 }
 private static void readProperties() throws IOException {
  Properties props = new Properties();
  InputStream inStream = Spike.class.getResourceAsStream("Mock.properties");
  props.load(inStream);
  Enumeration enums = props.propertyNames();
  while (enums.hasMoreElements()) {
   String key = (String) enums.nextElement();
   System.out.println("Property--->>>>[" + key + "]    " + "Value--->>>>" + props.getProperty(key));
  }
 }
 private static void readXml() throws IOException {
  Properties props = new Properties();
  InputStream inStream = Spike.class.getResourceAsStream("Mock.xml");
  props.loadFromXML(inStream);
        Enumeration enums = props.propertyNames();
  while (enums.hasMoreElements()) {
   String key = (String) enums.nextElement();
   System.out.println("Property--->>>>[" + key + "]    " + "Value--->>>>" + props.getProperty(key));
  }
 }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们