官术网_书友最值得收藏!

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:

Figure 1: Java Application with Spring IOC Container

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);
}
}
主站蜘蛛池模板: 榆林市| 柏乡县| 宜宾市| 张家口市| 巴彦县| 特克斯县| 留坝县| 铜陵市| 商洛市| 株洲市| 东源县| 玉环县| 新津县| 崇礼县| 阳城县| 万宁市| 西和县| 佛学| 天峨县| 永宁县| 双辽市| 松溪县| 永昌县| 裕民县| 高平市| 健康| 社会| 望都县| 蓬溪县| 塔城市| 彰化县| 青龙| 迁安市| 商洛市| 东山县| 民县| 鹿邑县| 北川| 云浮市| 玉林市| 武夷山市|