Spring Boot is a framework for developing Java-based enterprise applications, helping to develop web applications quickly and easily. At its core is the Spring container, and the concept of beans is a key element of Spring applications. In this lecture, we will take a closer look at beans and the Spring container.
1. Overview of Spring Framework
The Spring framework is an application framework for the Java platform that allows developers to manage the lifecycle of beans and connect components through Dependency Injection. This reduces software coupling, improves testability, and enhances maintainability.
2. Spring’s Container
The Spring container is a crucial part that manages the components of an application. It is responsible for object creation, configuration, and dependency injection. The two main containers of Spring are:
- BeanFactory: The most basic container, which creates objects using lazy loading.
- ApplicationContext: It includes all the functionalities of BeanFactory and provides more features. For example, it supports internationalization, event propagation, and various application components through layers.
3. Concept of Bean
In Spring, a bean refers to an object managed by the Spring container. Beans are generally necessary for executing the business logic of the application. Beans can be defined in XML files or Java configuration files.
When defining a bean, the following elements should be considered:
- scope: Defines the lifecycle of the bean. Common scopes include singleton, prototype, request, session, and global session.
- qualifier: Provides a method to select a specific bean when multiple beans are available.
- autowiring: The method by which the Spring container automatically injects the dependencies of the bean.
4. Bean Lifecycle
Spring can manage the lifecycle of beans in various states. Let’s examine the process from the creation to the destruction of a bean.
- Bean Instantiation: The Spring container creates the bean object.
- Dependency Injection: Injects the required dependencies.
- Initialization: The initialization method is called. At this stage, the application is set up so that the bean is ready for use.
- Usage: The bean is used to handle requests or perform specific tasks.
- Destruction: The bean is destroyed when the application is terminated. At this stage, necessary cleanup tasks are performed.
5. Bean Definition
There are several ways to define a bean, but XML configuration files and Java-based configuration files are mainly used. Let’s explore each method.
5.1 Defining Beans with XML Configuration Files
The method for defining beans in an XML configuration file is as follows:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="exampleBean" class="com.example.Example"> <property name="propertyName" value="propertyValue"/> </bean> </beans>
5.2 Java-based Configuration Method
When defining a bean through Java-based configuration, you can use the @Configuration and @Bean annotations.
@Configuration public class AppConfig { @Bean public Example exampleBean() { return new Example("propertyValue"); } }
6. Dependency Injection
Dependency injection is a way to establish and manage the relationships between objects, and there are three methods of injection.
- Constructor Injection: Dependency is injected through the constructor of the bean.
- Setter Injection: Dependency is injected through setter methods.
- Field Injection: Dependency is injected directly into the fields. However, this method is not recommended.
6.1 Constructor Injection Example
public class SampleBean { private final DependencyBean dependency; public SampleBean(DependencyBean dependency) { this.dependency = dependency; } }
6.2 Setter Injection Example
public class SampleBean { private DependencyBean dependency; @Autowired public void setDependency(DependencyBean dependency) { this.dependency = dependency; } }
7. Defining Scope of Beans
Spring allows you to set scopes to manage the lifecycle of beans effectively. Below are the characteristics of each scope.
- singleton: The default scope, only one instance per application is created.
- prototype: A new instance is created every time it is requested.
- request: One instance is created per HTTP request in a web application.
- session: One instance is created per HTTP session.
- globalSession: An instance shared across all sessions in a portal application.
8. Initializing Spring Container
Before starting a Spring application, the Spring container must be initialized. Typically, this is done by calling the SpringApplication.run() method.
public static void main(String[] args) { SpringApplication.run(Application.class, args); }
9. Bean Profiles
Spring supports profiles to allow the configuration of beans based on the environment. Using profiles, you can maintain bean settings suitable for development, testing, and production environments.
@Profile("dev") @Bean public DataSource devDataSource() { return new HikariDataSource(); } @Profile("prod") @Bean public DataSource prodDataSource() { return new HikariDataSource(); }
10. Conclusion
Spring Boot is a powerful framework that supports backend application development, and understanding beans and the Spring container is essential for comprehending the structure and operation of applications. In this lecture, we discussed bean definitions, lifecycles, dependency injection, scopes, and initialization methods. We look forward to learning more in future lectures and developing real applications using Spring Boot.