Spring Application Services
Exercise: Hello Application (Basic Components)
- Command Line Runner
- Implement a Spring Boot application with a command line runner
HelloRunner
as component
- In the
run
method, print a hello message to the console
- Service Components
- Implement a service
HelloService
with a method sayHello(Sting name, int age)
that returns a casual or formal message depending on the age of the user
- Implement a service
GreetingService
with a getGreeting()
method that returns a greeting depending on the daytime
- Dependency Injection
- Inject the Greeting service into the Hello service and use it to create the formal hello message
- Inject the Hello service into the command line runner and invoke its method
- Lifecycle Callbacks
- Implement the lifecycle callbacks
PostConstruct
and PreDestroy
to observe the construction and destruction of the service instances
Solution