Spring - Core Flashcards

1
Q

What are the main features of the Spring Framework?

Core Spring

A

Dependency Injection (DI):
Core to Spring, it manages object creation and wiring, promoting loose coupling and easier testing.

Aspect-Oriented Programming (AOP):
Enables separation of cross-cutting concerns (like logging, security, transactions) from business logic.

Modular Architecture:
Though comprehensive, Spring allows you to use only the modules you need (e.g., Spring Core, Spring MVC, Spring Data).

Declarative Transaction Management:
Supports transactions through annotations or XML, simplifying the management of transactions.

Integration with Other Frameworks:
Easily integrates with ORM tools like Hibernate, JPA, and others.

Testability:
Encourages good design practices that make unit and integration testing easier.

Lightweight Container:
Unlike heavy Java EE containers, Spring can run in any environment (e.g., standalone, servlet container).

Consistent Programming Model:
Unified approach across various modules, reducing learning curve.

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

Why is Spring Common in Java Applications?

Core Spring

A
  • Reduces boilerplate code.
  • Encourages good design principles (IoC, separation of concerns).
  • Flexible and non-intrusive.
  • Large ecosystem and community support.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Inversion of Control (IoC)?

Core Spring

A

IoC is a design principle where the control of object creation and dependency management is transferred from the application code to a container or framework.

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

How does Spring implement IoC?

Core Spring

A

Spring implements IoC through Dependency Injection (DI). The framework is responsible for instantiating, configuring, and managing the lifecycle of application objects (beans).

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

What types of DI support Spring?

Core Spring

A

Constructor Injection:
Dependencies are provided through the constructor.

Setter Injection:
Dependencies are set via public setter methods.

Field Injection (not recommended in some cases):
Spring injects dependencies directly into fields (via reflection, often using @Autowired).

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

BeanFactory vs. ApplicationContext

Core Spring

A

They are IoC Containers in Spring.

BeanFactory

  • Basic IoC container in Spring.
  • Provides fundamental dependency injection support.
  • Lazy initialization: Beans are created only when requested.
  • Lightweight and suitable for simple or memory-constrained applications.
    Interface: org.springframework.beans.factory.BeanFactory.

ApplicationContext

  • A superset of BeanFactory with more advanced features.
  • Eager initialization: Beans are created at startup by default.
  • Provides:
    – Internationalization support (i18n)
    – Event propagation
    – AOP integration
    – Declarative mechanisms (e.g., annotation processing, automatic bean registration)
    Interface: org.springframework.context.ApplicationContext.

In Practice

ApplicationContext is preferred for most enterprise applications due to its rich feature set.

BeanFactory is rarely used directly unless in specific performance-sensitive scenarios (like mobile or embedded systems).

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

What are the different bean scopes available in the Spring Framework?

Core Spring

A

Singleton:
* Default scope in Spring
* Only one instance.
* Shared across all requests and components.
* Used for stateless services or shared components.

Prototype:
* A new instance is created each time the bean is requested.
* Useful for stateful beans or objects with short lifespans.
* Requires careful lifecycle management (Spring doesn’t manage destruction).

Request (Web-aware context):
* One bean instance per HTTP request.
* Used for request-scoped data in web applications.
* Requires Spring Web module.

Session (Web-aware context):
* One bean instance per HTTP session.
* Useful for user-specific data across multiple requests.

Application:
* One bean instance per ServletContext.
* Shared across all sessions and requests.
* Suitable for app-wide data or cache holders.

Websocket:
* One bean per WebSocket session.
* Useful in WebSocket-based communication scenarios.

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

Can you describe the lifecycle of a Spring bean?

Core Spring

A

TBD

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

What are the key lifecycle callback interfaces and annotations provided by Spring?

Core Spring

A

TBD

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

What are the different ways to configure beans in Spring?

Core Spring

A

XML-Based Configuration
Beans are defined in .xml files using <bean> tags.
Example: applicationContext.xml</bean>

Annotation-Based Configuration
Uses annotations like @Component, @Service, @Repository, and @Autowired.
Requires component scanning via @ComponentScan.

Java-Based Configuration
Uses @Configuration classes and @Bean methods to define beans explicitly in code.

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

What is the difference between @Component and @Bean?

Core Spring

A

@Component
* Used to mark a class as a Spring-managed bean.
* Spring automatically detects it via component scanning (@ComponentScan).

Variants:
@Service – for service-layer classes
@Repository – for DAO/persistence
@Controller – for web controllers

Use When:
* You control the source code.
* The class is part of your application logic and should be auto-detected.

======

@Bean
* Used to define a bean method inside a @Configuration class.
* Spring registers the return object of the method as a bean.

Use When:
* You need to configure beans from third-party libraries (no annotations).
* You want full control over bean creation logic (e.g., conditionally creating instances).

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

What is lazy initialization in Spring, and how does it differ from eager initialization?

Core Spring

A

Eager Initialization (Default in Spring):
Beans are created at application startup.
All singleton beans are instantiated as soon as the application context is loaded.

Pros:
Fails fast — issues in bean wiring are detected early.
Ready to use immediately after startup.

Cons:
Increases startup time.
Unused beans still consume resources.

====

Lazy Initialization:
Beans are created only when first requested (i.e., at the point of actual use).

How to Enable:
@Lazy on a class → applies to all beans in it.
@Lazy on a field → delays injection of that dependency.

Pros:
Faster startup time.
Useful for rarely used or heavy beans.

Cons:
Errors in bean setup are only caught at runtime.
Slightly longer response on first usage.

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