zoukankan      html  css  js  c++  java
  • Springboot项目根据Key获取properties中的value值

    1、在springboot项目中的resources创建properties文件

    2、配置你需要的key=value

    Login=登录
    UpdatePassword=修改密码

    3、创建PropertitesUtil类读取文件,根据key获取value

    import org.springframework.core.io.ClassPathResource;
    
    import java.io.*;
    import java.util.Properties;
    
    public class PropertitesUtil extends BaseAction {
        public static Properties props;
    
        static {
            try {
                readPropertiesFile("/method.properties");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static Properties readPropertiesFile(String filePath) throws FileNotFoundException, IOException {
            try {
                ClassPathResource classPathResource = new ClassPathResource(filePath);
                InputStream inputStream = classPathResource.getInputStream();
                props = new Properties();
                props.load(new InputStreamReader(inputStream, "UTF-8"));
                return props;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public static String getPropertys(String key) {
            String methodName = props.getProperty(key);
            return methodName;
        }
    
    }

    4、pom.xml文件配置

    <resources></resources>标签中需要配置文件,否则会出现读取不到的问题
     <resources>
                <resource>
                    <directory>${project.basedir}/src/main/resources</directory>
                    <includes>
                        <include>mybatis/*</include>
                        <include>application.yml</include>
                        <include>log4j.properties</include>
                        <include>method.properties</include>
                        <include>i18n/*</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
                <resource>
                    <directory>${project.basedir}/src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
               </resource>
      </resources>
     
  • 相关阅读:
    PHP的注释规范
    IP地址与,域名,DNS服务器,端口号的联系与概念
    转: CentOS上安装LAMP之第一步:Apache环境及安装过程报错解决方案(纯净系统环境)
    转:VMware中CentOS配置静态IP进行网络访问(NAT方式和桥接模式)
    虚拟主机详细的配置
    PHP操作MySQL
    【优化】EXPLAIN--type
    数据库范式
    【优化】碎片OPTIMIZE
    【原理】原理与优化(二)
  • 原文地址:https://www.cnblogs.com/shoose/p/12930354.html
Copyright © 2011-2022 走看看