Spring and Spring Boot

107 flashcards by MentisLibrary

The Spring stack for Java developers: the dependency injection container, Boot auto-configuration and properties, Spring MVC controllers, Spring Data JPA, Bean Validation, transactions, Spring Security and the testing slices.

What is the Spring Framework?
A Java application framework built around an inversion of control container: objects declare what they need, and the container creates them, wires them together and manages their lifecycle.
What is inversion of control?
The application declares dependencies and the framework supplies them, rather than each object locating or constructing its own collaborators.
What is a Spring bean?
Any object the container creates, configures and manages, identified by a name and a type inside the application context.
How does component scanning find your classes?
It walks the package of the class annotated with @SpringBootApplication and every sub-package, registering classes marked with a stereotype annotation.
@Component, @Service, @Repository, @Controller
All four register a bean; the last three are specializations that state intent, and @Repository additionally translates persistence exceptions into Spring's DataAccessException hierarchy.
Which form of dependency injection should you use?
Constructor injection: dependencies are final, the object cannot exist half-built, and the class is usable in a plain unit test with new.
What does @Bean do?
Declares a bean produced by a method inside a @Configuration class, for objects you cannot annotate, such as third-party types.
How do you choose between two beans of the same type?
Mark one @Primary as the default, or name the one you want with @Qualifier at the injection point.
What are the main bean scopes?
singleton is the default and means one instance per container; prototype creates a new instance per injection; web applications add request and session.
Why must singleton beans be stateless?
One instance serves every concurrent request, so a mutable field is shared state without any synchronization.
What do @PostConstruct and @PreDestroy do?
Mark methods to run after the bean is fully injected and just before the container shuts it down.
What is ApplicationContext?
The container itself: it holds every bean definition, performs the wiring, publishes events and exposes the environment and resources.
What is aspect-oriented programming in Spring?
A way to apply cross-cutting behaviour, such as logging, metrics or transactions, by wrapping bean methods in a proxy rather than editing each method.
Why does a self-invocation skip @Transactional or @Async?
Because the annotation is implemented by a proxy that wraps the bean; an internal this.method() call never leaves the object, so the proxy is not involved.
What is Spring Boot?
A layer over Spring that adds auto-configuration, opinionated starter dependencies, embedded servers and externalized configuration, so an application runs with almost no wiring code.
What does @SpringBootApplication combine?
@Configuration, @EnableAutoConfiguration and @ComponentScan on one class, which is also the application's entry point.
What is a Spring Boot starter?
A dependency that pulls in a coherent set of libraries and triggers the matching auto-configuration, so one entry replaces a page of versions.
What is auto-configuration?
Conditional configuration classes that back off when you define your own bean, so defaults appear only where you have not decided.
How do you see what auto-configuration did?
Start the application with the debug flag, which prints the condition evaluation report listing matched and unmatched configurations with reasons.
What does Spring Boot Actuator provide?
Production endpoints over HTTP or JMX for health, metrics, environment, mappings, loggers and thread dumps.
How is a Spring Boot application packaged?
As an executable jar containing the application, its dependencies and an embedded server, built by the Boot build plugin.
What is CommandLineRunner used for?
Running code once after the context is fully started, receiving the raw command line arguments.
How do you fail fast on an invalid configuration?
Bind properties into a typed class and put Bean Validation constraints on it, so startup aborts instead of failing on the first request.
What is the difference between Spring Boot 2 and 3 for imports?
Spring Boot 3 moved to Jakarta EE, so javax.persistence and javax.validation became jakarta.persistence and jakarta.validation.
Where does Spring Boot read configuration from?
From an ordered list of property sources: command line arguments, environment variables, profile-specific files, then application.properties or application.yml.
How do you read a single property?
With @Value and a placeholder, optionally with a default after a colon.
What does @ConfigurationProperties do?
Binds a whole group of properties onto a typed object by prefix, with relaxed name matching between kebab-case and camelCase.
What is a Spring profile?
A named set of beans and properties activated by environment, so development, test and production can differ without code branches.
How do profile-specific property files work?
A file named application-<profile> is loaded on top of application when that profile is active, and its values win.
How should secrets be supplied?
Through environment variables or a secret manager, never as literals in a packaged properties file.