Home > Blockchain >  Could not autowire. No beans of 'int' type found
Could not autowire. No beans of 'int' type found

Time:11-18

I try to read data from application properties file in Spring Boot Application.

Following Code is my main class.

@SpringBootApplication(scanBasePackages = "com.fsk.limitservice")
public class LimitServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(LimitServiceApplication.class, args);
    }
}

Following Code is My Controller Class

@RestController
public class LimitsConfigurationController {

    @Autowired
    LimitConfiguration limitConfiguration;

    @GetMapping("/limits")
    public LimitConfiguration retrieveLimitFromConfiguration() {
        return new LimitConfiguration(limitConfiguration.getMinimum(), limitConfiguration.getMaximum());
    }
}

Following Code is my component class

@Component
@ConfigurationProperties("limits-service")
public class LimitConfiguration {

    private int minimum;
    private int maximum;

    public LimitConfiguration(int minimum, int maximum) {
        this.minimum = minimum;
        this.maximum = maximum;
    }

    public int getMinimum() {
        return minimum;
    }

    public void setMinimum(int minimum) {
        this.minimum = minimum;
    }

    public int getMaximum() {
        return maximum;
    }

    public void setMaximum(int maximum) {
        this.maximum = maximum;
    }
}

And Lastly this is my application properties file

spring.application.name=limits-service
limits-service.minimum=17
limits-service.maximum=1124

When i click to run button, i get the following error. How can i fix this.?

enter image description here

CodePudding user response:

  1. add @ConfigurationPropertiesScan to LimitServiceApplication

  2. add @ConstructorBinding to LimitConfiguration and remove @Component

CodePudding user response:

If you remove the constructor, the exception would not be thrown, because the Spring framework uses setters to bind properties, so setters must be declared for each of the properties.

As of Spring 2.2, @ConfigurationProperties can be found via classpath scanning as an alternative to using @EnableConfigurationProperties or @Component. To enable scanning, @ConfigurationPropertiesScan should be added to your application class.

CodePudding user response:

You can remove the constructor and add the following annotations on the fields:

@Configuration
public class LimitConfiguration {

    @Value("${limits-service.minimum}")
    private int minimum;

    @Value("${limits-service.maximum}")
    private int maximum;

...
}

Also, there is no need to do

return new LimitConfiguration(limitConfiguration.getMinimum(),limitConfiguration.getMaximum());

in the controller. This is instantiating a new LimitConfiguration that will not be recognized by the spring container.

CodePudding user response:

Looks like LimitConfiguration class is used as a data transfer purpose. Then you can get the property values inside the controller class like this

@RestController
@RequestMapping("/limits")
public class LimitsConfigurationController {

    @Value("${limits-service.minimum}")
    private int minimum;

    @Value("${limits-service.maximum}")
    private int maximum;

    @GetMapping
    public LimitConfiguration retrieveLimitFromConfiguration() {
        return new LimitConfiguration(minimum, maximum);
    }
}

and the LimitConfiguration class should look like this

public class LimitConfiguration {

    private int minimum;

    private int maximum;

    // Getters, Setters, No args constructor and 2 args constructor

}
  • Related