Java 注入SpringBoot自定义配置属性列表

Java 注入SpringBoot自定义配置属性列表,java,spring,spring-boot,spring-annotations,lombok,Java,Spring,Spring Boot,Spring Annotations,Lombok,我试图将在application.Properties文件中创建的自定义配置属性列表注入控制器。我得到一个java.lang.IllegalArgumentException:无法解析值“{${custom.resource}}”中的占位符“custom.resource”我还尝试按如下方式注入它:@value(${custom.resource}” 这是我的ConfigurationProperty类: 导入org.springframework.boot.context.properties

我试图将在application.Properties文件中创建的自定义配置属性列表注入控制器。我得到一个
java.lang.IllegalArgumentException:无法解析值“{${custom.resource}}”中的占位符“custom.resource”
我还尝试按如下方式注入它:
@value(${custom.resource}”

这是我的ConfigurationProperty类: 导入org.springframework.boot.context.properties.ConfigurationProperties; 导入org.springframework.context.annotation.Configuration

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "custom")
public class ConfigProperties {

    @Data
    public static class Resource {

        private String description;

        private String category;

        private String URL;

        private Boolean PDF;
    }
}
import lombok.Data;

@Configuration
@ConfigurationProperties("custom")
@Data
public class ConfigProperties {

    private List<Resource> resourceList;

    @Data
    public static class Resource {

        private String description;

        private String category;

        public String url;

        public Boolean pdf;
    }
}
我的getter和setter方法是由Lombok生成的

以下是我如何布置我的财产:

custom.resource.description[6]=Description
custom.resource.category[6]=the-category
custom.resource.url[6]=somelink.com
custom.resource.pdf[6]=false

custom.resource.description[7]=Description
custom.resource.category[7]=the-category
custom.resource.url[7]=somelink.com
custom.resource.pdf[7]=false

我不确定我所做的是不是根本不可能,是否还有其他方法,或者我应该做什么。任何指导都是很好的

请按照以下方式更正您的课堂:

@Getter
@Component
@ConfigurationProperties(prefix = "custom")
public class ConfigProperties {

    private final Resource resource = new Resource();

    @Getter
    @Setter
    public static class Resource {

        private List<String> description = new ArrayList<>();
        private List<String> category = new ArrayList<>();
        private List<String> URL = new ArrayList<>();
        private List<Boolean> PDF = new ArrayList<>();
    }
}
@Getter
@组成部分
@配置属性(前缀=“自定义”)
公共类配置属性{
私有最终资源=新资源();
@吸气剂
@塞特
公共静态类资源{
私有列表描述=新建ArrayList();
私有列表类别=新的ArrayList();
私有列表URL=new ArrayList();
私有列表PDF=新的ArrayList();
}
}

.

为了解决这个问题,我需要向ConfigProperty类添加一个ResourceList,并使用spring EL在
@Value
中通过bean名调用该列表

import java.util.List;
导入org.springframework.boot.context.properties.ConfigurationProperties; 导入org.springframework.context.annotation.Configuration

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "custom")
public class ConfigProperties {

    @Data
    public static class Resource {

        private String description;

        private String category;

        private String URL;

        private Boolean PDF;
    }
}
import lombok.Data;

@Configuration
@ConfigurationProperties("custom")
@Data
public class ConfigProperties {

    private List<Resource> resourceList;

    @Data
    public static class Resource {

        private String description;

        private String category;

        public String url;

        public Boolean pdf;
    }
}
导入lombok.数据;
@配置
@配置属性(“自定义”)
@资料
公共类配置属性{
私有列表资源列表;
@资料
公共静态类资源{
私有字符串描述;
私有字符串类别;
公共字符串url;
公共布尔值pdf;
}
}
@Value(“#{configProperties.resourceList}”)
包含在我的控制器类中


对于任何想知道我是通过在测试中查看应用程序上下文bean名称来实现这一点的人来说,我使用了许多前缀来分隔mi配置属性,如(IMHO更优雅)

按照描述的官方文档。您需要将属性文件更改为:

custom.resource[6].description=Description
custom.resource[6].category=the-category
custom.resource[6].url=somelink.com
custom.resource[6].pdf=false

custom.resource[7].description=Description
custom.resource[7].category=the-category
custom.resource[7].url=somelink.com
custom.resource[7].pdf=false
您还需要创建一个POJO来管理信息,如下所示:

public class Resource {
    private String description;
    private String category;
    private String url
    private boolean pdf

    /**
    * Setters and getters
    */
}
最后添加类以正确管理配置属性:

@Configuration
@PropertySource("classpath:config.properties")
public class ConfigProperties {

    private final List<Resource> resources = new ArrayList<>();

    @Bean
    @ConfigurationProperties(prefix = "custom.resource")
    public List<Resource> resources() {
        return this.resources;
    }
}

private final resource=new resource();
上获取编译错误。
final,应为浮点值我不需要Configuration和ConfigurationProperties注释吗?我仍然得到相同的IllegalArgumentexception@ebraun99似乎你需要每个属性的列表-我在答案中添加了这个…关于异常-你如何读取属性?我正在使用一个值读取属性,我认为列表的正确语法是
@Value(${custom.resource}”)
但我一直在尝试这两种方法和
@Value({${custom.resource}}”)
但添加列表后仍然没有变化。为了澄清,每个资源都应该有一个属性。我想从application.properties文件中获取一个资源列表。我的方括号是否应该移到资源的右侧?@ebraun99如果您要使用
@Value
@Value(“${custom.resource}”)
-语法正确)那么您不必创建属性类-只需将它们添加到您的
应用程序中即可。属性
。类对于类型安全或验证参数非常有用。请仔细阅读该类-我相信您会解决问题。