Here you will know about most common Spring Bean Annotations to create different type of beans. We can create spring beans by declaring 3 ways:
- Declare bean using XML configuration
- Declare bean with @Bean annotations in @Configuration class.
- Declare bean with one on type from org.springframework.stereotype package and leave the rest to component scanning.
Here we will mainly focus on annotations which help for creating beans:
See Also :
- Java : Annotation Tutorial
- Spring Boot Annotations
- Spring Core Annotations
- Spring Web & REST Annotations
- Spring Scheduling Annotations
- Spring Data Annotations
- Spring Junit Annotations
- Restful Webservice & JAX-RS Annotations
- JAXB Annotations
- JUnit Annotations
- Hibernate & JPA Annotations
Component Scanning
Spring can automatically scan a package or class if component scanning is enabled.
We can configure component scanning parameter as below:
- Use basePackages to scan @Configuration classes from package.
- Use basePackageClasses to scan .
- If no argument is specified, the scanning happens from the same package where the @ComponentScan annotated class is present.
Component Scan from Packages
@Configuration @ComponentScan(basePackages = "com.fiot.annotations") class VehicleFactoryConfig {}
Component Scan from Classes
@Configuration @ComponentScan(basePackageClasses = VehicleFactoryConfig.class) class VehicleFactoryConfig {}
Both arguments are arrays so that we can provide multiple packages for each. After Java 8 we can use repeating annotation also.
@Configuration @ComponentScan(basePackages = "com.fiot.annotations") @ComponentScan(basePackageClasses = VehicleFactoryConfig.class) class VehicleFactoryConfig {}
or
@Configuration @ComponentScans({ @ComponentScan(basePackages = "com.fiot.annotations"), @ComponentScan(basePackageClasses = VehicleFactoryConfig.class) }) class VehicleFactoryConfig {}
Component Scan XML Configuration
@Component
@Component is a class level annotation. During the component scan, Spring Framework automatically detects classes annotated with @Component.
@Component class CarUtility { // ... }
Note: By default, the bean instances @Component class have the same name as the class name with a lowercase initial. On top of that, we can specify a different name using the optional value argument of this annotation.
Since @Repository, @Service, @Configuration, and @Controller are all meta-annotations of @Component, they share the same bean naming behavior. Also, Spring automatically picks them up during the component scanning process.
@Repository
@Repository annotation use to represent DAO or Repository classes of database access layer in an application.
@Repository class VehicleRepository { // ... }
Main advantage of using @Repository annotation is that it has automatic persistence exception translation enabled. When using a persistence framework such as Hibernate, native exceptions thrown within classes annotated with @Repository will be automatically translated into subclasses of Spring’s DataAccessExeption.
To enable exception translation, we need to declare our own PersistenceExceptionTranslationPostProcessor bean:
@Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); }
@Service
@Service annotation use for service layer classes as business logic of an application usually resides within the service layer.
@Service public class VehicleService { // ... }
@Controller
@Controller is a class level annotation which tells the Spring Framework that this class serves as a controller in Spring MVC.
@Controller public class VehicleController { // ... }
@Configuration
Configuration classes can contain bean definition methods annotated with @Bean:
@Configuration class VehicleFactoryConfig { @Bean Engine engine() { return new Engine(); } }
Stereotype Annotations with AOP
Spring stereotype annotations, makes easy to create a pointcut that targets all classes that have a particular stereotype.
For example: Suppose we want to measure the execution time and performance of methods from the DAO layer. We’ll create the following aspect (using AspectJ annotations) taking advantage of @Repository stereotype:
@Aspect @Component public class PerformanceAspect { @Pointcut("within(@org.springframework.stereotype.Repository *)") public void daoClassMethods() {}; @Around("daoClassMethods()") public Object measureMethodExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.nanoTime(); Object returnValue = joinPoint.proceed(); long end = System.nanoTime(); String methodName = joinPoint.getSignature().getName(); System.out.println( "Execution of " + methodName + " took " + TimeUnit.NANOSECONDS.toMillis(end - start) + " ms"); return returnValue; } }
In this example, we have created a pointcut that matches all methods in classes annotated with @Repository. We used the @Around advice to then target that pointcut and determine the execution time of the intercepted methods calls. Same approach can be apply on other layer of application.
You must log in to post a comment.