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;
        }
    }
  • 相关阅读:
    HDU 1058
    Codeforces 349C
    HDU 2602
    HDU 2571
    HDU 2955
    HDU 2084
    HDU 1003
    HDU 1506 & 1505
    POJ 1854
    HDU 2095
  • 原文地址:https://www.cnblogs.com/adao21/p/13209886.html
Copyright © 2011-2022 走看看