zoukankan      html  css  js  c++  java
  • java加载properties配置文件的几种方法

     自由自在 废话省略...

    1.普通方法 直接上我写的一共配置文件获取类:

    配置文件

    socket.server.address = 127.0.0.1
    socket.server.port = 8511
    socket.connect.timeout = 3000
    package com.adao.common;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import org.apache.log4j.Logger;
    import org.springframework.stereotype.Component;
    
    /**
     * 配置
     * 
     * @author adao
     *
     */
    @Component
    public class PropertiesUtil {
        private final static Logger logger = Logger.getLogger(PropertiesUtil.class);
    
        private static Properties properties;
    
        public Properties getProperties() {
            return properties;
        }
    
        public static void initProperties() {
            properties = new Properties();
            try {
                InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("application.properties");
                properties.load(in);
                in.close();
                logger.error("配置文件加载完毕. ");
            } catch (IOException e) {
                logger.error("配置文件加载发生异常. ", e);
            }
        }
    
        public static Properties loadProperties() throws IOException {
            Properties properties = new Properties();
            InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("application.properties");
            properties.load(in);
            in.close();
            return properties;
        }
    
    }

    获取方法:

        resource = new PropertiesUtil().getProperties();
           String serverAddress = resource.getProperty("socket.server.address");
        int serverPort = Integer.valueOf(resource.getProperty("socket.server.port"));
        int connectTimeout = Integer.valueOf(resource.getProperty("socket.connect.timeout")); // 超时时间

    2.springboot加载配置文件:

    1.配置文件

    connection.username=adao
    connection.password=adao@126.com

    2.定义一个实体类在装载配置文件信息

    @Component
    @ConfigurationProperties(prefix="connection")
    public class ConfigProperties{
        private String username;
        private String password ;
     
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
     
    }

    3.在App类中配置bean的配置文件

       @SpringBootApplication
       public class DemoApplication{
        //...
       @Resource
       private  ConfigProperties  configProperties;
     
        @Bean
        public JavaClass  javaclass (){
       
        JavaClass  javaClass = new JavaClass();
        javaClass.setxxx("",configProperties.getUsername());
        javaClass.setxxx("",configProperties.getPassword() );
     
        return javaClass;
        }
     
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    或 在项目中,获取properties配置文件属性

      @RestController
      @RequestMapping("/user")
      public class  UserController {
     
      @Autowired 
      private ConfigProperties config;
     
      @RequestMapping("getproper")
      public String userInfo(){
           String userName = config.getUsername();     
          return userName;
        }
    }
  • 相关阅读:
    swift 学习资源 大集合
    ACM:回溯,八皇后问题,素数环
    hibernate 批量处理数据
    CTR校准
    Android Handler Message总结一下
    使用腾讯电子邮件,邮箱的一部分是无法接收正常邮件的问题
    JS获得URL参数
    JSP 获取访问者真正的IP地址
    根据百度API获得经纬度,然后根据经纬度在获得城市信息
    oracle initialization or shutdown in progress解决方法
  • 原文地址:https://www.cnblogs.com/adao21/p/13209886.html
Copyright © 2011-2022 走看看