Week 9 Flashcards

1
Q

What is Spring Framework?

A

Spring Framework is a comprehensive Java-based framework that provides infrastructure support for developing Java applications. It simplifies the development of enterprise-level applications by offering features like dependency injection, aspect-oriented programming, and transaction management.

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

What are the core features of Spring?

A

Inversion of Control (IoC) / Dependency Injection (DI)
Aspect-Oriented Programming (AOP)
Transaction management
Data access with JDBC support
MVC (Model-View-Controller) web framework
Integration with various frameworks (Hibernate, JPA, etc.)

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

What is the primary purpose of the Spring Framework?

A

To simplify enterprise Java development by managing dependencies, providing a flexible architecture, and supporting various components such as transaction management, web frameworks, and data access.

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

What is the Spring IoC (Inversion of Control) container?

A

The IoC container in Spring is responsible for managing the lifecycle and configuration of application objects (beans). It allows objects to be created, injected, and managed without tight coupling between them.

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

What are the two types of IoC containers in Spring?

A

BeanFactory
ApplicationContext

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

What is Inversion of Control (IoC)?

A

IoC is a design principle where the control of creating objects and managing dependencies is given to the framework instead of being handled by the application code.

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

How does IoC improve application modularity?

A

By decoupling the creation of objects from their use, IoC makes components more reusable, testable, and easier to maintain.

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

What is Dependency Injection (DI)?

A

DI is a technique in which an object receives its dependencies (other objects) from an external source, typically a container or framework, rather than creating them itself.

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

What are the benefits of Dependency Injection?

A

Decoupling components
Easier testing and mocking
Clear dependency management
Improved code modularity

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

What are the three types of Dependency Injection?

A

Constructor Injection
Setter Injection
Field Injection

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

What is Constructor Injection?

A

Constructor Injection involves passing dependencies to an object through its constructor.

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

What is Setter Injection?

A

Setter Injection involves passing dependencies through setter methods after the object is created.

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

What is Field Injection, and when is it used?

A

Field Injection involves injecting dependencies directly into fields of a class using annotations like @Autowired. It is less preferred because it hides dependencies and is harder to test.

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

How do you define a bean in Spring using XML configuration?

A

A bean can be defined in an XML file using the <bean> tag, specifying the class name and optionally providing constructor arguments or property values.</bean>

<bean>
<property></property>
</bean>

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

How do you inject dependencies using XML configuration?

A

By using the <property> tag for setter injection and the <constructor-arg> tag for constructor injection.</constructor-arg></property>

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

What is the purpose of the <constructor-arg> element in XML configuration?</constructor-arg>

A

It defines constructor arguments to be passed when a bean is instantiated using constructor injection.

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

What is Java-based configuration in Spring? .

A

Java-based configuration is an alternative to XML configuration where beans and dependencies are defined using @Configuration and @Bean annotations in Java classes

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

How do you define a bean using Java-based configuration?

A

You use the @Bean annotation inside a class annotated with @Configuration.

@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}

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

How do you inject dependencies in Spring using Java-based configuration?

A

You define beans in a @Configuration class and Spring will manage the injection based on the @Bean methods or @Autowired annotation.

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

What is annotation-based configuration in Spring?

A

Annotation-based configuration uses annotations like @Component, @Autowired, and @Configuration to configure beans and inject dependencies without XML.

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

How is @Autowired used for Dependency Injection?

A

The @Autowired annotation marks a field, setter, or constructor to indicate that Spring should automatically inject the required dependency.

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

What is the purpose of @ComponentScan in annotation-based configuration?

A

@ComponentScan is used to specify the packages that Spring should scan to find and register beans automatically based on annotations like @Component, @Service, and @Controller.

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

What is component scanning in Spring?

A

Component scanning is the process by which Spring automatically detects and registers beans defined by annotations like @Component.

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

How do you enable component scanning in a Spring application?

A

You enable component scanning by using the @ComponentScan annotation or by specifying a <context:component-scan> element in XML configuration.</context:component-scan>

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

What package will Spring scan by default when component scanning is enabled?

A

Spring will scan the package of the class that declares the @ComponentScan annotation and its sub-packages by default.

23
Q

What are stereotype annotations in Spring?

A

Stereotype annotations like @Component, @Service, @Repository, and @Controller are used to indicate different types of Spring-managed components.

23
Q

What is the difference between @Component, @Service, @Repository, and @Controller annotations?

A

@Component: Generic annotation for any Spring-managed component.
@Service: Indicates a service layer component.
@Repository: Marks a data access object (DAO) component.
@Controller: Defines a web controller in Spring MVC.

24
Q

What is a bean in Spring?

A

A bean is an object that is instantiated, configured, and managed by the Spring IoC container.

24
Q

How is a bean instantiated in the Spring IoC container?

A

Beans are instantiated by the IoC container either by invoking the constructor or through factory methods defined in configuration files.

25
Q

What are the main stages of the Spring bean lifecycle?

A

Instantiation
Dependency Injection
Initialization (after properties are set)
Post-initialization (via @PostConstruct)
Destruction (via @PreDestroy)

26
Q

What are the @PostConstruct and @PreDestroy annotations used for?

A

@PostConstruct: Marks a method to be executed after the bean’s initialization.
@PreDestroy: Marks a method to be executed before the bean’s destruction.

27
Q

What are the different scopes of a Spring bean?

A

Singleton
Prototype
Request
Session
Application

27
Q

What is the default bean scope in Spring?

A

The default scope is singleton, meaning one instance per Spring IoC container.

28
Q

What is the difference between singleton and prototype bean scopes?

A

Singleton: Only one instance of the bean is created and shared across the container.
Prototype: A new instance is created each time the bean is requested.

29
Q

What is Lombok, and how does it simplify Java code?

A

Lombok is a Java library that automatically generates boilerplate code such as getters, setters, constructors, and more using annotations, reducing the amount of code developers need to write.

30
Q

What are some common Lombok annotations used in Spring development?

A

@Getter and @Setter
@NoArgsConstructor and @AllArgsConstructor
@Data (combines multiple annotations)
@Builder

31
Q

What is Spring Boot?

A

Spring Boot is a framework that simplifies Spring application development by providing pre-configured templates, auto-configuration, and an embedded server.

32
Q

What are the advantages of using Spring Boot?

A

Simplifies configuration
Embedded servers (Tomcat, Jetty)
Rapid application development
Pre-configured templates and starters

33
Q

How does Spring Boot simplify application development?

A

Spring Boot uses convention over configuration and auto-configuration to eliminate the need for extensive setup, letting developers focus on building business logic.

34
Q

What is Spring Initializr?

A

Spring Initializr is an online tool that allows developers to quickly generate a Spring Boot project with the necessary dependencies and configuration.

35
Q

How can you create a new Spring Boot project using Spring Initializr?

A

By visiting the Spring Initializr website, selecting the required project dependencies, and generating the project structure.

36
Q

What is auto-configuration in Spring Boot?

A

Auto-configuration automatically configures Spring beans based on the classpath and application properties, reducing manual configuration.

37
Q

How does Spring Boot’s auto-configuration work?

A

Spring Boot scans the classpath for certain dependencies and automatically configures components like databases, web servers, and other services based on default settings.

37
Q

What are Spring Boot starters?

A

Spring Boot starters are pre-packaged dependency sets that include common libraries for particular functionalities (e.g., spring-boot-starter-web, spring-boot-starter-data-jpa).

38
Q

What are some common Spring Boot starters, and what do they include?

A

spring-boot-starter-web: Includes Spring MVC and embedded Tomcat/Jetty.
`spring

39
Q

What is Spring MVC?

A

Spring MVC is a framework within the Spring Framework that provides a structured way to develop web applications following the Model-View-Controller design pattern. It separates the application into three main components: the Model (business logic), the View (UI), and the Controller (handles requests and responses).

40
Q

What is the architecture of Spring MVC?

A

The architecture of Spring MVC consists of several components:

DispatcherServlet: The front controller that handles incoming requests.
Controller: Processes user requests and returns a response.
Model: Contains the application data and business logic.
View: The user interface that presents the model data.
View Resolver: Resolves the view names returned by controllers to actual views.

41
Q

How does the DispatcherServlet work in Spring MVC?

A

The DispatcherServlet intercepts incoming HTTP requests and delegates them to the appropriate controllers based on URL mappings. It also manages the workflow of request processing, including invoking controllers, selecting views, and rendering responses.

41
Q

What is Spring Boot DevTools?

A

Spring Boot DevTools is a module that enhances the development experience by providing features like automatic application restarts, live reload of resources, and improved error messages. It helps developers see changes in real-time without needing to restart the application manually.

42
Q

How does Spring Boot DevTools improve developer productivity?

A

It allows developers to make code changes and see the effects immediately without restarting the entire application. Features like live reload automatically refresh the browser when static resources change, speeding up the development process.

43
Q

What are environments in Spring Boot?

A

Environments refer to different configurations that can be set up for various stages of application development, such as development, testing, and production. They allow developers to manage environment-specific properties and settings.

44
Q

How can you define environment-specific properties in Spring?

A

You can define environment-specific properties by creating separate configuration files for each environment (e.g., application-dev.properties, application-prod.properties) and using the spring.profiles.active property to specify the active profile.

45
Q

What is the purpose of the @Controller annotation in Spring MVC?

A

The @Controller annotation is used to indicate that a class serves as a controller in the Spring MVC framework. It marks the class as a candidate for handling HTTP requests and returning appropriate responses.

46
Q

What is @RequestMapping used for in Spring MVC?

A

The @RequestMapping annotation is used to map HTTP requests to specific handler methods in a controller. It can specify the HTTP method, path, and other attributes to determine which method should handle a particular request.

47
Q

What is @ResponseBody used for in Spring MVC?

A

The @ResponseBody annotation is used to indicate that the return value of a controller method should be written directly to the HTTP response body. It allows for the return of JSON, XML, or other types of data instead of rendering a view.

48
Q

What are the different HTTP method annotations in Spring (e.g., @GetMapping, @PostMapping)?

A

@GetMapping: Used to handle HTTP GET requests.
@PostMapping: Used to handle HTTP POST requests.
@PutMapping: Used to handle HTTP PUT requests.
@DeleteMapping: Used to handle HTTP DELETE requests.
@PatchMapping: Used to handle HTTP PATCH requests.

49
Q

How can you retrieve request parameters in Spring MVC?

A

Request parameters can be retrieved in a controller method using the @RequestParam annotation. This annotation binds the value of a request parameter to a method parameter.

@GetMapping(“/greet”)
public String greet(@RequestParam String name) {
return “Hello, “ + name;
}

50
Q

What is the difference between request parameters and path variables?

A

Request parameters are key-value pairs sent in the URL query string (e.g., /api/users?id=1).
Path variables are dynamic segments in the URL that are part of the request URI (e.g., /api/users/{id}), allowing for cleaner and more readable URLs.

51
Q

What is the purpose of the @RequestBody annotation in Spring MVC?

A

The @RequestBody annotation is used to bind the HTTP request body to a method parameter in a controller. It allows for the automatic conversion of incoming JSON or XML data into a Java object.

@PostMapping(“/users”)
public ResponseEntity<User> createUser(@RequestBody User user) {
// Save the user
return ResponseEntity.ok(user);
}</User>

52
Q

What is ResponseEntity, and how is it used in Spring MVC?

A

ResponseEntity is a class that represents the entire HTTP response, including status code, headers, and body. It provides greater control over the response returned by a controller method, allowing developers to customize status codes, headers, and the response body.

@GetMapping(“/user/{id}”)
public ResponseEntity<User> getUser(@PathVariable Long id) {
User user = userService.findById(id);
return user != null ? ResponseEntity.ok(user) : ResponseEntity.notFound().build();
}</User>