Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier than compared to classic Java frameworks and application programming interfaces (APIs), such as Java database connectivity (JDBC), JavaServer Pages(JSP), and Java Servlet. This framework uses various new techniques such as Aspect-Oriented Programming (AOP), Plain Old Java Object (POJO), and dependency injection (DI), to develop enterprise applications. Now talking about Spring Annotation
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program.
There are many annotations available in Spring Framework. Some of the Spring Framework Annotations are listed below as follows where here we are going to discuss one of the most important annotations that are, @Component Annotation
- @Required
- @Autowired
- @Configuration
- @ComponentScan
- @Bean
- @Component
- @Controller
- @Service
- @Repository, etc.
@Component Annotation
@Component is a class-level annotation. It is used to denote a class as a Component. We can use @Component across the application to mark the beans as Spring’s managed components. A component is responsible for some operations. Spring framework provides three other specific annotations to be used when marking a class as a Component.
- @Service
- @Repository
- @Controller
1: @Service: We specify a class with @Service to indicate that they’re holding the business logic. Besides being used in the service layer, there isn’t any other special use for this annotation. The utility classes can be marked as Service classes.
2: @Repository: We specify a class with @Repository to indicate that they’re dealing with CRUD operations, usually, it’s used with DAO (Data Access Object) or Repository implementations that deal with database tables.
3: @Controller: We specify a class with @Controller to indicate that they’re front controllers and responsible to handle user requests and return the appropriate response. It is mostly used with REST Web Services.
Note: All these four annotations are available in package ‘org.springframework.stereotype’ and part of ‘spring-context jar’.
Example: Spring @Component
Let’s create a very simple Spring boot application to showcase the use of Spring Component annotation and how Spring autodetects it with annotation-based configuration and classpath scanning.
Step 1: Create a Simple Spring Boot Project.
Step 2: Add the spring-context dependency in your pom.xml file. Go to the pom.xml file inside your project and add the following spring-context dependency.
XML
< dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >5.3.13</ version > </ dependency > |
Step 3: Create a simple component class
Go to the src > main > java > your package name > right-click > New > Java Class and create your component class and mark it with @Component annotation.
Example
Java
// Java Program to Illustrate Component class package com.example.demo; import org.springframework.stereotype.Component; // Annotation @Component // Class public class ComponentDemo { // Method public void demoFunction() { // Print statement when method is called System.out.println( "Hello GeeksForGeeks" ); } } |
Step 4: Create an annotation-based spring context
Now go to your Application (@SpringBootApplication) file and here in this file create an annotation-based spring context and get the ComponentDemo bean from it.
Example
Java
// Java Program to Illustrate Application class // Importing package here package com.example.demo; // Importing required classes import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.AnnotationConfigApplicationContext; // Annotation @SpringBootApplication // Class public class DemoApplication { // Main driver method public static void main(String[] args) { // Annotation based spring context AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.scan( "com.example.demo" ); context.refresh(); // Getting the Bean from the component class ComponentDemo componentDemo = context.getBean(ComponentDemo. class ); componentDemo.demoFunction(); // Closing the context // using close() method context.close(); } } |
Output:
So you can see the power of @Component annotation, we didn’t have to do anything to inject our component to spring context. The below image shows the directory structure of our Spring Component example project.