Questions
Questions
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.
A2:
• 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.
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
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.
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
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})
SpringApplication.run(MyApplication.class, args);
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.
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
A10: You can test Spring Boot applications using Spring's testing support. Common practices include:
Miscellaneous
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.
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.
A13:
Advanced Configuration
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.
properties
app.message=Hello World
java
@Value("${app.message}")
private String message;
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
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.
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
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
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
SpringApplication.run(MyApplication.class, args);
@Service
@Cacheable("items")
return repository.findById(id).orElse(null);
}
Miscellaneous
Example:
java
@ControllerAdvice
@ExceptionHandler(ResourceNotFoundException.class)
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
@Primary
@Bean(name = "dataSource1")
@ConfigurationProperties(prefix = "datasource1")
return DataSourceBuilder.create().build();
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "datasource2")
return DataSourceBuilder.create().build();
Example:
java
@RestController
@RequestMapping("/api")
return itemService.getAllItems();
@PostMapping("/items")
return itemService.createItem(item);