Veröffentlicht · wird verbessert
Spring Boot-Anleitung · 3/6
Dieses Kapitel ist vorerst nur auf Englisch verfügbar.
Spring Boot runs on top of the Spring Framework. Spring's IoC container creates your objects and wires them together, and Spring Boot adds starters and auto-configuration so that most of the setup disappears. This chapter covers beans and dependency injection, auto-configuration, starters, the web layer and type-safe configuration.
An object managed by the container is called a bean. Annotate a class with a stereotype and component scanning registers it:
| Annotation | Purpose |
|---|---|
@Component | General-purpose component |
@Service | Business logic |
@Repository | Data access; translates persistence exceptions |
@Controller / @RestController | Web request handling |
When a bean needs another bean, take it through constructor injection. With a single constructor, @Autowired is unnecessary, and final fields make the class easy to create by hand in tests.
@Service
public class GreetingService {
private final Clock clock;
public GreetingService(Clock clock) {
this.clock = clock;
}
public String greet(String name) {
int hour = LocalTime.now(clock).getHour();
return (hour < 12 ? "Good morning, " : "Hello, ") + name;
}
}For objects you cannot annotate, such as library classes, declare a @Bean method in a @Configuration class:
@Configuration
public class AppConfig {
@Bean
public Clock clock() {
return Clock.systemDefaultZone();
}
}@SpringBootApplication combines three annotations: @SpringBootConfiguration (a configuration class), @ComponentScan (component scanning) and @EnableAutoConfiguration.
Auto-configuration inspects the classpath and the beans you already defined, then creates the beans you are likely to need. With Spring MVC and Tomcat on the classpath you get an embedded web server and a DispatcherServlet; with a JDBC driver and spring.datasource.url you get a DataSource. Every auto-configuration is guarded by conditions such as @ConditionalOnClass and @ConditionalOnMissingBean, so if you define a bean of the same type yourself, auto-configuration backs off. Your own beans always win.
To see which auto-configurations applied and why, run with --debug and read the condition evaluation report. To switch one off entirely, use exclude:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication { /* ... */ }A starter is a curated set of dependencies for one capability. A single dependency brings in the libraries and their matching auto-configuration:
spring-boot-starter-webmvc: Spring MVC, embedded Tomcat, JSON via Jacksonspring-boot-starter-data-jpa: Spring Data JPA with Hibernatespring-boot-starter-validation: Bean Validationspring-boot-starter-security: Spring Securityspring-boot-starter-actuator: health checks, metrics and other production featuresMethods in a @RestController return objects that are serialized to JSON. Bind path variables with @PathVariable and request bodies with @RequestBody. Java records make good request and response types.
@RestController
@RequestMapping("/api/todos")
public class TodoController {
private final TodoService service;
public TodoController(TodoService service) {
this.service = service;
}
@GetMapping("/{id}")
public TodoResponse get(@PathVariable Long id) {
return service.find(id);
}
@PostMapping
public ResponseEntity<TodoResponse> create(@RequestBody CreateTodo request) {
TodoResponse created = service.create(request.title());
return ResponseEntity.created(URI.create("/api/todos/" + created.id())).body(created);
}
public record CreateTodo(String title) {}
public record TodoResponse(Long id, String title, boolean done) {}
}You can inject single values with @Value("${...}"), but groups of related settings are safer as @ConfigurationProperties. Keys under the prefix bind to the record's components:
@ConfigurationProperties("app.mail")
public record MailProperties(String from, Duration timeout, boolean enabled) {}app:
mail:
from: no-reply@example.com
timeout: 5s
enabled: trueAdd @ConfigurationPropertiesScan to the entry-point class and these records become beans that other beans can receive through their constructors. Values like 5s are converted to Duration automatically.
Kotlin classes are final by default, which prevents Spring from creating proxies. Kotlin projects from Initializr include the kotlin("plugin.spring") compiler plugin, which opens classes carrying Spring annotations. Constructor injection maps naturally onto Kotlin's primary constructor:
@Service
class GreetingService(private val clock: Clock) {
fun greet(name: String) = "Hello, $name"
}@ConfigurationProperties records for type safety.
0 Kommentare
Anmelden · Melde dich an, um einen Kommentar zu schreiben.
Schreib den ersten Kommentar.