使用 @ConfigurationProperties 注入配置信息之一

上一篇博客(使用@Value注入配置信息)的结尾我们说了,对于属性特别多的bean以及bean的属性之间有层次继承关系的情况,不太适合使用@Value来注入配置信息,为此,spring boot 为我们提供了 @ConfigurationProperties 这个注解来解决这个问题。下面来看一下这个注解的用法:

  • 首先看配置文件
# ConfigurationProperties 学习,见 spring boot 文档第 24.7小节
foo.enabled=false
foo.remote-address=192.168.1.1
foo.security.username=user
foo.security.password=pwd
foo.security.roles=USER,ADMIN
  • 对应的属性配置类
/**
 * 在这种绑定中,getter 和 setter 方法是强制的,因为这里的绑定是通过标准的Java Bean属性绑定,但是也有例外,这里不讲,在POJO中,getter 和 setter 方法一般都会写上的吧。具体见 spring boot 文档第 24.7小节
 */
@ConfigurationProperties(prefix = "foo")
public class FooProperties {

    private boolean enabled;

    private InetAddress remoteAddress;

    private Security security;

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public InetAddress getRemoteAddress() {
        return remoteAddress;
    }

    public void setRemoteAddress(InetAddress remoteAddress) {
        this.remoteAddress = remoteAddress;
    }

    public Security getSecurity() {
        return security;
    }

    public void setSecurity(Security security) {
        this.security = security;
    }

    public static class Security {

        private String username;

        private String password;

        private List<String> roles = new ArrayList<>(Collections.singleton("USER"));

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public List<String> getRoles() {
            return roles;
        }

        public void setRoles(List<String> roles) {
            this.roles = roles;
        }

        @Override
        public String toString() {
            return "{ " + "username='" + username + '\'' + ", password='" + password + '\'' + " }";
        }
    }

    @Override
    public String toString() {
        return "{ " + "enabled=" + enabled + ", remoteAddress=" + remoteAddress + ", security="
                + security + " }";
    }
}

注意:我们在上面定义的属性配置类上使用了@ConfigurationProperties注解,并指定了前缀,该前缀对应的就是配置文件中配置信息的前缀。还要注意的就是属性配置类的属性名和配置文件中配置信息是对应的(这里的对应并不是要求一模一样,只要能转换成功,就算对应,比如下划线语法、驼峰语法等都能接受)。

  • 下一步,我们还需要把上面的属性配置类注册到@EnableConfigurationProperties里边

/**
 * 结合 {@link ConfigurationProperties} 注入了一个 FooProperties bean, 这 bean 的全名是 foo-com.info.maka.waimai.config.tutorial.environment.properties.FooProperties (属性名前缀-属性类的完全限定名)
 */
@Configuration
@EnableConfigurationProperties(FooProperties.class)
public class TestPropertiesConfiguration {

}
  • 到这里,属性配置类以及可以实例化 bean 了,我们就可以在系统中使用这个bean了
@Service
public class FooServiceImpl implements FooService {

    private static final Logger logger = LoggerFactory.getLogger(FooService.class);

    private FooProperties fooProperties;

    @Autowired
    public FooServiceImpl(FooProperties fooProperties) {
        this.fooProperties = fooProperties;
    }

    /**
     * 这个注解在这里没实际用处,就是为了方便在该类构造完成后打印日志,看看配置信息是否加载到配置类中了
     */
    @PostConstruct
    public void verifyConfigurationProperties() {
        logger.info("FooProperties: {}", fooProperties);
    }
}
  • 查看日志,验证一下,看看配置文件中的配置信息是否加载到bean中了
    查看配置信息是否加载到配置类中

本文先到这里,但是关于@ConfigurationProperties的知识,还没有说完,下一篇博客还会继续讲。使用 @ConfigurationProperties 注入配置信息之二


版权声明:本文为qq_17586821原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。