Want to prove that dependency injection actually works and not just in conference slides This tiny Spring Boot Hello World example uses a Spring bean and a startup runner to print or serve a greeting on embedded Tomcat It is the kind of minimal setup that teaches IoC without drama
Spring Boot wires a bean you declare into the places you need it This is inversion of control in practice Not magic just configuration and a framework doing the heavy lifting Constructor injection keeps dependencies explicit and easy to test If your app prints Hello World it means the bean was found and injected correctly and Tomcat can serve requests
// Greeting service as a Spring bean
@Component
public class GreetingService {
public String greet() {
return "Hello World"
}
}
// Runner that executes at startup
@Component
public class AppRunner implements CommandLineRunner {
private final GreetingService greetingService
public AppRunner(GreetingService greetingService) {
this.greetingService = greetingService
}
@Override
public void run(String... args) {
System.out.println(greetingService.greet())
}
}
Use a @RestController instead of CommandLineRunner if you prefer to serve the greeting over HTTP Then inject the same GreetingService into the controller constructor and return the greeting from a handler method
This minimal example teaches the essentials of Spring Boot dependency injection IoC and lifecycle without pulling you into configuration quicksand Build on it when you are brave enough to add more features and worse dependencies
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.