DEV Community

Tamilarasi Nathan
Tamilarasi Nathan

Posted on

Annotation

@Component
The @Component annotation is used to mark a Java class as a Spring-managed component. When a class is annotated with @Component, Spring automatically detects it during classpath scanning and creates an object of that class, storing it in the Spring IoC (Inversion of Control) container.

@Autowired
The @Autowired annotation is used for automatic dependency injection in Spring. When applied to a field, constructor, or setter method, Spring automatically injects the required dependency (a bean that is already created and managed by the Spring container).

@bean
The @bean annotation is used to define a bean in Spring manually. It is applied to a method, and the method's return value is registered as a bean in the Spring container. This is especially useful for creating beans from third-party classes (such as classes from .jar files) .
Note: The method must be inside a class annotated with @Configuration.

@Configuration
The @Configuration annotation is used to indicate that the class can be used by the Spring IoC container as a source of bean definitions. It typically contains one or more @Bean-annotated methods. When this annotation is used, Spring treats the class as a configuration class and processes the @bean methods to register them as beans.

@ComponentScan
The @ComponentScan annotation is used to specify the base packages that Spring should scan to find components (@Component, @Service, @Repository, @Controller). It helps Spring discover and register beans automatically.

Top comments (0)