Spring Core Annotations with Examples

Core Spring Framework Annotations are a set of annotations that are used to simplify the development process of Java applications using the Spring Framework. These annotations can be used to inject dependencies, define components, and specify configuration details without the need for explicit XML configuration. Some of the most commonly used Core Spring Framework Annotations include @Configuration, @ComponentScan, @Required, @Bean, @Autowired, @Qualifier, @Lazy, and @Value.

@Configuration

The @Configuration annotation in Spring Boot indicates that a class contains bean definitions for the application context. This means that the Spring container can use the class to generate bean instances and manage their lifecycle. Classes annotated with @Configuration are typically used to define application-level beans and their dependencies and to configure various aspects of the Spring container, such as the component scan base packages and the properties to be used by the application.

Read more about @Configuration annotation.

@ComponentScan

The @ComponentScan annotation is used to enable component scanning in a Spring Boot application. Component scanning is a feature of the Spring framework that allows it to automatically detect and register beans in your application.

By default, Spring Boot will scan for beans in the package containing your application’s main class. You can use the @ComponentScan annotation to specify additional packages to scan or to exclude certain packages from the scan.

Read more about component scanning.

@Required

The @Required annotation in Spring Boot is used to indicate that a particular bean property or constructor argument is required to be injected. When a bean is initialized, if any required properties or arguments are not injected, a BeanInitializationException will be thrown. This annotation is typically used in conjunction with dependency injection to ensure that all required dependencies are properly injected into a bean before it is used.

@Bean

The @Bean annotation is used to define a bean in a @Configuration class. When a method is annotated with@Bean, the return value of the method will be registered as a bean in the application context.

For example, you might use the @Bean annotation to define a data source bean like this:

@Configuration
public class AppConfig {
   @Bean
   public DataSource dataSource() {
       return new DataSource();
   }
}

Read more about @Bean annotation.

@Autowired

The @Autowired annotation is a powerful tool in the Spring framework that allows you to inject dependencies into your beans. It is often used to inject services, repositories, and other objects required by your application.

  1. First, let’s create a simple service we want to inject into our bean. We will create a service called HelloService, which has a single method called sayHello().
@Service
public class HelloService {
  public String sayHello() {
    return "Hello World!";
  }
}
  1. Next, let’s create a bean into which we want to inject the HelloService. We will create a bean called MyBean, which has a field called helloService.
@Component
public class MyBean {
  private HelloService helloService;
}
  1. To inject the HelloService into the MyBean, we can use the @Autowired annotation on the helloService field.
@Component
public class MyBean {
  @Autowired
  private HelloService helloService;
}

Read more about @Autowired annotation.

@Qualifier

@Qualifier is an annotation in Spring Boot used to specify a specific bean that should be injected into a field or constructor. This can be useful when multiple beans of the same type are available in the application context, and you want to specify which one to use. The @Qualifier annotation is used with the @Autowired annotation to specify the specific bean to be injected. For example:

@Autowired @Qualifier("myBean") private MyBean myBean;

In this example, the @Autowired annotation tells Spring to inject a bean of the specified type (MyBean) into the myBean field. The @Qualifier(“myBean”) annotation specifies that the bean with the name “myBean” should be injected. If multiple beans of type MyBean are available in the application context, this annotation helps specify which one to use.

Read more about @Qualifier annotation.

@Lazy

The @Lazy annotation in Spring Boot is a way to tell the Spring framework to lazily initialize a bean, meaning that the bean will not be created until it is actually needed by the application. This can be useful for optimizing the application’s performance, as it can help reduce the number of beans that are created and initialized at startup. Additionally, the @Lazy annotation can be used to specify that a bean should be created as a singleton, meaning that only one instance of the bean will be created and shared among all components that depend on it.

Read more about @Lazy annotation.

@Value

The @Value annotation in Spring Boot injects values from external sources, such as properties files or environment variables, into a field or method parameter. It allows you to specify a default value for a field or parameter if it is not found in the external source. This allows you to externalize your configuration and make it easier to manage and maintain. For example, you might use the @Value annotation to inject a database connection string from a properties file into a field in your application.

Read more about @Value annotation.

I hope this tutorial was helpful to you. To learn more, check out the Spring Boot tutorials page.