Spring Annotations allows us to configure dependencies and implement dependency injection through java programs.

Spring Annotations

  • Spring framework implements and promotes the principle of control inversion (IOC) or dependency injection (DI) and is in fact an IOC container.
  • Traditionally, Spring allows a developer to manage bean dependencies by using XML-based configuration.
  • There is an alternative way to define beans and their dependencies. This method is a Java-based configuration.
  • Unlike the XML approach, Java-based configuration allows you to manage bean components programmatically. That’s why Spring annotations were introduced.

In this article we will explore most commonly used Spring Annotations and also look at some example program.

Spring Annotations List

Some of the spring core framework annotations are:

  1. @Configuration: Used to indicate that a class declares one or more @Bean methods. These classes are processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
  2. @Bean: Indicates that a method produces a bean to be managed by the Spring container. This is one of the most used and important spring annotation. @Bean annotation also can be used with parameters like name, initMethod and destroyMethod.
    • name – allows you give name for bean
    • initMethod – allows you to choose method which will be invoked on context register
    • destroyMethod – allows you to choose method which will be invoked on context shutdown

    For example:

  3. @PreDestroy and @PostConstruct are alternative way for bean initMethod and destroyMethod. It can be used when the bean class is defined by us. For example;

  4. @ComponentScan: Configures component scanning directives for use with @Configuration classes. Here we can specify the base packages to scan for spring components.
  5. @Component: Indicates that an annotated class is a “component”. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
  6. @PropertySource: provides a simple declarative mechanism for adding a property source to Spring’s Environment. There is a similar annotation for adding an array of property source files i.e @PropertySources.
  7. @Service: Indicates that an annotated class is a “Service”. This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.
  8. @Repository: Indicates that an annotated class is a “Repository”. This annotation serves as a specialization of @Component and advisable to use with DAO classes.
  9. @Autowired: Spring @Autowired annotation is used for automatic injection of beans. Spring @Qualifier annotation is used in conjunction with Autowired to avoid confusion when we have two of more bean configured for same type.

Spring MVC Annotations

Some of the important Spring MVC annotations are:

  1. @Controller
  2. @RequestMapping
  3. @PathVariable
  4. @RequestParam
  5. @ModelAttribute
  6. @RequestBody and @ResponseBody
  7. @RequestHeader and @ResponseHeader

You can learn more about them at Spring MVC Tutorial.

Spring Transaction Management Annotations

@Transactional is the spring declarative transaction management annotation, read more at Spring MVC Hibernate.

Spring Security Annotations

@EnableWebSecurity is used with @Configuration class to have the Spring Security configuration defined, read more at Spring Security Example.

Spring Boot Annotations

  1. @SpringBootApplication
  2. @EnableAutoConfiguration

Read more at Spring Boot Example.

Spring Annotations Example

Let’s look at a simple example where we will use Spring annotations in our application. Below image illustrates my Spring Annotations Example project.

spring-annotations

Spring Framework Dependencies

I have created the maven project and added Spring Core Framework dependencies.

This will pull up all the spring core jars for our project.

Component Classes

Next step is to create component classes. Here I am imitating multiple database components, one for MySQL and another for Oracle.

DataBaseDriver is the base interface that we will implement.

Notice the use of @Component annotation to indicate spring framework to treat this class as a Component. We are also using @PropertySource and @Value annotations, Spring will use these at runtime to inject and set these variable values from specified property file. Below is the properties declared in mysqldatabase.properties file.

OracleDriver is a simple bean, we will use service class to inject properties to this bean.

Spring Service Class

Here we are using @Service annotation to indicate Spring framework to treat this as a Service class. Then we are using @Autowired and @Qualifier("oracleDriver") annotations to tell spring framework to inject bean named oracleDriver to class property dataBaseDriver. Note that we haven’t yet created this spring bean.

Spring Beans

Final step is to create our spring beans and configuration classes to glue everything together.

Notice the bean definition for oracleDriver. In this method, we are reading properties from oracledatabase.properties file that is being set to environment variable by Spring framework.

Here is the properties defined in oracledatabase.properties file.

Our spring annotations example project is ready to test. As a summary, we performed following steps:

  1. Created maven project and added required spring dependencies.
  2. Created component classes and inject properties from a resource file into it’s variable.
  3. If we have a third party component, we can use Service class to inject dependencies into it. Just like we did for OracleDriver through UserService class.
  4. Finally, we created Configuration class to define spring beans and set the base package to scan for spring component classes and configure them.

Spring Annotations Example Testing

Here is our main class to test our Spring annotations example project.

Below image shows the output produced. Notice that we haven’t configured any logging framework, so all the spring framework logging is getting printed into console in red color.

spring annotations example

That’s all for a brief introduction for spring annotations. I have listed here most of the important annotations, but there are a lot more of them for specific tasks. You can download my spring annotations example project from below link.

Reference: API Doc

By admin

Leave a Reply

%d bloggers like this: