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

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

userBucket.path=${HOME}/bucket

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


当前回答

阅读应用程序。属性或应用程序。Yml属性遵循以下步骤:

在应用程序中添加属性。属性或application.yaml 创建配置类并添加属性

application.jwt.secretKey=value
application.jwt.tokenPrefix=value
application.jwt.tokenExpiresAfterDays=value ## 14
application:
  jwt:
    secret-key: value
    token-prefix: value
    token-expires-after-days: value ## 14
@Configuration("jwtProperties") // you can leave it empty
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "application.jwt") // prefix is required
public class JwtConfig {
    private String secretKey;
    private String tokenPrefix;
    private int tokenExpiresAfterDays;

    // getters and setters
}

注意:在.yaml文件中,你必须使用kabab-case

现在使用配置类实例化它,你可以手动或依赖注入。

public class Target {

  private final JwtConfig jwtConfig;

  @Autowired
  public Target(JwtConfig jwtConfig) {
      this.jwtConfig = jwtConfig;
  }

  // jwtConfig.getSecretKey()

}

其他回答

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

Spring-boot允许我们使用几种方法来提供外部化配置,您可以尝试使用application。Yml或yaml文件代替属性文件,并根据不同的环境提供不同的属性文件设置。

我们可以将每个环境的属性分离到单独的spring概要文件下的单独的yml文件中。然后在部署期间,您可以使用:

java -jar -Drun.profiles=SpringProfileName

指定要使用哪个弹簧概要文件。注意,yml文件的名称应该类似

application-{environmentName}.yml

让他们自动被新兵带走。

参考资料:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

从申请中读取。Yml或属性文件:

从属性文件或yml中读取值的最简单方法是使用spring @value注释。Spring会自动将yml中的所有值加载到Spring环境中,所以我们可以直接从环境中使用这些值,比如:

@Component
public class MySampleBean {

@Value("${name}")
private String sampleName;

// ...

}

或者spring提供的另一个读取强类型bean的方法如下:

YML

ymca:
    remote-address: 192.168.1.1
    security:
        username: admin

对应的POJO读取yml:

@ConfigurationProperties("ymca")
public class YmcaProperties {
    private InetAddress remoteAddress;
    private final Security security = new Security();
    public boolean isEnabled() { ... }
    public void setEnabled(boolean enabled) { ... }
    public InetAddress getRemoteAddress() { ... }
    public void setRemoteAddress(InetAddress remoteAddress) { ... }
    public Security getSecurity() { ... }
    public static class Security {
        private String username;
        private String password;
        public String getUsername() { ... }
        public void setUsername(String username) { ... }
        public String getPassword() { ... }
        public void setPassword(String password) { ... }
    }
}

上述方法适用于yml文件。

参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

你也可以这样做....

@Component
@PropertySource("classpath:application.properties")
public class ConfigProperties {

    @Autowired
    private Environment env;

    public String getConfigValue(String configKey){
        return env.getProperty(configKey);
    }
}

然后无论你想从应用程序中读取什么。属性,只需将键传递给getConfigValue方法。

@Autowired
ConfigProperties configProp;

// Read server.port from app.prop
String portNumber = configProp.getConfigValue("server.port"); 

实际上有三种方法来读取应用程序。属性文件,

使用环境,

@Autowired
Environment environment

environment.getProperty({propertyName})

或者使用@Value,

@Value("${property}")

但是@Value的问题是,如果值不在属性文件中,它可能会抛出异常

建议使用@ConfigurationProperties

@ConfigurationProperties("userBucket")
public class test{
  private String path;
  //getters and setters
}

详细示例-读取application.properties

1.Injecting a property with the @Value annotation is straightforward:
@Value( "${jdbc.url}" )
private String jdbcUrl;

2. we can obtain the value of a property using the Environment API

@Autowired
private Environment env;
...
dataSource.setUrl(env.getProperty("jdbc.url"));