In this quick article, we will discuss how to use @Component annotation in Spring (Spring Boot) based applications.
YouTube Video
Spring @Component Annotation Overview
The @Component annotation indicates that an annotated class is a "component". Such classes are considered candidates for auto-detection when using annotation-based configuration and classpath scanning.In short, @Component is a class-level annotation. During the component scan, Spring Framework automatically detects classes annotated with @Component annotation and creates Spring beans for those classes.
Component Scanning
Spring can automatically scan a package for beans if component scanning is enabled.@ComponentScan configures which packages to scan for classes with annotation configuration. We can specify the base package names directly with one of the basePackages or value arguments (value is an alias for basePackages):
@Configuration @ComponentScan(basePackages = "com.javaguides.annotations") class UserConfig {}
Spring @Component Annotation Example
Let’s create a very simple Spring boot maven application to showcase the use of Spring @Component annotation and how Spring autodetects it with annotation-based configuration and classpath scanning.Add below Spring boot starter web dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Create Spring Component class - ComponentDemo.java
Let’s create a simple component class and mark it with @Component annotation.
@Component
class ComponentDemo{
public String getValue() {
return "Hello World";
}
}
Spring Container will automatically create and manage the spring bean for the above class because it is annotated with @Component annotation.
Running Spring Boot Application
Note that we have created ApplicationContext and retrieved ComponentDemo bean using getBean() Method.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
ComponentDemo componentDemo = (ComponentDemo) applicationContext.getBean("componentDemo");
System.out.println(componentDemo.getValue());
}
}
@Component
class ComponentDemo {
public String getValue() {
return "Hello World";
}
}
Output:
@Component("componentDemo")
class ComponentDemo {
public String getValue() {
return "Hello World";
}
}
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Comments
Post a Comment
Leave Comment