application.properties
file 中快速获取 import org.springframework.stereotype.*; import org.springframework.beans.factory.annotation.*; @Component public class MyBean { @Value("${name}") private String name; // ... }
package com.example; import java.net.InetAddress; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties("acme") public class AcmeProperties { private boolean enabled; 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; private List<String> roles = new ArrayList<>(Collections.singleton("USER")); public String getUsername() { ... } public void setUsername(String username) { ... } public String getPassword() { ... } public void setPassword(String password) { ... } public List<String> getRoles() { ... } public void setRoles(List<String> roles) { ... } } }
package com.example; import java.net.InetAddress; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConstructorBinding; import org.springframework.boot.context.properties.DefaultValue; @ConstructorBinding @ConfigurationProperties("acme") public class AcmeProperties { private final boolean enabled; private final InetAddress remoteAddress; private final Security security; public AcmeProperties(boolean enabled, InetAddress remoteAddress, Security security) { this.enabled = enabled; this.remoteAddress = remoteAddress; this.security = security; } public boolean isEnabled() { ... } public InetAddress getRemoteAddress() { ... } public Security getSecurity() { ... } public static class Security { private final String username; private final String password; private final List<String> roles; public Security(String username, String password, @DefaultValue("USER") List<String> roles) { this.username = username; this.password = password; this.roles = roles; } public String getUsername() { ... } public String getPassword() { ... } public List<String> getRoles() { ... } } }
可以使用@DefaultValue指定默认值。
要使用构造函数绑定,必须使用@EnableConfigurationProperties或配置属性扫描启用该类。您不能使用构造函数绑定由常规Spring机制创建的bean(例如@Component bean、通过@Bean方法创建的bean或使用@Import加载的bean)
@ConfigurationProperties
-annotated types Spring Boot提供了绑定@ConfigurationProperties类型并将它们注册为bean的基础设施。您可以逐个类地启用配置属性,也可以启用与组件扫描工作方式类似的配置属性扫描。
有时,用@ConfigurationProperties注释的类可能不适合扫描,
@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(AcmeProperties.class) public class MyConfiguration { } @SpringBootApplication @ConfigurationPropertiesScan({ "com.example.app", "org.acme.another" }) public class MyApplication { }
@ConfigurationProperties
-annotated types 要使用@ConfigurationProperties bean,您可以以与任何其他bean相同的方式注入它们,如下面的示例所示:
@Service public class MyService { private final AcmeProperties properties; @Autowired public MyService(AcmeProperties properties) { this.properties = properties; } //... @PostConstruct public void openConnection() { Server server = new Server(this.properties.getRemoteAddress()); // ... } }
Externalized Configuration