How to avoid JMX bean getting same autowired value from the constructor injection in Spring Boot?

14 views Asked by At

I am trying to register a JMX bean in my Spring Boot application. This same class is using constructor autowiring to autowire another bean. The issue that I'm facing is that when the addBrandToList method is called during startup, the notBrandId variable is still getting the ${brandsForMetrics} value from constructor injection. Is there a way to avoid this?

    @Slf4j
    @Endpoint(id="metric-endpoint")
    @Configuration
    @EnableScheduling
    public class Tasks {

    private Set<String> brandsSet = new HashSet<>();
    private MyService service;

    @Autowired
    public void Tasks(@Value("${brandsForMetrics}") String brands, MyService service) {
        this.service = service;
        if (!StringUtils.isEmpty(brands)) {
            brandsSet = Arrays.stream(brands.split(","))
                    .map(String::trim)
                    .collect(Collectors.toSet());
        }
    }

    @WriteOperation
    @Bean
    public String addBrandToList(String notBrandId) {
        brandsSet.add(notBrandId);
        return Arrays.toString(brandsSet.toArray());
    }
}
0

There are 0 answers