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

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

userBucket.path=${HOME}/bucket

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


当前回答

也许它可以帮助其他人: 你应该注入@Autowired私有环境env;from import org.springframework.core. environment;

然后这样使用它: env.getProperty(“yourPropertyNameInApplication.properties”)

其他回答

为了从属性文件中选择值,我们可以有一个配置读取器类,比如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(); 
}

应用程序。Yml或application.properties

config.value1: 10
config.value2: 20
config.str: This is a simle str

MyConfig类

@Configuration
@ConfigurationProperties(prefix = "config")
public class MyConfig {
    int value1;
    int value2;
    String str;

    public int getValue1() {
        return value1;
    }

    // Add the rest of getters here...      
    // Values are already mapped in this class. You can access them via getters.
}

任何想要访问配置值的类

@Import(MyConfig.class)
class MyClass {
    private MyConfig myConfig;

    @Autowired
    public MyClass(MyConfig myConfig) {
        this.myConfig = myConfig;
        System.out.println( myConfig.getValue1() );
    }
}

另一种在配置中找到键/值的方法。

...
import org.springframework.core.env.ConfigurableEnvironment;
...
@SpringBootApplication
public class MyApplication {

    @Autowired
    private ConfigurableEnvironment  myEnv;

...
  
    @EventListener(ApplicationReadyEvent.class)
    public void doSomethingAfterStartup() 
    throws Exception {
        
        LOG.info("myEnv (userBucket.path): " + myEnv.getProperty("userBucket.path"));
    }
} 

@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();

}

另一种方法是将org.springframework.core. environment注入到bean中。

@Autowired
private Environment env;
....

public void method() {
    .....  
    String path = env.getProperty("userBucket.path");
    .....
}