WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Boot & Hibernate · Basic · question 8 of 100

Can you provide examples of common annotations used in Spring Boot applications?

📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

Annotations are an important aspect of Spring Boot, as they provide a way to declare the behavior and configuration of components in a Spring Boot application. Here are some common annotations used in Spring Boot applications, along with their purpose and usage:

@SpringBootApplication: This is a meta-annotation that combines several other annotations to provide a convenient way to configure a Spring Boot application. It is typically used to annotate the main class of a Spring Boot application, like so:

    @SpringBootApplication
    public class MyApp {
        public static void main(String[] args) {
            SpringApplication.run(MyApp.class, args);
        }
    }

@Controller: This annotation is used to indicate that a class is a controller, which means that it handles HTTP requests and returns responses. Here is an example of how to use the @Controller annotation:

    @Controller
    public class MyController {
        @GetMapping("/")
        public String index() {
            return "index";
        }
    }

@Service: This annotation is used to indicate that a class is a service, which means that it performs some business logic in a Spring Boot application. Here is an example of how to use the @Service annotation:

    @Service
    public class MyService {
        public void doSomething() {
            // ...
        }
    }

@Autowired: This annotation is used to indicate that a dependency should be injected into a class. It can be used on constructor parameters, fields, and methods. Here is an example of how to use the @Autowired annotation:

    @Service
    public class MyService {
        private MyRepository myRepository;
        
        @Autowired
        public MyService(MyRepository myRepository) {
            this.myRepository = myRepository;
        }
    }

@Repository: This annotation is used to indicate that a class is a repository, which means that it performs database operations in a Spring Boot application. Here is an example of how to use the @Repository annotation:

    @Repository
    public interface MyRepository extends JpaRepository<MyEntity, Long> {
        // ...
    }

@RequestMapping: This annotation is used to map HTTP requests to a controller method. It can be used to specify the HTTP method, URL path, and other request parameters. Here is an example of how to use the @RequestMapping annotation:

    @Controller
    @RequestMapping("/api")
    public class MyController {
        @GetMapping("/users")
        public List<User> getUsers() {
            // ...
        }
    }

Overall, annotations are a powerful and flexible way to configure and customize Spring Boot components. By using annotations to declare the behavior and configuration of components, Spring Boot applications can be built quickly and easily, with minimal boilerplate code.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Boot & Hibernate interview — then scores it.
📞 Practice Spring Boot & Hibernate — free 15 min
📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

All 100 Spring Boot & Hibernate questions · All topics