Spring Java-Based Configuration
Notes
A Comprehensive Guide to Spring Annotations and Dependency
Injection
Compiled on July 13, 2025
Created for Java Developers
Contents
1 Java-Based Configuration 2
1.1 Key Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Bean Naming 2
2.1 Default Bean Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2.2 Custom Bean Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Scope Annotation 2
3.1 Scope Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4 Autowiring 3
4.1 Types of Autowiring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5 Primary and Qualifier Annotations 3
5.1 @Primary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5.2 @Qualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
6 Component Stereotype Annotations 4
6.1 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
7 Autowire Injection Methods 4
7.1 Field Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
7.2 Constructor Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
7.3 Setter Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
7.3.1 Best Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
8 Scope and Value Annotations 5
8.1 @Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
8.2 @Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1
1 Java-Based Configuration
Spring allows defining beans in Java classes using annotations, replacing traditional XML-
based configuration (e.g., applicationContext.xml).
1.1 Key Annotations
• @Configuration: Indicates that a class declares one or more @Bean methods and
serves as a source of bean definitions.
• @Bean: Marks a method as a bean definition, allowing Spring to manage the
returned object.
1.2 Example
1 @Configuration
2 public class AppConfig {
3 @Bean
4 public Alien alien () {
5 return new Alien () ;
6 }
7 }
Listing 1: AppConfig.java
To use the configuration:
1 App lica ti onContext context = new
A n n o t a t i o n C o n f i g A p p l i c a t i o n C o n t e x t ( AppConfig . class ) ;
2 Alien obj = context . getBean ( Alien . class ) ;
2 Bean Naming
By default, Spring uses the method name as the bean ID when defined with @Bean.
2.1 Default Bean Name
1 @Bean
2 public Alien alien () {} // Bean ID = " alien "
2.2 Custom Bean Name
1 @Bean ( name = " superAlien " )
2 public Alien alien () {} // Bean ID = " superAlien "
3 Scope Annotation
The @Scope annotation defines the lifecycle and instantiation behavior of a bean.
2
3.1 Scope Types
• singleton (default): A single instance shared across the application context.
• prototype: A new instance is created each time the bean is requested.
• request, session: Scoped to HTTP request or session (web applications only).
3.2 Example
1 @Component
2 @Scope ( " prototype " )
3 public class Alien {}
4 Autowiring
The @Autowired annotation enables automatic dependency injection by Spring.
4.1 Types of Autowiring
• By Type: Matches beans by their type (default behavior).
• By Name: Matches when the field name corresponds to a bean ID.
• Constructor: Injects dependencies via a constructor.
4.2 Example
1 @Component
2 public class Alien {
3 @Autowired
4 private Laptop laptop ;
5 }
5 Primary and Qualifier Annotations
These annotations resolve ambiguity when multiple beans of the same type exist.
5.1 @Primary
1 @Component
2 @Primary
3 public class Laptop implements Computer {}
Marks a bean as the preferred choice when multiple beans of the same type are available.
3
5.2 @Qualifier
1 @Autowired
2 @Qualifier ( " desktop " )
3 private Computer comp ;
Specifies a particular bean by its ID to resolve ambiguity.
6 Component Stereotype Annotations
Stereotype annotations mark classes as Spring-managed components, each with specific
semantic roles.
6.1 Types
• @Component: A generic Spring-managed bean.
• @Service: Indicates a service-layer component.
• @Repository: Marks a Data Access Object (DAO) component, with added ex-
ception translation.
• @Controller: Designates a web controller in Spring MVC.
All are treated as components by Spring but provide semantic clarity.
7 Autowire Injection Methods
Dependencies can be injected via fields, constructors, or setters.
7.1 Field Injection
1 @Autowired
2 private Laptop laptop ;
Most common but less explicit; suitable for optional dependencies.
7.2 Constructor Injection
1 @Autowired
2 public Alien ( Laptop laptop ) {
3 this . laptop = laptop ;
4 }
Recommended for mandatory dependencies (Spring 5+ best practice).
4
7.3 Setter Injection
1 @Autowired
2 public void setLaptop ( Laptop laptop ) {
3 this . laptop = laptop ;
4 }
Useful for optional or changeable dependencies.
7.3.1 Best Practice
For Spring 5 and later, prefer constructor injection for mandatory dependencies to ensure
immutability and testability.
8 Scope and Value Annotations
Additional annotations for configuring bean behavior and injecting values.
8.1 @Scope
Controls the bean’s lifecycle (e.g., singleton, prototype).
1 @Scope ( " prototype " )
8.2 @Value
Injects literal values or values from configuration files (e.g., application.properties).
1 @Value ( " 42 " )
2 private int age ;
3
4 @Value ( " $ { app . name } " )
5 private String appName ; // from application . properties