Suggestion, don’t overcomplicate.
Say you want that list as an
Environmentvariable. You’d set it using
-Dtopics=topic-01,topic-02,topic-03
You then can recover it using the injected
EnvironmentBean, and create a
new
List<String>Bean
@Bean@Qualifier("topics")List<String> topics(final Environment environment) { final var topics = environment.getProperty("topics", ""); return Arrays.asList(topics.split(","));}From now on, that
Listcan be
@Autowired.
You can also consider creating your custom qualifier annotation, maybe
@Topics.
Then
@Serviceclass TopicService { @Topics @Autowired private List<String> topics; ...}Or even
@Serviceclass TopicService { private final List<String> topics; TopicService(@Topics final List<String> topics) { this.topics = topics; } ...}What you could do is use an externalized file.
Pass to the environment parameters the path to that file.
-DtopicsPath=C:/whatever/path/file.json
Than use the
EnvironmentBean to recover that path. Read the file content
and ask
Jacksonto deserialize it
You’d also need to create a simple
Topicclass
public class Topic { public String name; public String id;}Which represents an element of this
JSONarray
[ { "name": "topic-1", "id": "id-1" }, { "name": "topic-2", "id": "id-2" }]@BeanList<Topic> topics( final Environment environment, final ObjectMapper objectMapper) throws IOException { // Get the file path final var topicsPath = environment.getProperty("topicsPath"); if (topicsPath == null) { return Collections.emptyList(); } // Read the file content final var json = Files.readString(Paths.get(topicsPath)); // Convert the JSON to Java objects final var topics = objectMapper.readValue(json, Topic[].class); return Arrays.asList(topics);}


