What is the significance of the @Primary annotation?
@Primary marks the default bean Spring injects when several beans share a type. See how it is resolved, how @Qualifier overrides it, and what to avoid.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
@Primary marks the default bean Spring injects when several beans share a type. See how it is resolved, how @Qualifier overrides it, and what to avoid.
Learn how to write a custom BeanPostProcessor in Spring, where it runs in the bean lifecycle, how to register and order it, and when AOP is a better fit.
Component scanning lets Spring find @Component classes and register them as beans. See how to configure it and fix missing beans.
@ComponentScan makes Spring discover annotated classes in chosen packages and register them as beans. Covers Spring Boot defaults, filters and common errors.
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.