How do you define a configuration class in Spring?
Define a Spring configuration class with @Configuration and @Bean methods, then load it with Spring Boot or an annotation-based context.
2,887 posts about Java: questions with answers, tutorials and articles, newest first.
Define a Spring configuration class with @Configuration and @Bean methods, then load it with Spring Boot or an annotation-based context.
@Qualifier tells Spring which bean to inject when multiple beans match a type. See constructor injection, custom qualifier annotations and how it differs from @Primary.
Spring injects dependencies from its IoC container. See constructor, setter and field injection, how to pick between beans and why constructor injection is recommended.
@Autowired tells Spring to inject a matching bean automatically. Learn the three injection styles and why constructor injection is preferred.
ApplicationContext extends BeanFactory with events, i18n, resource loading and eager singletons. See the differences with current Spring code.
Learn how to retrieve beans from a Spring ApplicationContext with getBean(), when to prefer injection, and how lazy initialization and scopes affect creation.
The ApplicationContext creates and wires beans, manages scopes and lifecycle, and adds events, messages and resources over BeanFactory. See how it works in practice.
Declare a fragment interface, implement it in a class ending in Impl, and extend it from your repository so one bean serves derived and custom methods.
@EnableJpaRepositories scans for Spring Data JPA repositories and configures them. See when Boot already does it and when to add it yourself.
Set up Querydsl with Spring Data JPA and write type-safe, dynamic queries using JPAQueryFactory, BooleanBuilder, projections and pagination.
Learn what @PageableDefault does in Spring Data web support, how its attributes work, and how to combine it with @SortDefault and global Boot properties.
Page and sort JPA results with Pageable, PageRequest and Sort, add @Query count queries, and avoid in-memory paging and unstable sorts.
H2 is an embedded Java database for tests and prototypes; MySQL is a production client-server database. See the differences and how to test safely.
Spring Boot uses HikariCP by default. Tune pool size, timeouts and lifetimes through spring.datasource.hikari properties and monitor the pool with Actuator.
spring.datasource.url tells Spring Boot which JDBC database to connect to. Learn URL formats, per-environment overrides and common connection errors.
Add a JDBC driver, set the spring.datasource properties and remove H2 to point Spring Boot at PostgreSQL or MySQL, with profiles and Testcontainers.
Spring Boot does not ship a default database, but it auto-configures an embedded H2 database when no DataSource is set and H2 is on the classpath.
Add spring-boot-starter-data-jpa, set the datasource, write jakarta.persistence entities and a JpaRepository. Boot configures Hibernate and transactions for you.
With Spring Data JPA, repository interfaces are detected by scanning, so @Repository is optional; it still matters for custom DAOs and implementations.
Implement the DAO pattern in Java with a generic interface, a JPA EntityManager implementation, Optional results and service-level transactions.