On this page
Spring Framework
Prev: JUnit 5 | Up: third_party/java | Next: Spring Boot
Rel: Spring Boot, Classes, Records & Sealed Hierarchies, Jackson, JUnit 5, Apache Maven, Gradle
Ref: Spring Framework Reference Documentation
Tags: #public #prog #java #third-party #framework #di #web #tier/core
Overview
Spring Framework is the dominant application framework in the Java ecosystem. Its core provides an Inversion of Control (IoC) / Dependency Injection (DI) container, aspect-oriented programming (AOP), declarative transactions, and web/data abstractions. Spring Boot builds directly on top of Spring Framework to add auto-configuration, embedded servers, and opinionated starter dependencies.
Core Modules
| Module | Responsibility |
|---|---|
spring-core / spring-beans / spring-context |
IoC container (ApplicationContext), bean lifecycle, @Configuration, @Component |
spring-aop |
Proxy-based cross-cutting concerns (logging, metrics, security, transactions) |
spring-tx / spring-jdbc |
Declarative @Transactional boundaries and JdbcClient / JdbcTemplate |
spring-web / spring-webmvc |
@RestController, HTTP routing, content negotiation (Jackson), and synchronous RestClient |
spring-test |
SpringExtension for JUnit 5, MockMvc, and context caching across tests |
Idiomatic Constructor Injection
Prefer constructor injection with private final fields (never field injection with @Autowired on private fields). Constructor injection guarantees immutable dependencies and makes classes trivial to unit-test with plain JUnit without starting a Spring container:
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
public interface OrderRepository {
void save(Order order);
}
@Service
public class OrderService {
private final OrderRepository repository;
// Single constructor: Spring injects automatically without @Autowired
public OrderService(OrderRepository repository) {
this.repository = repository;
}
@Transactional
public void placeOrder(Order order) {
repository.save(order);
}
}
Explicit Java Configuration (@Configuration + @Bean)
Use @Bean factory methods for third-party classes you don't own (such as a configured Jackson ObjectMapper or HttpClient):
import java.net.http.HttpClient;
import java.time.Duration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ClientConfig {
@Bean
HttpClient httpClient() {
return HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.build();
}
}
Proxy Gotcha (@Transactional Self-Invocation)
Spring implements @Transactional and @Cacheable using dynamic proxies around beans. Calling a @Transactional method from another method inside the same class (this.helper()) bypasses the proxy and does not start a transaction. Put transactional boundaries on a separate collaborator bean or on the public entry-point method.
Linked from
Related notes
- tuts/third_party/java/spring_boot Linked here; links to this page; shared tags: #framework, #java, #prog, #tier, #web
- tuts/third_party/java/jackson Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java/junit Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java Linked here; links to this page; shared tags: #java, #prog
- tuts/third_party/java/gradle Linked here; shared tags: #java, #prog, #tier
- tuts/third_party/java/maven Linked here; shared tags: #java, #prog, #tier
- tuts/java links to this page; shared tags: #java, #prog
- tuts/java/language_and_oop/classes_records_and_sealed_hierarchies Linked here; shared tags: #java, #tier
- tuts/interop/java_and_c_ffm_and_jni shared tags: #java, #prog, #tier
- tuts/java/builtins/AutoCloseable shared tags: #java, #prog, #tier