- 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);
}
}
- 數據庫系統原理及MySQL應用教程(第2版)
- 精通Nginx(第2版)
- Getting Started with PowerShell
- Magento 1.8 Development Cookbook
- 學習正則表達式
- BIM概論及Revit精講
- Developing SSRS Reports for Dynamics AX
- Moodle 3 Administration(Third Edition)
- 大學計算機基礎實訓教程
- jQuery Mobile Web Development Essentials(Second Edition)
- 和孩子一起學編程:用Scratch玩Minecraft我的世界
- Leaflet.js Essentials
- Python程序設計現代方法
- MATLAB從入門到精通
- Mastering Wireless Penetration Testing for Highly Secured Environments