What are Annotations in Java ?

Abhinav Vinci
4 min readJul 22, 2023

What are Annotations ?

Annotations are a form of metadata that can be added to code elements such as classes, methods, fields, and parameters.

  • Annotations start with the @ symbol, followed by the annotation name. They can also have parameters that provide additional information or configuration for the annotated elements.

In Java, annotations are defined using the @interface keyword, and they can be added to code elements as shown in the examples below:

// Definition of a simple annotation
public @interface MyAnnotation {
}
// Usage of the annotation on a class
@MyAnnotation
public class MyClass {
}
what-when-how-why-advantage-and-disadvantage-of-java-annotations

Why use Annotations ?

  • They provide additional information about the code they are attached to and are used by various tools and frameworks to process or configure the code.
  • They make it easier to work with Spring and save you from writing a lot of repetitive code.
  • Annotations do not directly affect the behavior of the code at runtime; instead, they provide instructions or hints to the compiler or other tools.
  • By using annotations, developers can enhance code readability, reduce boilerplate configuration, and enable better integration with various tools and frameworks.

What’s Spring annotations ?

They are special keywords that you can use in your Java code to tell the Spring framework how to do certain things automatically.

In Spring, annotations are extensively used for dependency injection, aspect-oriented programming, and mapping HTTP endpoints to controller methods.

Spring Annotations Examples :

@Component:

The @Component annotation is a generic stereotype annotation that marks a Java class as a Spring component. It indicates that the class is a candidate for Spring's auto-detection and automatic bean registration.

import org.springframework.stereotype.Component;
@Component
public class MyComponent {
// Class implementation
}
  • The @Repository annotation is a specialization of the @Component annotation. It is used to indicate that a particular class is a Spring Data repository, responsible for data access operations.
  • The @Service annotation is also a specialization of @Component. It indicates that the class represents a service in the business logic layer.
  • The @Controller annotation is used to mark a class as a Spring MVC controller. It handles HTTP requests and serves as the entry point for handling web requests.
https://www.java67.com/2018/11/top-10-spring-framework-annotations-for-java-developers.html

@Autowired

This annotation helps with automatic dependency injection.

  • When you use @Autowired on a field, constructor, or setter method, Spring will automatically search for a bean that matches the type of the field, constructor parameter, or method argument, and inject it into the class where the @Autowired annotation is used.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyService {
private final MyRepository myRepository;
@Autowired
public MyService(MyRepository myRepository) {
this.myRepository = myRepository;
}
// Service methods using myRepository
}

Field Injection: Field injection is the most common way of using @Autowired. You simply annotate the field that you want Spring to inject with @Autowired, and Spring will automatically search for a bean of the same type.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyService {
@Autowired
private MyRepository myRepository;
// Service methods using myRepository
}

@Value

The @Value annotation is used to inject values from property files or environment variables into Spring beans.

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
@Value("${app.api.key}")
private String apiKey;
// Class implementation using apiKey
}

Why spring annotations ?

  1. Reduced Configuration: Prior to the widespread use of annotations, Spring relied heavily on XML configuration files to manage beans and their dependencies. With annotations, you can accomplish the same configuration directly in your Java code, reducing the need for verbose XML files.
  2. Simplified Dependency Injection: Annotations like @Autowired make dependency injection a breeze. You no longer need to write the injection code manually; Spring automatically identifies the dependencies and injects them where needed.
  3. Improved Readability: Annotations make your code more concise and easier to read. Instead of navigating through XML files, you can quickly understand the relationships and configurations within the Java classes themselves.
  4. Easy Customization: Spring allows you to create your custom annotations to encapsulate specific behaviors or configurations. This way, you can tailor the annotations to fit your application’s unique requirements.

In summary, Spring annotations streamline the development process, enhance code readability, and reduce boilerplate configuration, making it easier to work with the Spring framework.

--

--