Your First Application
The entry point
@SpringBootApplication is three annotations in one:
| Annotation | Job |
|---|---|
@SpringBootConfiguration | this class may declare beans |
@ComponentScan | find components in this package and below |
@EnableAutoConfiguration | configure what the classpath suggests |
Warning
Component scanning starts at the application class’s package. Keep it at the root — classes in a sibling package are invisible.
A first endpoint
No web.xml, no servlet registration: spring-boot-starter-web puts Tomcat on the
classpath, and auto-configuration starts it.
Returning JSON
Return an object and Jackson serializes it.
Configuration
src/main/resources/application.properties:
Or YAML, in application.yaml:
What happens on startup
SpringApplication.runcreates an application context- component scan collects your
@Component/@Service/@RestControllerclasses - auto-configuration adds what the classpath implies (web server,
DataSource, …) - beans are created and wired
- the embedded Tomcat starts and binds a port
★ Exercises
- Add a
/timeendpoint returning the current time as JSON. - Change the port to 9000 in
application.properties. - Move
HelloControllerintocom.example.other— what happens, and why? - Run with
-Ddebugand find three auto-configurations that matched.