读取ApplicationResources.properties资源文件

本文介绍三种在Java中读取配置文件的方法:利用Java国际化API、Struts框架的MessageResources以及直接使用Properties类。文章详细展示了每种方法的具体实现步骤及代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用java国际化提供的方法读取
public String readProperties() {
  ResourceBundle bundle = ResourceBundle
    .getBundle("com.gsoa.properties.ApplicationResources");
  String ipAndPort = bundle.getString("MOBILE.IPANDPORT");
  return ipAndPort;
}
2、使用struts国际化提供的方法读取 
public String readProperties() {
  MessageResources mr = MessageResources
    .getMessageResources("com.gsoa.properties.ApplicationResources");
  String ipAndPort = mr.getMessage("MOBILE.IPANDPORT");
  return ipAndPort;
}

3、使用Properties类 

public String readProperties() { 

  String ipAndPort="";
  Properties prop = new Properties();
  InputStream is = this.getClass().getResourceAsStream("/com/gsoa/properties/ApplicationResources.properties");
  try {
    prop.load(is); 
    is.close(); 
    if(prop.containsKey("MOBILE.IPANDPORT")){ 
      iipAndPort=prop.getProperty("MOBILE.IPANDPORT") 
    }
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }  
  return ipAndPort;
}

 

读取src下的配置文件jdbc.properties时,

Properties prop = new Properties();
InputStream is = RreadPro.class.getResourceAsStream("/jdbc.properties");
RreadPro是把class文件所在的目录做为根目录。


Properties prop = new Properties();
InputStream is = DBCPConnection.class.getClassLoader().getResourceAsStream("jdbc.properties");

Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取。




<think>好的,我现在要帮用户解决如何使用Spring Boot的Environment类读取application.properties配置内容的问题。首先,我需要回忆一下Spring Boot的相关知识。记得Environment是Spring框架中的一个接口,它提供了访问应用程序环境属性的方法,包括系统属性、环境变量以及配置文件中的属性。 用户提到的Environment类,应该是指通过这个接口来获取配置项。我需要先确认在Spring Boot中如何获取Environment实例。通常可以通过依赖注入的方式,比如在类中使用@Autowired注解来注入Environment对象。 接下来,用户需要一个具体的示例。我需要分步骤来写,首先创建一个配置类或者Controller,然后在其中注入Environment,接着使用getProperty方法读取配置项。还要记得在application.properties中添加测试用的配置项,比如示例中的custom.message=Hello from Environment。 然后,我需要检查是否有相关的注意事项。比如,确保application.properties文件的位置正确,通常放在src/main/resources目录下。另外,可能需要确认是否需要额外的注解或配置来启用属性绑定,但根据引用内容,@ConfigurationProperties注解的类必须是@Component,而Environment本身可能不需要额外配置,因为Spring Boot默认已经处理了这些。 另外,用户可能想知道不同环境配置文件的加载情况,比如application-dev.properties或application-test.properties,这时候可以通过设置spring.profiles.active来激活不同的环境,但这个问题用户没有直接问,可能暂时不需要展开,不过可以作为相关问题提出。 还需要考虑是否需要引入其他依赖,但一般情况下,Spring Boot的starter已经包含了必要的依赖,所以示例代码应该可以直接运行。 最后,要确保代码示例的正确性,比如是否正确使用@Autowired,是否在Spring管理的Bean中使用Environment,以及是否正确调用getProperty方法。同时,要引用用户提供的参考资料,特别是引用[2]提到使用Environment读取属性,引用[3]关于不同环境配置文件的加载,以及引用[5]的官方文档链接。 可能还需要提醒用户关于属性名称的正确拼写,以及如何处理默认值的情况,比如使用getProperty的第二个参数设置默认值,避免返回null导致的问题。这些细节可以增强示例的健壮性。 总结一下,回答的结构应该是先介绍Environment类的作用,然后分步骤给出示例代码,包括配置文件的编写、注入Environment和使用它读取属性,最后补充注意事项和相关问题。同时,引用提供的参考资料来支持回答的正确性。</think>### 通过 Environment 类读取 application.properties 配置项的步骤 #### 1. 添加配置项到 application.properties 在 `src/main/resources/application.properties` 中添加测试配置项: ```properties # 自定义配置示例 custom.message=Hello from Environment server.port=8080 ``` --- #### 2. 在代码中注入 Environment 对象 通过 `@Autowired` 注入 `Environment` 类,调用 `getProperty()` 方法读取配置: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigController { @Autowired private Environment env; @GetMapping("/getConfig") public String readConfig() { // 读取配置项(支持默认值) String message = env.getProperty("custom.message", "Default Message"); String port = env.getProperty("server.port"); return "Message: " + message + ", Port: " + port; } } ``` --- #### 3. 运行并验证 启动 Spring Boot 应用后,访问 `http://localhost:8080/getConfig`,输出结果应为: ``` Message: Hello from Environment, Port: 8080 ``` --- ### 关键注意事项 1. **Environment 的适用范围** - `Environment` 可直接读取 `application.properties`、系统环境变量、命令行参数等[^2][^3]。 - 若属性不存在,`getProperty()` 返回 `null`,建议添加默认值(如 `env.getProperty("key", "default")`)。 2. **配置文件优先级** - 若存在多环境配置(如 `application-dev.properties`),需通过 `spring.profiles.active=dev` 激活。 3. **属性命名规范** - 推荐使用 `kebab-case`(短横线分隔),如 `custom.message`,而非 `customMessage`[^5]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值