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

 

posted @ 2020-06-29 19:33  adao  阅读(753)  评论(0编辑  收藏  举报