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

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);
}
}
主站蜘蛛池模板: 长葛市| 沙洋县| 新蔡县| 叶城县| 门源| 东阿县| 莱芜市| 鹰潭市| 正阳县| 武川县| 衡南县| 日喀则市| 南岸区| 阳江市| 岳阳市| 四川省| 淮滨县| 广德县| 油尖旺区| 汤阴县| 秭归县| 沁阳市| 正阳县| 乌兰浩特市| 永年县| 屯门区| 涞源县| 溧阳市| 乌兰浩特市| 雷州市| 顺平县| 雷州市| 莱州市| 句容市| 汶上县| 钟山县| 新河县| 石柱| 二手房| 江门市| 南郑县|