
Most Important Spring Boot Annotations
Here are the most used Spring Boot annotations, along with their explanations and examples:
- @SpringBootApplication : This annotation is used to configure a Spring Boot application. It combines the functionality of the @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations.
@SpringBootApplication
public class MyApplication{
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
- The @SpringBootApplication annotation tells Spring Boot to automatically configure the application, scan for beans, and start the application.
- @EnableAutoConfiguration: This annotation enables auto-configuration. It means that Spring Boot looks for auto-configuration beans on its classpath and automatically applies them. Note that this annotation must be used with @Configuration1. For example:
@Configuration
@EnableAutoConfiguration
class VehicleFactoryConfig {}
- @Component : This annotation is used to mark a class as a Spring bean. Spring beans are objects that are managed by the Spring IoC container.
@Component
publicclassMyBean{
public String getName() {
return"My Bean";
}
}
- The @Component annotation tells Spring Boot to automatically register the MyBean class as a bean in the application context.
- @Controller : This annotation is used to mark a class as a web controller. Web controllers are responsible for handling HTTP requests.
@Controller
public class MyController{
@RequestMapping("/")
public String home() {
return"index";
}
}
- The @Controller annotation tells Spring Boot that the MyController class is a web controller. The @RequestMapping("/") annotation tells Spring Boot to map all HTTP requests to the home() method in the MyController class.
- @RequestMapping : This annotation is used to map HTTP requests to methods in a controller.
- The @RequestMapping annotation can be used to map HTTP requests to methods in a controller by specifying the HTTP method and the URL path. For example, the following annotation maps all HTTP GET requests to the home() method in the MyController class:
@RequestMapping("/")
public String home() {
return "index";
}
- The @RequestMapping annotation can also be used to map HTTP requests to methods in a controller by specifying the HTTP method and the URL path pattern. For example, the following annotation maps all HTTP GET requests to the home() method in the MyController class, but only if the URL path starts with /api/:
@RequestMapping(value = "/api/**", method = RequestMethod.GET)
public String home() {
return "index";
}
- @Autowired : This annotation is used to inject dependencies into a bean. Dependencies are other beans that are required by a bean to function.
@Autowired
private MyBean myBean;
public String getName() {
return myBean.getName();
}
- The @Autowired annotation tells Spring Boot to inject an instance of the MyBean class into the myBean field in the MyController class.
- @Service : This annotation is used to mark a class as a service. Services are responsible for providing business logic to a Spring Boot application.
@Service
public class MyService{
public String getName() {
return "My Service";
}
}
- The @Service annotation tells Spring Boot that the MyService class is a service.
- @Repository : This annotation is used to mark a class as a repository. Repositories are responsible for accessing data from a database or other data source.
@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long>{
}
- The @Repository annotation tells Spring Boot that the MyRepository interface is a repository. The JpaRepository interface provides a number of methods for accessing data from a database.
- @ConditionalOnClass and @ConditionalOnMissingClass: Using these conditions, Spring will only use the marked auto-configuration bean if the class in the annotation’s argument is present/absent
For example:
@Configuration
@ConditionalOnClass(DataSource.class)
class MySQLAutoconfiguration {
//...
}
- @ConditionalOnBean and @ConditionalOnMissingBean: These annotations can be used when we want to define conditions based on the presence or absence of a specific bean
For example:
@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBeanentityManagerFactory() {
// ...
}
@ConditionalOnProperty: With this annotation, we can make conditions on the values of properties
For example:
@Bean
@ConditionalOnProperty(
name = "usemysql",
havingValue = "local"
)
DataSourcedataSource() {
// ...
}
If you want to know more or have doubt in any topic regarding spring boot annotations then feel free to contact us or leave a comment below the blog post. We are here to help you! :)
0 Comments