Spring Core Annotations

Last updated on February 28th, 2024

The Java Programming language provided support for Annotations from Java 1.5. Popular Java frameworks were quick to adopt annotations and the Spring Framework started using annotations from release 2.5. Due to this way they are defined, annotations provide a lot of context in their declaration. Before annotations, the behaviour of the Spring Framework was largely controlled via XML configuration. Today, the use of annotations provides us with huge capabilities in how we configure the behaviours of the Spring Framework.

Some of the important Spring Core Annotations
These are the following Spring Core Framework annotations are:
       • @Required
       • @Autowired
       • @Component
       • @Qualifier
       • @Configuration
       • @Bean
       • @ComponentScan
       • @Lazy
       • @Value etc…

@Required:
This annotation is applied to bean setter methods. Consider a scenario where we need to enforce a required property. This @Required annotation indicates that the affected bean must be populated at configuration time with the required property. 

@Autowired:
This @Autowired annotation is applied to the fields, setter methods, and constructors. This annotation injects object dependency implicitly. We use this @Autowired annotation to mark the dependency that will be injected by the Spring container.

Field Injection with @Autowired Example:

class Employee{
@Autowired
Address address;
}

Setter Injection with @Autowired Example:

class Employee{
Address address;
@Autowired
void setAddress(Address address) {
this.address = address;
    }
}

→ Refer to this article Setter Injection with Annotation for example of Setter Injection with @Autowired.

Constructor Injection with @Autowired Example:

class Employee{
Address address;
@Autowired
Employee(Address address) {
this.address = address;
    }
}

→ Refer to this article Constructor Injection with Annotation for example of Setter Injection with @Autowired.

@Component:
 This @Component annotation is used on classes to indicate a Spring component. This annotation marks the Java class as a bean or says component so that the component-scanning mechanism of Spring can add to the application context. Refer to this article Default Bean Id Creation using @Component for an example of this annotation.

@Qualifier:
This @Qualifier annotation is used along with @Autowired annotation. When we need more control of the dependency injection process, @Qualifier can be used. This @Qualifier annotation can be specified on individual constructor arguments or method parameters. It is used to avoid confusion which occurs when we create more than one bean of the same type and want to wire only one of them with a property.
Consider an example in which an interface BeanIntf is implemented by two bean classes Bean1 and Bean2.

@Component
public class Bean1 implements BeanIntf{
}
@Component
public class Bean2 implements BeanIntf{
}

→ Now if BeanDemo autowires this interface, Spring will not know which one of the two implementations to inject. The solution to this problem is the use of the @Qualifier annotation.

@Component
public class BeanDemo {
  @Autowired
  @Qualifier("bean1")
  private BeanIntf beanIntf;
  ...
}

→ With this @Qualifier annotation added then Spring will now know which bean to autowire where bean1 is the name of Bean1.

@Configuration:
It is used to indicate that a class declares one or more @Bean methods. These classes are processed by the Spring container to generate bean definitions and service requests for those beans at the runtime. Refer to this article Spring Bean Configuration with Java Code (No Xml) for an example of this annotation.
Example:

@Configuration
public class AppConfig {
@Bean
Employee getEmployee(){
return new Employee();
    }
}

Employee{
----------------
---------------
}

@Bean:
This @Bean annotation is used at the method level. It works with @Configuration to create Spring beans. As mentioned earlier, @Configuration annotation will have methods to instantiate and configure dependencies. Such methods will be annotated with the @Bean. The method is annotated with this annotation works as bean Id and it creates and returns the actual bean.
Example:

@Configuration
public class AppConfig{
@Bean
public Employee(){
return new Employee(address());
}
@Bean
public Address address(){
    return new Address();
  }
}

→ Refer to this article Spring Bean Configuration with Java Code (No Xml) for an example of this annotation.

@ComponentScan:
This @ComponentScan annotation is used with @Configuration annotation to allow Spring to know the packages to scan for annotated components. @ComponentScan annotation is also used to specify base packages using base package classes or base package attributes to scan. If the specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

@Lazy:
This @Lazy annotation is used on component classes. By default, all autowired dependencies are created and configured at the startup. But if we want to initialize a bean lazily, we can use @Lazy annotation over the class. This means that the Spring Bean will be created and initialized only when it is first requested. We can also use this annotation on the @Configuration classes. It indicates that all @Bean methods within that @Configuration should be lazily initialized.

@Value:
This @Value annotation is used at the field, constructor parameter, and method parameter level. This annotation indicates a default value expression for the field or parameter to initialize the property with. As the @Autowired annotation tells Spring to inject an object into another when it loads our application context, we can also use this @Value annotation to inject values from a property file into a bean’s attribute. This supports both #{…} and ${…} placeholders.Refer to the article Injecting Value from Properties File with XML for an example of this annotation.

Conclusion:
In this topic, we learnt about important Spring Core Annotations.

Leave a Comment