Top Spring Boot Annotation.
Table of contents
@SpringBootApplication**:** The main annotation used in Spring Boot applications. It is a combination of three other annotations: @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
.
@Configuration
: Indicates that the class is a source of bean definitions. This is used to define Spring beans and other configuration settings.@EnableAutoConfiguration
: Enables Spring Boot's auto-configuration feature, which automatically configures Spring beans based on the dependencies included in the project.@ComponentScan
: Scans the specified packages for Spring components, such as controllers, services, and repositories.
By combining these three annotations, @SpringBootApplication
provides a convenient way to bootstrap a Spring Boot application. It is typically used to annotate the main class of a Spring Boot application, which contains the main
method. This annotation enables auto-configuration, component scanning, and other Spring features that make it easy to develop and deploy Spring Boot applications.
@Autowired
is an annotation used in Spring Framework to inject dependencies into a Spring-managed bean. It is used to automatically wire the dependencies of a bean at runtime, without the need for manual configuration.
When a bean is annotated with @Autowired
, Spring will look for other beans in the container that match the type of the dependency and automatically inject them into the bean. If there are multiple beans that match the type, Spring will use a specified qualifier to determine which bean to inject.
The @Autowired
annotation can be used to inject dependencies into fields, setter methods, and constructor parameters. Here are some examples:
- Field injection:
@Component
public class MyService {
@Autowired
private MyRepository myRepository;
}
2. Setter injection:
@Component
public class MyService {
private MyRepository myRepository;
@Autowired
public void setMyRepository(MyRepository myRepository) {
this.myRepository = myRepository;
}
}
3. Constructor injection:
@Component
public class MyService {
private MyRepository myRepository;
@Autowired
public MyService(MyRepository myRepository) {
this.myRepository = myRepository;
}
}
In addition to @Autowired
, there are other annotations such as @Qualifier
, @Resource
, and @Inject
that can also be used for dependency injection in Spring.
When a bean is annotated with @Autowired
, Spring will look for other beans in the container that match the type of the dependency and automatically inject them into the bean. If there are multiple beans that match the type, Spring will use a specified qualifier to determine which bean to inject.
@Component
is an annotation used in Spring Framework to mark a class as a Spring-managed bean. It is a generic stereotype annotation and can be used to annotate any class, indicating that it is a Spring component.
When a class is annotated with @Component
, Spring will automatically detect and instantiate the class as a bean. The bean will be added to the application context, making it available for use by other parts of the application.
Here’s an example of a class annotated with @Component
:
@Component
public class MyService {
public void doSomething() {
// ...
}
}
In the above example, MyService
is a Spring component, and Spring will create an instance of the class and manage its lifecycle. Other parts of the application can use MyService
by injecting it as a dependency using @Autowired
or other dependency injection annotations.
@Component
is a base annotation for other Spring stereotype annotations, such as @Controller
, @Service
, and @Repository
. These annotations provide additional semantics and can be used to indicate the specific role of a Spring-managed bean within the application. For example, a class annotated with @Controller
is typically used to handle HTTP requests in a Spring MVC application.
@Configuration
is an annotation used in Spring Framework to indicate that a class defines one or more Spring beans. It is used to create beans in the Spring container that can be used by other parts of the application.
When a class is annotated with @Configuration
, Spring will treat it as a source of bean definitions and will create and manage the beans defined in the class. The beans are created using the @Bean
annotation, which is used to mark a method that returns an object as a bean.
Here’s an example of a class annotated with @Configuration
:
@Configuration
public class MyConfiguration {
@Bean
public MyService myService() {
return new MyService();
}
}
In the above example, MyConfiguration
is a Spring configuration class that defines a bean of type MyService
. The myService()
method is annotated with @Bean
, indicating that it returns a Spring bean. When the Spring container starts up, it will create an instance of MyService
and add it to the application context.
@Configuration
is a powerful annotation that enables flexible and extensible bean configuration in Spring. It can be used to define beans with complex dependencies, externalize configuration settings, and create conditional beans based on certain conditions. It is commonly used in conjunction with other Spring annotations, such as @ComponentScan
and @PropertySource
.
@Bean
is an annotation used in Spring Framework to mark a method as a provider of a Spring-managed bean. It is used in conjunction with @Configuration
annotation to create beans in the Spring container that can be used by other parts of the application.
When a method is annotated with @Bean
, Spring will treat it as a factory method for creating a bean. The return value of the method is the object that will be managed by Spring as a bean. The name of the bean is determined by the name of the method.
Here’s an example of a method annotated with @Bean
:
@Configuration
public class MyConfiguration {
@Bean
public MyService myService() {
return new MyService();
}
}
In the above example, myService()
is a factory method that creates a bean of type MyService
. When the Spring container starts up, it will call the myService()
method and create an instance of MyService
, which will be added to the application context. Other parts of the application can use MyService
by injecting it as a dependency using @Autowired
or other dependency injection annotations.
@Bean
is a powerful annotation that enables flexible and extensible bean configuration in Spring. It can be used to create beans with complex dependencies, externalize configuration settings, and create conditional beans based on certain conditions. It is commonly used in conjunction with other Spring annotations, such as @Configuration
and @Autowired
.
@Controller
is an annotation used in Spring Framework to mark a class as a Spring MVC controller. It is used to handle HTTP requests in a Spring MVC web application.
When a class is annotated with @Controller
, Spring will treat it as a controller and map incoming requests to methods in the controller based on the URL pattern. The methods are typically annotated with @RequestMapping
annotation to specify the URL pattern that they handle.
Here’s an example of a class annotated with @Controller
:
@Controller
public class MyController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
In the above example, MyController
is a Spring MVC controller that handles requests to the URL "/hello". The hello()
method returns a string "hello", which is used as the view name to render the response. The view is typically resolved by a view resolver configured in the Spring application context.
@Controller
is a powerful annotation that enables flexible and extensible web application development in Spring. It can be used to handle different types of requests, including HTTP GET, POST, PUT, DELETE, and others. It is commonly used in conjunction with other Spring MVC annotations, such as @RequestMapping
, @RequestParam
, and @ModelAttribute
.
@Service
is an annotation used in Spring Framework to mark a class as a Spring-managed service. It is used to indicate that a class provides some business logic or service to other parts of the application.
When a class is annotated with @Service
, Spring will treat it as a service and create an instance of it in the Spring container. The service can be used by other parts of the application by injecting it as a dependency using @Autowired
or other dependency injection annotations.
Here’s an example of a class annotated with @Service
:
@Service
public class MyService {
public void doSomething() {
// some business logic here
}
}
In the above example, MyService
is a Spring-managed service that provides some business logic or service. When the Spring container starts up, it will create an instance of MyService
, which can be used by other parts of the application by injecting it as a dependency.
@Service
is a powerful annotation that enables flexible and extensible application development in Spring. It can be used to provide various types of services, such as data access, security, transaction management, and others. It is commonly used in conjunction with other Spring annotations, such as @Autowired
, @Transactional
, and @Component
.
@Repository
is an annotation used in Spring Framework to mark a class as a Spring-managed repository. It is used to indicate that a class provides data access to a particular data source, such as a database, using Spring Data.
When a class is annotated with @Repository
, Spring will treat it as a repository and create an instance of it in the Spring container. The repository can be used by other parts of the application by injecting it as a dependency using @Autowired
or other dependency injection annotations.
Here’s an example of a class annotated with @Repository
:
@Repository
public class MyRepository {
public List<User> findAll() {
// some data access logic here
}
}
In the above example, MyRepository
is a Spring-managed repository that provides data access to a particular data source. When the Spring container starts up, it will create an instance of MyRepository
, which can be used by other parts of the application by injecting it as a dependency.
@Repository
is a powerful annotation that enables flexible and extensible data access in Spring. It can be used to provide data access to various data sources, such as databases, NoSQL stores, and web services. It is commonly used in conjunction with other Spring annotations, such as @Autowired
, @Transactional
, and @Component
.
@RequestMapping
is an annotation used in Spring Framework to map a URL request to a particular method or controller in a Spring MVC web application. It is used to handle HTTP requests, such as GET, POST, PUT, DELETE, and others.
When a method or controller is annotated with @RequestMapping
, Spring will map incoming requests to that method or controller based on the URL pattern specified in the annotation. The URL pattern can include variable placeholders and regular expressions to provide flexible request mapping.
Here’s an example of a method annotated with @RequestMapping
:
@Controller
public class MyController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
In the above example, MyController
is a Spring MVC controller that handles requests to the URL "/hello". The hello()
method returns a string "hello", which is used as the view name to render the response. The view is typically resolved by a view resolver configured in the Spring application context.
@RequestMapping
is a powerful annotation that enables flexible and extensible web application development in Spring. It can be used to handle different types of requests, including HTTP GET, POST, PUT, DELETE, and others. It supports various options, such as request method, headers, content type, and others, to provide fine-grained request mapping. It is commonly used in conjunction with other Spring MVC annotations, such as @Controller
, @RequestParam
, and @ModelAttribute
.@PathVariable: Extracts a variable from the URL path and passes it as a method parameter. This can be used to provide dynamic behaviour in a controller.
@Value
is an annotation used in Spring Framework to inject values from external sources, such as properties files, environment variables, and command line arguments, into a Spring-managed bean. It is used to externalize configuration and make it easier to change values without modifying the code.
When a field or method parameter is annotated with @Value
, Spring will resolve the value from the external source and inject it into the bean at runtime. The value can be a string or a SpEL expression that evaluates to a string or a primitive type.
Here’s an example of a field annotated with @Value
:
@Service
public class MyService {
@Value("${my.property}")
private String myProperty;
public void doSomething() {
// use myProperty here
}
}
In the above example, MyService
is a Spring-managed service that uses a property value named "my.property". The property value is resolved from an external source, such as a application.properties file or a system property, and injected into the myProperty
field using @Value
. The value of myProperty
can be used in the doSomething()
method.
@Value
is a powerful annotation that enables flexible and extensible configuration in Spring. It can be used to inject values from various external sources, such as properties files, environment variables, and command line arguments. It supports various options, such as default values, placeholders, and SpEL expressions, to provide fine-grained configuration. It is commonly used in conjunction with other Spring annotations, such as @Autowired
, @Configuration
, and @Component
.
Thank you.
Subscribe to my newsletter
Read articles from Moin Malik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by