when running spring Boot application getting required a bean of type that could not be found
@SpringBootApplication
@ComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(MileageFeeController.class, args);
}
}
@Controller
public class MileageFeeController {
@Autowired
MileageFeeCalculator calc;
public float mileageFee( int miles) {
System.out.println("Helloe");
return calc.mileageCharge(miles);
}
}
@Component
public class MileageFeeCalculator {
@Autowired
private MileageRateService rateService;
public float mileageCharge(int miles) {
return (miles * rateService.ratePerMile());
}
}
@Component
public class MileageRateService {
public float ratePerMile() {
return 0.565f;
}
}
While executing above code getting below errors
com.demo.DemoApplication : Starting DemoApplication using Java 17 on N13115 with PID 9336 (D:\Spring Tool\SpringWorkSpace\Demo\target\classes started by jitendra2.k in D:\Spring Tool\SpringWorkSpace\Demo)
com.demo.DemoApplication : No active profile set, falling back to default profiles: default
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mileageFeeController': Unsatisfied dependency expressed through field 'calc'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.demo.MileageFeeCalculator' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2022-01-15 20:28:39.643 ERROR 9336 --- [main] o.s.b.d.LoggingFailureAnalysisReporter:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field calc in com.demo.MileageFeeController required a bean of type 'com.demo.MileageFeeCalculator' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.demo.MileageFeeCalculator' in your configuration.
You can try with below change in DemoApplication class. Instead of passing MileageFeeController.class to SpringApplication.run, pass DemoApplication.class
@SpringBootApplication
@ComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
As you mentioned you have to MileageFeeController.mileageFee method, you should run SpringBoot application as CommandLineRunner as below.
CommandLineRunner has a run method which you can use to trigger MileageFeeController.mileageFee method
@SpringBootApplication
@ComponentScan
public class DemoApplication implements CommandLineRunner {
@Autowired
MileageFeeController feeController;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) {
System.out.println(feeController.mileageFee(5));
}
}