java 直接获取application.properties值

试了很多种方法,@VALUE,ENVIROMENT,都不行,最后发现用这种可以

在Java中,你可以使用java.util.Properties类来直接获取application.properties文件中的值。以下是一个简单的例子,演示如何读取application.properties文件中的属性:

首先,确保你的application.properties文件在类路径下,例如在src/main/resources目录中。

然后,使用java.util.Properties类加载并读取这个文件:

import java.io.InputStream;
import java.util.Properties;
 
public class ApplicationProperties {
 
    private static final Properties properties = new Properties();
 
    static {
        try {
            InputStream inputStream = ApplicationProperties.class.getClassLoader().getResourceAsStream("application.properties");
            if (inputStream != null) {
                properties.load(inputStream);
            } else {
                throw new RuntimeException("Could not find application.properties");
            }
        } catch (Exception e) {
            throw new RuntimeException("Error loading application.properties", e);
        }
    }
 
    public static String getProperty(String key) {
        return properties.getProperty(key);
    }
}

使用这个类,你可以通过调用getProperty方法来获取属性值:

public class Main {
    public static void main(String[] args) {
        String someProperty = ApplicationProperties.getProperty("some.property.key");
        System.out.println(someProperty);
    }
}

确保你的application.properties文件中包含了你想要获取的属性,例如:

some.property.key=someValue

当你运行Main类的main方法时,它将输出application.properties中对应some.property.key的值。

Java中,有多种方法可以使用`application.properties`里的: ### 使用`java.util.Properties`类 可以使用`java.util.Properties`类直接获取`application.properties`文件中的。示例代码如下: ```java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class PropertiesExample { public static void main(String[] args) { Properties properties = new Properties(); try { // 加载application.properties文件 properties.load(new FileInputStream("src/main/resources/application.properties")); // 获取指定属性的 String value = properties.getProperty("property.name"); System.out.println(value); } catch (IOException e) { e.printStackTrace(); } } } ``` ### 使用`@Value`注解 在Spring框架中,可以使用`@Value`注解注入`application.properties`中的。示例代码如下: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyComponent { @Value("${property.name}") private String propertyValue; public String getPropertyValue() { return propertyValue; } } ``` 以下是一个完整的使用示例,包含主类: ```java package com.alibaba.surge.hsf.provider; import com.alibaba.boot.hsf.annotation.HSFProvider; import com.alibaba.surge.hsf.EnvService; import org.springframework.beans.factory.annotation.Value; /** * @author zhenyuan.he */ @HSFProvider(serviceInterface = EnvService.class) public class EnvServiceImpl implements EnvService { @Value("${spring.profiles.active}") private String currentEnv; @Override public String getCurrentEnv() { return currentEnv; } } ``` ### 在拦截器中使用 在Spring Boot项目的拦截器中使用`application.properties`配置的,可实现`InterceptorProperties.java`来获取。例如: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class InterceptorProperties { @Value("${property.name}") private String propertyValue; public String getPropertyValue() { return propertyValue; } } ``` 然后在拦截器中注入该组件使用其
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值