Spring Boot Basic Interview Questions Part 1 1690952442
Spring Boot Basic Interview Questions Part 1 1690952442
Interview Questions
@Configuration:
This annotation indicates that the class
contains Spring bean configuration. Inside
a @Configuration class, you can define
beans using @Bean methods, and Spring
will manage those beans in the application
context.
@EnableAutoConfiguration:
This annotation enables Spring Boot's auto-
configuration feature. Auto-configuration
simplifies the configuration process, as Spring
Boot can automatically set up beans, data
sources, security, and other components based
on the application's environment.
@ComponentScan:
This annotation enables component scanning
within the specified package(s) and their sub-
packages. Component scanning allows Spring to
automatically detect and register Spring
components (e.g., @Component, @Service,
@Repository, @Controller, etc.) as beans in the
application context. It helps in automatically
discovering and wiring beans without the need
for explicit bean registration.
Define beans
Enable autoconfiguration
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
Project Configuration:
Developers can select various project
options, such as the build tool (Maven or
Gradle), programming language (Java or
Kotlin), and Spring Boot version.
Dependency Selection:
Spring Initializer offers a list of starter
dependencies that developers can choose
from. Starter dependencies are curated
sets of related dependencies for specific
functionalities like web applications, data
access, security, testing, and more.
Customization:
Developers can further customize the
generated project by modifying the
pom.xml (for Maven) or build.gradle (for
Gradle) to include additional dependencies
or make specific configuration changes.
@SpringBootApplication:
This is the main annotation used to
bootstrap a Spring Boot application.
It is a combination of three annotations:
@Configuration,
@EnableAutoConfiguration,
@ComponentScan.
@RestController:
This annotation is used to mark a class
as a RESTful controller in Spring Boot
applications.
It combines the @Controller and
@ResponseBody annotations, making it
convenient to create RESTful APIs that
return JSON or XML responses.
@RequestMapping:
This annotation is used to map HTTP
requests to controller methods in Spring
Boot applications.
It defines the URL path and the HTTP
method for which the method should
handle incoming requests.
@Autowired:
This annotation is used to perform
dependency injection in Spring Boot
applications.
It allows Spring to automatically wire
(inject) dependencies into a bean's
property, constructor, or setter method.
@Component:
This annotation marks a class as a Spring
component.
Components are auto-detected during
component scanning and registered in
the Spring application context as beans.
@Service:
This annotation is a specialization of
@Component used to mark a class as a
service in Spring Boot applications.
Services typically represent business
logic or services that can be injected
into other components.
@Repository:
This annotation is a specialization of
@Component used to mark a class as a
data repository in Spring Boot
applications.
It is commonly used for classes that
interact with the database through
Spring Data.
@Bean:
This annotation is used to define a bean
manually in a Spring Boot configuration
class.
The method annotated with @Bean
returns an object that is managed as a
bean by Spring.
@Controller:
The @Controller annotation is a
fundamental annotation in the Spring
Framework used to mark a class as a
controller in a web application.
@RequestMapping:
@RequestMapping is a general-purpose
annotation used to map HTTP requests
to controller methods in Spring
applications.
@RequestMapping:
@Controller
public class MyController {
@RequestMapping(value="/hello", method=
RequestMethod.GET)
public String hello() {
return "hello"; // Returns the view name "hello" to
be rendered as HTML
}
}
@GetMapping:
@GetMapping is a specialized version of
@RequestMapping introduced in Spring
4.3.
@GetMapping:
@Controller
public class MyController {
@GetMapping("/hello")
public String hello() {
return "hello"; // Returns the view name
"hello" to be rendered as HTML
}
}
Constructor Injection:
Dependencies are provided through the
class constructor.
This is the most common type of
dependency injection.
Setter Injection:
Dependencies are provided through the
setter methods.
Interface Injection:
Rarely used
Dependent class implements an
interface that defines methods for
setting its dependencies.