- Building Web Apps with Spring 5 and Angular
- Ajitesh Shukla
- 373字
- 2021-07-02 19:38:18
What is a Spring IOC container?
A Spring IOC container is a framework which, basically, manages the life cycle of plain old Java objects (POJOs), and injects them into the application as required. Java objects define their dependencies using one of the following methods:
- Dependencies are passed as arguments to the constructor method of the object. See how the object is passed as an argument to the constructor method in the example cited in the previous section.
- Dependencies are passed as arguments to the setter method of the object.
- Dependencies are passed as arguments to a factory method of the object.
A Spring IOC container injects the dependencies after it creates the beans. Note the fact that dependencies are no longer managed by Java objects. They are rather managed and injected by the framework, and hence, Inversion of Control.
The following are the packages which are core to the IOC container:
- org.springframework.beans
- org.springframework.context
It is the interface, org.springframework.context.ApplicationContext (sub-interface of the interface, BeanFactory), which represents the Spring IOC container and is responsible for managing the life cycle of beans. The instructions related to creating, configuring, and assembling the objects is termed Configuration Metadata. The configuration metadata is often represented using Java annotations or XML. A Java application using Spring IOC Container is created by combining Business POJOs with the previously mentioned configuration metadata, and passing it on to the IOC Container (an instance of ApplicationContext). The same is represented using the following diagram:

Let's illustrate the preceding diagram with an example. The following code represents how a service, namely, UserService is instantiated using Spring IOC container in a Spring Boot web application. Notice how the annotation-based autowiring feature has been used to have ApplicationContext autowired to the userService field in this code:
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.services.UserService;
import com.services.domain.User;
@Controller
@SpringBootApplication (scanBasePackages={"com.services"})
public class DemoApplication {
@Autowired
private UserService userService;
@RequestMapping("/")
@ResponseBody
String home() {
User user = null;
return "Hello " + userService.getUsername(user) + ". How are you?";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 數(shù)據(jù)庫系統(tǒng)教程(第2版)
- Oracle從新手到高手
- ASP.NET Core Essentials
- JavaScript by Example
- Modular Programming in Java 9
- JavaScript 程序設(shè)計(jì)案例教程
- 組態(tài)軟件技術(shù)與應(yīng)用
- SQL基礎(chǔ)教程(第2版)
- C語言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo) (第2版)
- SQL Server 2016 從入門到實(shí)戰(zhàn)(視頻教學(xué)版)
- RocketMQ實(shí)戰(zhàn)與原理解析
- Getting Started with Python
- Android移動(dòng)應(yīng)用開發(fā)項(xiàng)目教程
- Java RESTful Web Service實(shí)戰(zhàn)
- 體驗(yàn)之道:從需求到實(shí)踐的用戶體驗(yàn)實(shí)戰(zhàn)