Spring Framework Annotations

The Java Programming language has provided support for Annotations starting with Java 5. The Spring Framework started using annotations from since release 2.5 of the framework. Due to the way they are defined, annotations provide a lot of context in their declaration.

Prior to Annotations, the Spring Framework was mostly controlled through XML configuration files. In today’s modern programming environment, annotations provide tremendous capabilities in how we configure the behaviors of the Spring Framework for Java applications.

Let’s start by categorizing Spring Framework Annotations into three different groups: Spring Boot & Web Annotations, Spring Cloud Annotations, and Spring Framework Annotations.


Spring Framework Annotations

@Configuration

Configuration Annotation is used to mark a class as a source of the bean definitions. Beans are the components of the system that you want to wire together.

@ComponentScan

ComponentScan Annotation makes sure that Spring knows about your configuration classes so it can initialize the beans correctly. It makes Spring scan the packages configured with it for the @Configuration classes.

@Bean

Bean Annotation indicates that a method produces a bean that is to be managed by the Spring Container.

@Component

Component Annotation is another way to declare a bean. This will turn a class into a Spring Bean at during auto-scan.

@Service

Service Annotation marks a specialization of @Component annotation. This tells Spring that it’s safe to manage with more freedom than regular components.

@Autowired

Autowired Annotation will use Springs dependency injection to wire an appropriate bean into the marked class member.

@Lazy

Lazy Annotation will force @Bean or @Component to be initialized on demand instead of eager initialization.

@Qualifier

Qualifier Annotation filters what beans should be used to @Autowire a field or parameter.

@Value

Value Annotation indicates a default value expression for the field or parameter to initialize the property with.

@Required

Required Annotation will force the configuration to fail if the dependency can’t be injected.


Spring Boot & Web Annotations

@SpringBootApplication

SpringBootApplication Annotation will enable Spring to look at and combine the other annotations. Essential, SpringBootApplication is @Configuration, @EnableAutoConfiguration and @ComponentScan Annotations combined.

@EnableAutoConfiguration

EnableAutoConfiguration Annotation makes Spring guess the configuration based on the JAR files available on the classpath.

@Controller

Controller Annotation makes Spring configure your app to be a web application, capable of serving the HTTP response.

@RestController

RestController Annotation is a convenience syntax for @Controller and @ResponseBody together. This means that all the action methods in the marked class will return the JSON response.

@ResponseBody

ResponseBody Annotation is a utility annotation that makes Spring bind a method’s return value to the HTTP response body.

@RequestMapping

RequestMapping Annotation will specify a method in the controller that should be responsible for serving the HTTP request to the given path.

@RequestParam

RequestParam Annotation makes Spring parse the request parameters and put the appropriate ones into your method arguments.

@PathVariable

PathVariable Annotation will bring the values from the URL to the method arguments.


Spring Cloud Annotations

@EnableConfigServer

EnableConfigServer Annotation will turn your application into a server other apps can get their configuration from.

@EnableEurekaServer

EnableEurekaServer Annotation will make your Spring Boot app an Eureka discovery service. Now other apps can locate services through it.

@EnableDiscoveryClient

EnableDiscoveryClient Annotation will make your app register in the service discovery server and then consult with it to discover the other services you need.

@EnableCircuitBreaker

EnableCircuitBreaker Annotation will configure Hystrix circuit breaker protocols for your application.

@HystrixCommand

HystrixCommand Annotation will mark a method to fallback to another method upon failure.

Add a Comment

Your email address will not be published. Required fields are marked *