Spring Boot Annotation
Spring Boot Annotation
Spring Boot Annotations is a form of metadata that provides data about a program.
In other words, annotations are used to provide supplemental information about a
program. It is not a part of the application that we develop. It does not have a direct
effect on the operation of the code they annotate. It does not change the action of
the compiled program.
Pause
Unmute
Loaded: 34.84%
Fullscreen
Cancel
Example
1. public class Machine
2. {
3. private Integer cost;
4. @Required
5. public void setCost(Integer cost)
6. {
7. this.cost = cost;
8. }
9. public Integer getCost()
10.{
11.return cost;
12.}
13.}
Example
1. @Component
2. public class Customer
3. {
4. private Person person;
5. @Autowired
6. public Customer(Person person)
7. {
8. this.person=person;
9. }
10.}
Example
1. @Configuration
2. public class Vehicle
3. {
4. @BeanVehicle engine()
5. {
6. return new Vehicle();
7. }
8. }
Example
1. @ComponentScan(basePackages = "com.javatpoint")
2. @Configuration
3. public class ScanComponent
4. {
5. // ...
6. }
Example
1. @Bean
2. public BeanExample beanExample()
3. {
4. return new BeanExample ();
5. }
Example
1. @Component
2. public class Student
3. {
4. .......
5. }
Example
1. @Controller
2. @RequestMapping("books")
3. public class BooksController
4. {
5. @RequestMapping(value = "/{name}", method = RequestMethod.GET)
6. public Employee getBooksByName()
7. {
8. return booksTemplate;
9. }
10.}
@Service: It is also used at class level. It tells the Spring that class contains
the business logic.
Example
1. package com.javatpoint;
2. @Service
3. public class TestService
4. {
5. public void service1()
6. {
7. //business code
8. }
9. }
1. package com.javatpoint;
2. @Repository
3. public class TestRepository
4. {
5. public void delete()
6. {
7. //persistence code
8. }
9. }
1. @Controller
2. public class BooksController
3. {
4. @RequestMapping("/computer-science/books")
5. public String getAllBooks(Model model)
6. {
7. //application code
8. return "bookList";
9. }