Spring Core Module Study Notes
Spring Core Module Study Notes
A Spring Bean is essentially a Java object that is managed by the Spring Framework's Inversion
of Control (IoC) container. Unlike traditional Java where objects are created manually using the
`new` keyword, Spring automatically creates and manages these objects. To define a class as a
Spring Bean, annotations like @Component are used. When Spring starts, it creates instances
of these classes and manages their lifecycle, making them available for dependency injection
throughout the application.
Key Point: A Bean is an object created and managed by the Spring container, facilitating loose
coupling and easier management of dependencies.
The core of Spring is the Inversion of Control (IoC) container, which is responsible for creating,
configuring, and managing beans. The two main types of containers are:
This setup allows Spring to manage bean creation and dependency resolution automatically.
IoC means that the control of object creation and dependency management is transferred from
the programmer to the Spring container. In traditional Java, developers manually instantiate
objects, but with IoC, Spring handles this process, leading to more modular and testable code.
Spring IoC: Spring creates and manages objects, promoting loose coupling.
Dependencies and Dependency Injection (DI)
A dependency occurs when one class requires another class to function. For example, a Car
class depends on an Engine.
class Car {
Engine engine;
}
1. Constructor Injection: Dependencies are provided through class constructors. Ideal for
mandatory dependencies.
2. class Car { Engine engine; Car(Engine engine){ [Link] =
engine; } }
3. Setter Injection: Dependencies are injected via setter methods. Suitable for optional
dependencies.
4. class Car { Engine engine; public void setEngine(Engine engine){
[Link] = engine; } }
5. Field Injection: Dependencies are injected directly into class variables using
annotations like @Autowired. Not recommended for production due to poor testability.
6. @Autowired Engine engine;
Spring also supports arbitrary method injection, where dependencies are injected through any
method, but this is rarely used.
Example:
@Service
public class UserService {
}
@Autowired
@Autowired
Engine engine;
@Value
@Value("Prashant")
String name;
Or from properties:
@Value("${[Link]}")
@ComponentScan
@ComponentScan("[Link]")
@PropertySource
@PropertySource("classpath:[Link]")
@Configuration
public class AppConfig {
@Bean
public Engine engine() {
return new Engine();
}
}
@Autowired
@Qualifier("dieselEngine")
Engine engine;
This allows importing traditional XML bean definitions into Java config.
@Scope("prototype")
@Lazy
Creates beans only when they are first needed, improving startup performance.
@Lazy
@Component
class Car {}
**Simple Meaning:**
At its simplest, Spring provides a way to manage objects—called beans—by controlling their
instantiation and dependencies automatically, reducing the need for manual object creation and
wiring.
**Bean:**
A bean is essentially an object that is managed by the Spring IoC container. These beans are
created, configured, and maintained by Spring, allowing developers to focus on business logic
rather than object lifecycle management.
**@Autowired:**
This annotation is used to inject dependencies into a bean. When placed on a field, constructor,
or setter method, Spring automatically wires the appropriate bean into that location, based on
type or qualifier.
**@Value:**
The @Value annotation is used to inject simple values, such as strings, numbers, or other
primitive data types, into a bean's property. It allows for externalized configuration, such as
values from properties files.
**@Bean:**
This annotation is used within a configuration class to explicitly define a bean. Methods
annotated with @Bean produce a bean that is managed by Spring, allowing for custom
configuration and instantiation logic.
**@Scope:**
The @Scope annotation defines the lifecycle scope of a bean. Common scopes include
singleton (default, one shared instance), prototype (a new instance each time), request,
session, and application scopes, depending on the application's needs.
**@Lazy:**
The @Lazy annotation indicates that a bean should be created only when it is first needed,
rather than at application startup. This can improve startup time and resource utilization by
delaying bean creation until necessary. ---