0% found this document useful (0 votes)
6 views8 pages

Questions

The document provides a comprehensive overview of Spring Boot, detailing its features, configuration, and development practices. It covers topics such as auto-configuration, database integration, testing, security, and creating REST APIs. Additionally, it includes examples and explanations for various annotations, exception handling, and custom starters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Questions

The document provides a comprehensive overview of Spring Boot, detailing its features, configuration, and development practices. It covers topics such as auto-configuration, database integration, testing, security, and creating REST APIs. Additionally, it includes examples and explanations for various annotations, exception handling, and custom starters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Basic Questions

Q1: What is Spring Boot?

A1: Spring Boot is an extension of the Spring framework that simplifies the setup and development
of new Spring applications. It provides defaults for code and annotation configuration to quickly start
new Spring projects.

Q2: What are the main features of Spring Boot?

A2:

• Auto-configuration: Automatically configures your Spring application based on the jar


dependencies you have added.

• Standalone: Runs as a standalone Java application.

• Opinionated Defaults: Provides defaults to reduce developer effort.

• Embedded Servers: Comes with embedded servers like Tomcat, Jetty, or Undertow.

• Spring Boot CLI: Allows you to run and test Spring applications from the command line.

Q3: What is Spring Initializr?

A3: Spring Initializr is a web-based tool provided by Spring to generate a Spring Boot project
structure. It allows you to select dependencies and generate a project with a single click.

Configuration Questions

Q4: How do you create a Spring Boot application?

A4: You can create a Spring Boot application using Spring Initializr, Spring Boot CLI, or your IDE. The
typical steps include:

1. Go to Spring Initializr.

2. Select project metadata and dependencies.

3. Generate and download the project.

4. Import the project into your IDE and run it.

Q5: What is the use of the application.properties file?

A5: The application.properties file is used to configure your Spring Boot application. It allows you to
set various application-level properties, such as server port, database configurations, and custom
application properties.

Advanced Questions

Q6: How does Spring Boot auto-configuration work?

A6: Spring Boot auto-configuration works by scanning the classpath for specific classes and beans. If
certain classes are found, Spring Boot auto-configures beans and settings for those classes. For
example, if you have spring-boot-starter-data-jpa in your classpath, Spring Boot auto-configures a
DataSource, EntityManagerFactory, and other beans required for JPA.
Q7: How can you disable specific auto-configuration classes?

A7: You can disable specific auto-configuration classes using the @SpringBootApplication annotation
with the exclude attribute:

java

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

public class MyApplication {

public static void main(String[] args) {

SpringApplication.run(MyApplication.class, args);

Q8: What is Spring Boot Actuator?

A8: Spring Boot Actuator provides production-ready features such as monitoring, metrics, and health
checks. It includes several built-in endpoints that can be accessed via HTTP to monitor and manage
your application.

Development and Testing

Q9: How do you integrate Spring Boot with a database?

A9: To integrate Spring Boot with a database, you typically:

1. Add the relevant starter dependency (e.g., spring-boot-starter-data-jpa for JPA).

2. Configure the datasource properties in the application.properties file.

3. Define entity classes and repositories.

Example application.properties:

properties

spring.datasource.url=jdbc:mysql://localhost:3306/mydb

spring.datasource.username=root

spring.datasource.password=root

spring.jpa.hibernate.ddl-auto=update

Q10: How do you test Spring Boot applications?

A10: You can test Spring Boot applications using Spring's testing support. Common practices include:

• Unit Testing: Using @RunWith(SpringRunner.class) and @SpringBootTest annotations.

• Mocking Dependencies: Using @MockBean to mock dependencies.


• Integration Testing: Using @SpringBootTest to load the full application context and test the
application as a whole.

Miscellaneous

Q11: What is a Spring Boot starter?

A11: A Spring Boot starter is a set of convenient dependency descriptors you can include in your
application. They simplify the process of including dependencies related to a specific technology or
framework. For example, spring-boot-starter-web includes all necessary dependencies to build a web
application.

Q12: How do you secure a Spring Boot application?

A12: You can secure a Spring Boot application using Spring Security. Add the spring-boot-starter-
security dependency and configure security settings in a security configuration class extending
WebSecurityConfigurerAdapter.

Q13: What are some common Spring Boot annotations?

A13:

• @SpringBootApplication: Combines @Configuration, @EnableAutoConfiguration, and


@ComponentScan.

• @RestController: Combination of @Controller and @ResponseBody.

• @RequestMapping: Maps HTTP requests to handler methods.

• @Entity: Marks a class as a JPA entity.

• @Autowired: Injects bean dependencies.

Advanced Configuration

Q14: How do you externalize configuration in Spring Boot?

A14: Spring Boot allows you to externalize configuration using the application.properties or
application.yml file, environment variables, and command-line arguments. Additionally, you can use
@Value to inject property values into Spring beans.

Example using application.properties:

properties

app.message=Hello World

Injecting property value:

java

@Value("${app.message}")
private String message;

Q15: How do you profile specific configurations in Spring Boot?

A15: Spring Boot allows you to create different configuration files for different profiles. You can
activate profiles using the spring.profiles.active property.

Example:

• application-dev.properties

• application-prod.properties

Activate a profile:

properties

spring.profiles.active=dev

Dependency Management

Q16: How does Spring Boot manage dependencies?

A16: Spring Boot uses a parent POM (spring-boot-starter-parent) to manage dependencies and their
versions. This parent POM provides a default version for every dependency, ensuring compatibility
and reducing the need for explicit version management in your project's POM file.

Security and Authentication

Q17: How do you implement OAuth2 in a Spring Boot application?

A17: To implement OAuth2 in a Spring Boot application, you can use the spring-boot-starter-oauth2-
client dependency. Configure your OAuth2 client settings in the application.properties file and use
Spring Security's OAuth2 login mechanism.

Example configuration:

properties

spring.security.oauth2.client.registration.my-client.client-id=my-client-id

spring.security.oauth2.client.registration.my-client.client-secret=my-client-secret

spring.security.oauth2.client.registration.my-client.scope=read,write

spring.security.oauth2.client.registration.my-client.authorization-grant-type=authorization_code

spring.security.oauth2.client.registration.my-client.redirect-
uri={baseUrl}/login/oauth2/code/{registrationId}

spring.security.oauth2.client.provider.my-client.authorization-uri=https://auth-server/authorize

spring.security.oauth2.client.provider.my-client.token-uri=https://auth-server/token

spring.security.oauth2.client.provider.my-client.user-info-uri=https://auth-server/user
Actuator and Monitoring

Q18: How do you customize Spring Boot Actuator endpoints?

A18: You can customize Spring Boot Actuator endpoints by modifying the application.properties file
or creating a custom security configuration. You can change endpoint exposure, base path, and
security settings.

Example configuration:

properties

management.endpoints.web.exposure.include=health,info

management.endpoint.health.show-details=always

management.endpoints.web.base-path=/manage

Performance and Optimization

Q19: How do you enable caching in a Spring Boot application?

A19: To enable caching in a Spring Boot application, you need to add the spring-boot-starter-cache
dependency and annotate your main application class with @EnableCaching. Use @Cacheable,
@CachePut, and @CacheEvict annotations to manage caching on methods.

Example:

java

@SpringBootApplication

@EnableCaching

public class MyApplication {

public static void main(String[] args) {

SpringApplication.run(MyApplication.class, args);

@Service

public class MyService {

@Cacheable("items")

public Item getItemById(Long id) {

return repository.findById(id).orElse(null);
}

Miscellaneous

Q20: How do you handle exceptions in Spring Boot?

A20: Spring Boot provides several ways to handle exceptions:

• Controller-based: Using @ExceptionHandler within a controller.

• Global Exception Handling: Using @ControllerAdvice to handle exceptions globally.

Example:

java

@ControllerAdvice

public class GlobalExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)

public ResponseEntity<String> handleResourceNotFoundException(ResourceNotFoundException


ex) {

return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);

Q21: How do you create a custom starter in Spring Boot?

A21: To create a custom starter:

1. Create a new Maven project.

2. Define the necessary dependencies in the pom.xml.

3. Implement auto-configuration classes annotated with @Configuration and conditional


annotations (@ConditionalOnClass, @ConditionalOnMissingBean).

4. Register your auto-configuration class in spring.factories file under META-INF.

Example spring.factories:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.example.autoconfiguration.MyAutoConfiguration

Q22: How do you configure a Spring Boot application to use multiple data sources?

A22: Configure multiple data sources by defining multiple DataSource beans and specifying the
configuration properties for each data source.
Example:

java

@Configuration

public class DataSourceConfig {

@Primary

@Bean(name = "dataSource1")

@ConfigurationProperties(prefix = "datasource1")

public DataSource dataSource1() {

return DataSourceBuilder.create().build();

@Bean(name = "dataSource2")

@ConfigurationProperties(prefix = "datasource2")

public DataSource dataSource2() {

return DataSourceBuilder.create().build();

Q23: How do you create a REST API with Spring Boot?

A23: To create a REST API with Spring Boot:

1. Use the spring-boot-starter-web dependency.

2. Define a controller class annotated with @RestController.

3. Use @RequestMapping or @GetMapping, @PostMapping, etc., to map HTTP requests to


handler methods.

Example:

java

@RestController

@RequestMapping("/api")

public class MyController {


@GetMapping("/items")

public List<Item> getAllItems() {

return itemService.getAllItems();

@PostMapping("/items")

public Item createItem(@RequestBody Item item) {

return itemService.createItem(item);

You might also like