Spring Boot Flashcards

1
Q

What is Spring Boot?

A

Spring Boot is a framework designed to simplify the bootstrapping and development of a new Spring application. The framework takes an opinionated approach to configuration, freeing developers from the need to define boilerplate configuration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the advantage of using Spring Boot?

A

The advantage of using Spring Boot is that it simplifies Spring application setup and development. It has auto-configuration features for Spring and third party libraries, allowing developers to start quickly. It reduces the amount of manual configuration and code, and it has embedded Tomcat, Jetty, or Undertow, so you don’t need to deploy WAR files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the key components of Spring Boot?

A

Key components of Spring Boot are: Spring Boot Starters, Spring Boot AutoConfigurator, Spring Boot Actuator, Spring Boot CLI, and Spring Initializr.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between Spring and Spring Boot?

A

Spring is a comprehensive framework for building enterprise-grade applications, while Spring Boot is a way to simplify the configuration for Spring applications, aiming to be a rapid application development platform. Spring Boot uses the Spring framework underneath.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the Spring Initializer?

A

Spring Initializr is a web-based tool provided by Spring, allowing you to generate the basic project structure for your Spring Boot application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of the @SpringBootApplication annotation?

A

The @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and triggers auto-configuration and component scanning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between @Configuration and @SpringBootConfiguration?

A

@Configuration is used to mark a class as a source of bean definitions. On the other hand, @SpringBootConfiguration is a specialized form of @Configuration that indicates a configuration class specifically for Spring Boot application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between @Component and @Service?

A

Both @Component and @Service are used to denote a class as a Spring Bean. However, @Service is used in the service layer, and @Component can be used in any layer (it’s a more generic annotation).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of the @Autowired annotation?

A

The @Autowired annotation is used to auto-wire spring bean on setter methods, instance variable, and constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between @ComponentScan and @SpringBootApplication?

A

Both @ComponentScan and @SpringBootApplication serve a similar purpose, which is to tell Spring where to search for components. However, @SpringBootApplication is more specific to Spring Boot applications and it also implies @EnableAutoConfiguration and @Configuration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of the @Bean annotation?

A

The @Bean annotation is used at the method level and it tells Spring that the method will return an object that should be registered as a bean in the Spring application context.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of the @Value annotation?

A

The @Value annotation is used at the field level or method parameter level and it indicates a default value for the field or parameter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between @Controller and @RestController in Spring Boot?

A

The @Controller annotation is used to define a controller in a traditional Spring MVC application. The @RestController annotation is a specialized version of the controller, designed to simplify creating RESTful web services. It includes the @ResponseBody annotation which binds the return value directly to the response body.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between @Repository and @Service in Spring Boot?

A

The @Repository annotation is used on the persistence layer of the application, which indicates that the class provides the mechanism for storage, retrieval, search, update, and delete operation on objects. The @Service annotation is used at the service layer, which holds business logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of the @Primary annotation in Spring Boot?

A

The @Primary annotation in Spring Boot is used when you have more than one bean of the same type and you want to give one of them higher priority.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between @Qualifier and @Primary in Spring Boot?

A

Both @Qualifier and @Primary are used to resolve the autowiring conflict when there are multiple beans of the same type. @Primary gives a default preference to a bean, while @Qualifier can be used to specify the exact bean id of the bean to be wired.

17
Q

What is the purpose of the @Profile annotation in Spring Boot?

A

The @Profile annotation in Spring Boot is used to define beans or bean configurations that should be active in certain environments only.

18
Q

What is the difference between @Async and @Scheduled in Spring Boot?

A

The @Async annotation is used to make a method execute in asynchronous mode, whereas @Scheduled annotation is used to run methods at scheduled intervals.

19
Q

What is the purpose of the @Import annotation in Spring Boot?

A

The @Import annotation in Spring Boot allows you to import additional configuration classes.

20
Q

What is the difference between @Import and @ComponentScan in Spring Boot?

A

The @Import annotation in Spring Boot is used to import other configuration classes into the current configuration. @ComponentScan is used to specify the packages to be scanned for components, configurations, and services.

21
Q

What is the difference between @SpringBootApplication and @EnableAutoConfiguration in Spring Boot?

A

Both @SpringBootApplication and @EnableAutoConfiguration serve a similar purpose, which is to trigger auto-configuration of the Spring application context, attempting to guess and configure beans that you are likely to need. @SpringBootApplication also enables component scan on the package where it is declared and configures Spring Boot features.

22
Q

What is the purpose of the @ConditionalOnXXX annotations in Spring Boot?

A

The @ConditionalOnXXX annotations in Spring Boot are used to conditionally load beans. For example, @ConditionalOnProperty, @ConditionalOnClass, @ConditionalOnMissingBean, etc. These annotations help to load beans based on certain conditions.

23
Q

What is the difference between @EnableWebMvc and @SpringBootApplication in a Spring Boot web application?

A

@EnableWebMvc is used when you want to take full control of Spring MVC. It disables the default Spring MVC auto-configuration and allows you to use your own configuration. @SpringBootApplication, on the other hand, is used to enable auto-configuration, component scan, and optionally configuration properties on your Spring Boot application.

24
Q

What is the purpose of the @SpringBootTest annotation in Spring Boot?

A

The @SpringBootTest annotation is used in testing Spring Boot applications. It provides a fully loaded ApplicationContext and sets up a test environment similar to the one for running applications.

25
Q

What is the difference between @MockBean and @Mock in Spring Boot?

A

@MockBean is a Spring Boot specific annotation used for Mockito Mocking in Spring Boot tests. It adds the mock object into the application context. @Mock is a Mockito annotation used to create and inject mocked instances.

26
Q

What is the purpose of the @DataJpaTest annotation in Spring Boot?

A

The @DataJpaTest annotation in Spring Boot is used for JPA (Java Persistence API) tests. It configures an in-memory database, Hibernate, Spring Data, and the DataSource. It uses @Transactional tests, so each test method runs within its transaction that will be rolled back after execution.

27
Q

What is the purpose of the @TestPropertySource annotation in Spring Boot?

A

The @TestPropertySource annotation in Spring Boot is used in your test classes to define which properties files you want to use in your tests.

28
Q

What is the purpose of the @TestConfiguration annotation in Spring Boot?

A

The @TestConfiguration annotation in Spring Boot is used on classes that should contribute to the Spring ApplicationContext when running tests. It’s typically used to declare beans that are used in tests only.

29
Q

What is the difference between @Test and @SpringBootTest in Spring Boot?

A

@Test is a JUnit annotation used to specify methods that are test methods in JUnit tests. @SpringBootTest is a Spring Boot specific annotation used in conjunction with @Test to indicate that the context loaded for the test should be a Spring Boot context.

30
Q

How can you customize the banner in a Spring Boot application?

A

You can customize the banner in a Spring Boot application by creating a banner.txt file in the resources directory, or by defining a ‘spring.banner.*’ property in the application.properties (or .yml) file. You can also use a banner image.

31
Q

How can you customize the log level in a Spring Boot application?

A

You can customize the log level in a Spring Boot application by setting ‘logging.level.=LEVEL’ in the application.properties (or .yml) file, where ‘’ is a package name or logging category, and ‘LEVEL’ is one of the following: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF.

32
Q

How can you externalize configuration in a Spring Boot application?

A

You can externalize configuration in a Spring Boot application using application.properties or application.yml files. Spring Boot also provides support for command line arguments, environment variables, and you can also use @Value annotation, or type-safe Properties classes.

33
Q

How can you create a REST API using Spring Boot?

A

You can create a REST API using Spring Boot by annotating a controller class with @RestController annotation, and then mapping request URIs to methods using @RequestMapping or @GetMapping, @PostMapping etc.

34
Q

How can you implement security in a Spring Boot application?

A

You can implement security in a Spring Boot application using Spring Security, which provides comprehensive security services for Java EE-based enterprise software applications. This includes authentication, authorization, CSRF protection, and more.

35
Q

How can you integrate with a database using Spring Boot?

A

You can integrate with a database using Spring Boot by using Spring Data JPA, which provides a way to implement JPA repositories.