首页 > 编程语言 > 详细

java加载properties配置文件的几种方法

时间:2020-06-29 20:23:24      阅读:66      评论:0      收藏:0      [点我收藏+]

 

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;
    }
}

 

java加载properties配置文件的几种方法

原文:https://www.cnblogs.com/adao21/p/13209886.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!