Spring- Stereotype Annotations

Spring -Stereotype Annotations

Spring provides 4 stereotype annotations:

  • @Component
  • @Repository
  • @Service
  • @Controller

@Component This is a generic annotation and can be applied to any class of the application to make it a spring managed component(simply, the generic stereotype for any spring managed component).

When the classpath is scanned by the spring s component-scan (@ComponentScan) feature, it will identify the classes annotated with @Component annotation (within the given package) and create the beans of such classes and register them in the ApplicationContext.

@Component is a class level annotation and its purpose is to make the class as spring managed component and auto-detectable bean for classpath scanning feature

ANNOTATION USE DESCRIPTION
@Component Type Generic stereotype annotation for any Spring-managed component.
@Controller Type Stereotypes a component as a Spring MVC controller.
@Repository Type Stereotypes a component as a repository. Also indicates that SQLExceptions thrown from the component’s methods should be translated into Spring DataAccessExceptions.
@Service Type Stereotypes a component as a service.

@Component (and @Service and @Repository) are used to auto-detect and auto-configure beans using classpath scanning.

  • @Component is a generic stereotype for any Spring-managed component.

  • @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

  • Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects.

  • For example, these stereotype annotations make ideal targets for pointcuts. It is also possible that @Repository, @Service, and @Controller may carry additional semantics in future releases of the Spring Framework.

  • Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

@Bean vs @Componenet

  1. @Component auto detects and configures the beans using classpath scanning whereas @Bean explicitly declares a single bean, rather than letting Spring do it automatically.

  2. @Component does not decouple the declaration of the bean from the class definition where as @Bean decouples the declaration of the bean from the class definition.

  3. @Component is a class level annotation where as @Bean is a method level annotation and name of the method serves as the bean name.

  4. @Component need not to be used with the @Configuration annotation where as @Bean annotation has to be used within the class which is annotated with @Configuration.

  5. We cannot create a bean of a class using @Component, if the class is outside spring container whereas we can create a bean of a class using @Bean even if the class is present outside the spring container.

  6. @Component has different specializations like @Controller, @Repository and @Service whereas @Bean has no specializations.

Context Configuration Annotations

1.@Profile
The Spring @Profile allow developers to register beans by condition. For example load a database properties file based on the application running in development, test, staging or production environment.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("production")
public @interface Production {
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Profile("development")
public @interface Production {
}

2.@Import annotation
Normally, you will split a large Spring XML bean files into multiple small files, group by module or category, to make things more maintainable and modular. For example,

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<import resource="config/customer.xml"/>
    <import resource="config/scheduler.xml"/>
 
</beans>

3. @ImportResource
We can import XML configurations with this annotation. We can specify the XML file locations with the locations argument, or with its alias, the value argument:

@Configuration
@ImportResource("classpath:/annotations.xml")
class VehicleFactoryConfig {}

4.@PropertySource
With this annotation, we can define property files for application settings. @PropertySource leverages the Java 8 repeating annotations feature, which means we can mark a class with it multiple times:

@Configuration
@PropertySource("classpath:/annotations.properties")
@PropertySource("classpath:/vehicle-factory.properties")
class VehicleFactoryConfig {}

5.@PropertySources
We can use this annotation to specify multiple *@PropertySource *configurations:

@Configuration
@PropertySources({ 
    @PropertySource("classpath:/annotations.properties"),
    @PropertySource("classpath:/vehicle-factory.properties")
})
class VehicleFactoryConfig {}