Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
The @Component
annotation in Spring is a general-purpose stereotype annotation that indicates a class is a Spring component. When you annotate a class with @Component
, it tells Spring that this class is a bean and should be automatically discovered and instantiated as a bean when component scanning is enabled.
There are specialized forms of the @Component
annotation which are used for specific purposes, such as @Service
, @Repository
, and @Controller
, but they all serve the fundamental purpose of class identification for component scanning.
Example:
Creating a Simple Component
Let's say you have a simple class that you want Spring to manage:
package com.example; import org.springframework.stereotype.Component; @Component public class HelloWorld { public String sayHello() { return "Hello, World!"; } }
Here, the @Component
annotation tells Spring to treat the HelloWorld
class as a managed bean.
Enabling Component Scanning
If you're using XML-based configuration:
<context:component-scan base-package="com.example"/>
If you're using Java-based configuration:
@Configuration @ComponentScan("com.example") public class AppConfig { // ... }
Using the Component in another class
Here's how you can use the HelloWorld
component in another class using @Autowired
:
package com.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class GreetingService { private final HelloWorld helloWorld; @Autowired public GreetingService(HelloWorld helloWorld) { this.helloWorld = helloWorld; } public void greet() { System.out.println(helloWorld.sayHello()); } }
In the above example, Spring automatically injects the HelloWorld
bean into the GreetingService
bean due to the @Autowired
annotation on the constructor.
Bootstrap the Application
Finally, you'd bootstrap your application, either through a main method using AnnotationConfigApplicationContext
(for a standalone application) or through a web configuration (for a web application). For a standalone application:
public class Application { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); GreetingService service = context.getBean(GreetingService.class); service.greet(); } }
When you run the Application
class, you'll see "Hello, World!" printed on the console.
Remember, the @Component
annotation by itself won't get your beans picked up by Spring. You need to ensure that component scanning is enabled and configured to scan the appropriate packages.
Using @Component annotation in Spring:
@Component
annotation is a generic stereotype annotation indicating that the class is a Spring component.// ExampleComponent.java import org.springframework.stereotype.Component; @Component public class ExampleComponent { // Component logic... }
Spring @Component example project:
@Component
annotation.// ExampleComponent.java import org.springframework.stereotype.Component; @Component public class ExampleComponent { // Component logic... } // MainApplication.java import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MainApplication { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.example"); ExampleComponent exampleComponent = context.getBean(ExampleComponent.class); // Use exampleComponent... context.close(); } }
Creating Spring components with @Component annotation:
@Component
annotation to create Spring components.// MyComponent.java import org.springframework.stereotype.Component; @Component public class MyComponent { // Component logic... }
How to scan and register components in Spring using @Component:
@Component
are automatically registered by Spring component scanning.// AppConfig.java import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("com.example.components") public class AppConfig { // Configuration details... }
Dependency injection with @Component in Spring:
@Autowired
annotation with @Component
-annotated classes.// ClientComponent.java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class ClientComponent { private final ExampleComponent exampleComponent; @Autowired public ClientComponent(ExampleComponent exampleComponent) { this.exampleComponent = exampleComponent; } // Use exampleComponent... }
Customizing component names with @Component in Spring:
value
attribute of the @Component
annotation.// MyComponent.java import org.springframework.stereotype.Component; @Component("customName") public class MyComponent { // Component logic... }
Working with @ComponentScan and @Component in Spring:
@ComponentScan
to enable component scanning and discover @Component
-annotated classes.// AppConfig.java import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("com.example.components") public class AppConfig { // Configuration details... }
Spring @Component vs @Service vs @Repository annotation:
@Component
, @Service
, and @Repository
are all specialized forms of @Component
, they carry additional semantic meaning.// ExampleService.java import org.springframework.stereotype.Service; @Service public class ExampleService { // Service logic... } // ExampleRepository.java import org.springframework.stereotype.Repository; @Repository public class ExampleRepository { // Repository logic... }