Qas*_*sim 265 java properties-file spring-boot
我想访问提供的值application.properties,例如:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
我想userBucket.path在Spring Boot应用程序中访问我的主程序.
Mas*_*ave 388
您可以使用@Value注释并在您使用的任何Spring bean中访问该属性
@Value("${userBucket.path}")
private String userBucketPath;
Spring Boot文档的Externalized Configuration部分解释了您可能需要的所有细节.
Rod*_*yas 190
另一种方法是向您的bean注入Environment.
@Autowired
private Environment env;
....
public void method() {
    .....  
    String path = env.getProperty("userBucket.path");
    .....
}
Job*_*eph 23
@ConfigurationProperties可用于将值.properties(.yml也支持)映射到POJO.
请考虑以下示例文件.
的.properties
cust.data.employee.name=Sachin
cust.data.employee.dept=Cricket
Employee.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "cust.data.employee")
@Configuration("employeeProperties")
public class Employee {
    private String name;
    private String dept;
    //Getters and Setters go here
}
现在可以通过自动装配访问属性值employeeProperties,如下所示.
@Autowired
private Employee employeeProperties;
public void method() {
   String employeeName = employeeProperties.getName();
   String employeeDept = employeeProperties.getDept();
}
Dhw*_*tel 18
目前,我知道以下三种方式:
1.@Value注释
    @Value("${<property.name>}")
    private static final <datatype> PROPERTY_NAME;
null. 例如,当您尝试在preConstruct()方法或init()方法中设置它时。发生这种情况是因为值注入发生在类完全构造之后。这就是为什么最好使用第三个选项的原因。2.@PropertySource注释
@PropertySource("classpath:application.properties")
//env is an Environment variable
env.getProperty(configKey);
PropertySouceEnvironment加载类时,将属性源文件中的值设置在变量(在您的类中)中。因此,您可以轻松获取后记。3.@ConfigurationProperties注释。
这主要用于 Spring 项目中加载配置属性。
它根据属性数据初始化一个实体。
@ConfigurationProperties 标识要加载的属性文件。
@Configuration 根据配置文件变量创建一个 bean。
@ConfigurationProperties(prefix = "user")
  @Configuration("用户数据")
  类用户{
    //属性和它们的getter/setter
  }
  @自动连线
  私有用户数据用户数据;
  userData.getPropertyName();
luc*_*fer 12
你也可以用这种方式......
@Component
@PropertySource("classpath:application.properties")
public class ConfigProperties {
    @Autowired
    private Environment env;
    public String getConfigValue(String configKey){
        return env.getProperty(configKey);
    }
}
然后,无论您想从 application.properties 读取的任何内容,只需将键传递给 getConfigValue 方法即可。
@Autowired
ConfigProperties configProp;
// Read server.port from app.prop
String portNumber = configProp.getConfigValue("server.port"); 
小智 7
按着这些次序。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.
}
要从属性文件中选择值,我们可以有一个Config reader 类,例如ApplicationConfigReader.java。然后根据属性定义所有变量。参考下面的例子,
application.properties
myapp.nationality: INDIAN
myapp.gender: Male
下面是相应的读者类。
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "myapp")
class AppConfigReader{
    private String nationality;
    private String gender
    // Getter and setter
}
现在,我们可以在任何想要访问属性值的地方自动连接读取器类。
例如,
@Service
class ServiceImpl{
    @Autowired
    private AppConfigReader appConfigReader;
    //...
    // Fetching values from the configuration reader
    String nationality = appConfigReader.getNationality() ;
    String gender = appConfigReader.getGender();
}
小智 7
你应该@Autowired private Environment env;从import org.springframework.core.env.Environment;
然后这样使用它:
env.getProperty("yourPropertyNameInApplication.properties")
应用程序可以从application.properties文件中读取三种类型的值。
my.name = kelly
my.dbConnection = {connection_srting:'http://localhost:...', username:'benz', password:'pwd'}
@Value("${my.name}")
private String name;
@Value("#{${my.dbConnection}}")
private Map<String,String> dbValues;
如果application.properties中没有属性,则可以使用默认值:
@Value("${your_name: default value}")
private String msg;
小智 6
@Value Spring注释用于将值注入到Spring管理的bean中的字段中,它可以应用于字段或构造函数/方法参数级别。
\n例子
\n    @Value("string value identifire in property file")\n    private String stringValue;\n我们还可以使用@Value注解来注入Map property.
\n首先,我们需要在中定义属性{key: \xe2\x80\x98value\' } form in our properties file:
   valuesMap={key1: \'1\', key2: \'2\', key3: \'3\'}\n并不是说映射中的值必须用单引号引起来。
\n现在从属性文件中将此值作为映射注入:
\n   @Value("#{${valuesMap}}")\n   private Map<String, Integer> valuesMap;\n获取特定键的值
\n   @Value("#{${valuesMap}.key1}")\n   private Integer valuesMapKey1;\n   @Value("#{\'${listOfValues}\'.split(\',\')}")\n   private List<String> valuesList;\n有3种阅读方式application.properties:
第一种方式:
使用@Value,EnvironmentInterface和@ConfigurationProperties:
@Value("${userBucket.path}")
private String value;
第二种方式:
@Autowired
private Environment environment; // org.springframework.core.env.Environment;
String s = environment.getProperty("userBucket.path");
第三种方式:
@Value("${userBucket.path}")
private String value;
可以用 getter 和 setter 来读取..
参考-这里
如果您将在一个地方使用这个值,您可以使用@Value来加载变量application.properties,但是如果您需要一种更集中的方式来加载这个变量@ConfigurationProperties是一种更好的方法。
此外,如果您需要不同的数据类型来执行验证和业务逻辑,您可以加载变量并自动转换。
application.properties
custom-app.enable-mocks = false
@Value("${custom-app.enable-mocks}")
private boolean enableMocks;
小智 5
@Value("${property-name}")如果您的类用@Configuration或注释,您可以从 application.properties
使用@Component。
我尝试过的另一种方法是创建一个 Utility 类以通过以下方式读取属性 -
 protected PropertiesUtility () throws IOException {
    properties = new Properties();
    InputStream inputStream = 
   getClass().getClassLoader().getResourceAsStream("application.properties");
    properties.load(inputStream);
}
您可以使用静态方法来获取作为参数传递的键的值。
| 归档时间: | 
 | 
| 查看次数: | 305532 次 | 
| 最近记录: |