2

SpringBoot app fails while trying to initialize this class:

@Component
public class Weather {

private Map<Integer,Double> maxRainyDays;
private Map<Integer,  WeatherDays> totalDays;

public Weather(Map<Integer, WeatherDays> totalDays, Map<Integer,Double> maxRainyDays){

    this.setMaxRainyDays(maxRainyDays);
    this.setTotalDays(totalDays);

}

The error:

Parameter 0 of constructor in SolarSystem.Models.Weather required a bean of type 'SolarSystem.Utilities.WeatherDays' that could not be found.

The mentioned bean is already defined (in the same basepackage):

public enum WeatherDays {

RAINY,
MILD,
DRY,
MAX_RAIN}

Workaround:

When I changed to Weather() constructor I solved the issue.Of course I had to use setters to set the properties of the object.

But I need to understand the reasons why happened

7
  • This class doesn't look like it should be a bean (service object); instead, it looks like it's a data object. Commented Jan 10, 2019 at 0:28
  • Question, why do you consider to inject Weather as component? Commented Jan 10, 2019 at 2:54
  • @JonathanJohx because I'm autowiring into other Service in the package Commented Jan 10, 2019 at 2:59
  • Ok let me explain about it, first when you inject a Class in this context Weather as dependency then you need to pass an empty construct or a construct with parameters which they need to be injected as beans because when Spring scans all components, they should have an empty construct or parameters injected. Example public Weather (@Autowored Service service) {... } when a class is injected as component is more or less like a singleton class.. Commented Jan 10, 2019 at 3:11
  • 1
    Yes, you need it. For that you need to know if it's necessary creating a component called Weather or perhaps you need to create several instances from this Weather class. Commented Jan 10, 2019 at 3:36

1 Answer 1

2

Because the collections (Map here) you are injecting via constructor parameter aren't registered as Spring beans. This is why you would annotate class with @service, @repository, etc and have them autowired into other classes. To fix, you can set up a config class like such:

@Configuration
public class BeanConfig {

    @Bean
    public Map<Integer, WeatherDays> totalDays() {
        Map<Integer, WeatherDays> map = new HashMap<>();
        map.put(1, WeatherDays.DRY);
        return map;
    }

    @Bean
    public Map<Integer, Double> maxRainyDays() {
        Map<Integer, Double> map = new HashMap<>();
        map.put(1, 0.2);
        return map;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Good to know! So this is how you would normally inject POJOs or simple DTOs in your spring services? Thanks
Not quite, if you look at chrylis's comment. is your weather class meant to contain stateful data and instantiated many times? if so, it shouldn't be a bean and instead a service class (as a bean) should be used to set its state and create many Weather objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.