我想访问应用程序中提供的值。属性,例如:

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log

userBucket.path=${HOME}/bucket

我想访问userBucket。在Spring Boot应用程序的主程序中的路径。


当前回答

属性中的@Value("${property-name}") 应用程序。属性 @配置或@组件。

我尝试的另一种方法是用下面的方式创建一个Utility类来读取属性

 protected PropertiesUtility () throws IOException {
    properties = new Properties();
    InputStream inputStream = 
   getClass().getClassLoader().getResourceAsStream("application.properties");
    properties.load(inputStream);
}

您可以使用静态方法来获取作为参数传递的键的值。

其他回答

目前, 我知道以下三种方法:

1. @Value注释

    @Value("${<property.name>}")
    private static final <datatype> PROPERTY_NAME;

根据我的经验,有些情况下你并不是 能够获取该值或将其设置为null。 例如, 当你尝试在preConstruct()方法或init()方法中设置它时。 这是因为值注入发生在类完全构造之后。 这就是为什么最好使用3'选项的原因。

2. @PropertySource注释

@PropertySource("classpath:application.properties")

//env is an Environment variable
env.getProperty(configKey);

当装入类时,PropertySouce从环境变量中的属性源文件(在您的类中)设置值。 所以你可以很容易地获取后记。 可通过系统环境变量访问。

3.@ConfigurationProperties注释。

这主要用于Spring项目中加载配置属性。 它根据属性数据初始化实体。 @ConfigurationProperties标识要加载的属性文件。 @Configuration基于配置文件变量创建bean。 @ConfigurationProperties(prefix = "user") @ configuration(“用户数据”) 类用户{ //属性及其getter / setter } @ autowired private UserData; userData.getPropertyName ();

遵循以下步骤。 1:-创建你的配置类如下所示

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;

@Configuration
public class YourConfiguration{

    // passing the key which you set in application.properties
    @Value("${userBucket.path}")
    private String userBucket;

   // getting the value from that key which you set in application.properties
    @Bean
    public String getUserBucketPath() {
        return userBucket;
    }
}

2:-当你有一个配置类时,然后从你需要的配置中注入变量。

@Component
public class YourService {

    @Autowired
    private String getUserBucketPath;

    // now you have a value in getUserBucketPath varibale automatically.
}

您可以使用@Value注释并访问spring bean中的属性

@Value("${userBucket.path}")
private String userBucketPath;

从应用程序中获取价值有两种方法。属性文件

使用@Value注释

    @Value("${property-name}")
    private data_type var_name;

使用环境类的实例

@Autowired
private Environment environment;

//access this way in the method where it's required

data_type var_name = environment.getProperty("property-name");

您还可以使用构造函数注入或自己创建bean来注入环境实例

为了从属性文件中选择值,我们可以有一个配置读取器类,比如ApplicationConfigReader.java 然后根据属性定义所有变量。参考下面的例子,

application.properties

myapp.nationality: INDIAN
myapp.gender: Male

下面是相应的阅读类。

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "myapp") 
class AppConfigReader{
    private String nationality;
    private String gender

    //getter & setter
}

现在,我们可以在任何想要访问属性值的地方自动连接阅读器类。 如。

@Service
class ServiceImpl{
    @Autowired
    private AppConfigReader appConfigReader;

    //...
    //fetching values from config reader
    String nationality = appConfigReader.getNationality() ; 
    String gender = appConfigReader.getGender(); 
}