如何在我的自定义@ConfigurationProperties中的内部类型中@Autowire?

问题描述 投票:0回答:2

假设我创建自己的自定义配置属性:

@ConfigurationProperties("a.b")
public record Custom(Topic topics) {
    public record Topics(String topicA, String topicB) {}
}

我可以在属性文件中提供我的属性,并且我可以使用

@Autowire
类型来
Custom
它们。

但是如果我尝试

@Autowire
我的
Topics
它就会失败。

能够

@Autowire
这样的嵌套配置属性类型的最佳方法是什么?

spring spring-boot properties-file
2个回答
0
投票

您可以通过为嵌套配置声明一个 bean 来实现这一点。 bean 将接收基本属性作为参数,然后从那里提取所需的内部记录:

@Configuration
@EnableConfigurationProperties(Custom.class)
public class TopicsConfiguration {

  @Bean
  public Topics topics(Custom custom) {
    return custom.topics();
  }

}

0
投票

在 Spring Boot 中,您可以使用

@ConfigurationProperties
将外部配置属性绑定到 Java 对象。如果您想在自定义
@Autowired
类的内部类型中使用
@ConfigurationProperties
,只要内部类型是 Spring 管理的组件(例如,
@Component
@Service
等),就可以这样做。 )。以下是有关如何实现此目标的分步指南:

  1. 创建一个带有内部组件的自定义
    @ConfigurationProperties
    类:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@ConfigurationProperties(prefix = "myconfig")
public class MyConfigurationProperties {

    private String property1;
    private InnerType innerType = new InnerType();

    public String getProperty1() {
        return property1;
    }

    public void setProperty1(String property1) {
        this.property1 = property1;
    }

    public InnerType getInnerType() {
        return innerType;
    }

    public void setInnerType(InnerType innerType) {
        this.innerType = innerType;
    }

    @Component
    public static class InnerType {
        private String innerProperty;

        public String getInnerProperty() {
            return innerProperty;
        }

        public void setInnerProperty(String innerProperty) {
            this.innerProperty = innerProperty;
        }
    }
}

在这个例子中,

MyConfigurationProperties
是一个
@ConfigurationProperties
类,它包含一个内部组件类
InnerType
,它用
@Component
注释。

  1. 创建
    application.properties
    application.yml
    文件来定义您的配置属性:
myconfig.property1=PropertyValue
myconfig.inner-type.inner-property=InnerPropertyValue
  1. 使用
    @Autowired
    将内部组件 (
    InnerType
    ) 注入到 Spring beans 中:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {

    private final MyConfigurationProperties.InnerType innerType;

    @Autowired
    public MyService(MyConfigurationProperties.InnerType innerType) {
        this.innerType = innerType;
    }

    public String getInnerProperty() {
        return innerType.getInnerProperty();
    }
}

在此示例中,

MyService
使用
@Autowired
注入
MyConfigurationProperties.InnerType
,这是
MyConfigurationProperties
中定义的Spring组件。

  1. 现在,您可以在应用程序中使用
    MyService
    来访问属性:
@Service
public class AnotherService {

    private final MyService myService;

    @Autowired
    public AnotherService(MyService myService) {
        this.myService = myService;
    }

    public String getInnerPropertyFromMyService() {
        return myService.getInnerProperty();
    }
}

通过执行以下步骤,您可以在自定义

@Autowired
类的内部类型 (
InnerType
) 中使用
@ConfigurationProperties
,并在其他 Spring 管理的组件中访问其属性。

© www.soinside.com 2019 - 2024. All rights reserved.