I have a list of gaming rooms which is created by Spring. Each rooms corresponds some rules (which is enum), and the code is:
@Bean
List<Room> rooms() {
return Arrays.stream(Rules.values())
.map(rule -> new Room(rule))
.collect(Collectors.toList());
}
But now I need the rooms to be @Beans
too: I want Spring to process @EventListener
annotation in them. However, I don't want to declare them manually in config as the Rules
enum can be updated in the future. How can I solve this problem? Thanks.
It can be accomplished by invoking another method which is marked with @Bean
i.e. creates Room
bean as shown below
@Bean
List<Room> rooms() {
return Arrays.stream(Rules.values())
.map(rule -> room(rule))
.collect(Collectors.toList());
}
@Bean
Room room(Rule rule) {
return new Room(rule);
}
This should suffice without need of @EventListener
.
Let know in comments if any more information is required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With