[Developer Notes] Preliminary study of Spring-ioc, is ioc a singleton?

Foreword:

  Inversion of Control (Inversion of Control, abbreviated as IoC) gives the right to create objects to the framework, which is an important feature of the framework, not a special term for object-oriented programming. It includes Dependency Injection (DI) and Dependency Lookup. When I first explored Spring, when I saw this sentence, I suddenly wondered whether IOC is actually an object creation tool for singleton mode. At the beginning of creation, Spring will analyze which beans need to be created according to the annotations in the configuration file and code, so the objects of this class are created into the cache one by one, and when the front-end request is received, the corresponding object is called to cache. Find it in the pool, so what does that mean? So I did a little test.

 

Experimental principle:

  1. Build the Spring framework so that it can perform dependency injection normally

  2. Inject an ordinary object A with properties into the container. This ordinary object A has a property with a getter and a setter

  3. Write a Controller that reads and writes the A object

  4. Call a write operation of the A object

  5. Call the read operation of the A object multiple times

  6. If the result obtained by calling the read operation of the A object multiple times is the result written in step 4, it proves that the result of the injection is the same as the result obtained by the singleton mode, and the experiment is successful. The result is not the result written in step 4, so the understanding is biased.

 

Experimental steps:

  1. Write the A object:

 

package com.wyb.serviceimpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wyb.dao.BookDao;
import com.wyb.dao.UserMapper;
import com.wyb.model.User;
import com.wyb.service.UserService;

@Service
public class UseServiceImpl implements UserService {
	
	private String name;
	@Autowired
	private UserMapper userMapper;

	public User findUserById(int id) {
		User user = userMapper.selectByPrimaryKey(id);
		return user;
	}

	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	public void setName(String name) {
		// TODO Auto-generated method stub
		this.name = name;
	}
	
	public String myToString(){
		return "IocTest:"+super.toString()+",this.name="+this.name;
	}
	
}

  

  2. Write the read and write operation Controller of the A object

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	private String list(Model model,HttpServletRequest request) {
		String name = request.getParameter("name");
		String result  = null;
		if(!"".equals(name)&&name != null){
			userService.setName(name);
			result = "success to set name,the value is: "+userService.getName();
		}else{
			result = userService.myToString();
		}
		
		return result;
	}

  

 

  3. Run the program, and call the read and write operations according to the steps.

    3.1 Write operation

  

    3.2 Read operation

  

 

Experimental results:

  The task of IOC is to create objects, which are public objects, singleton objects, created once and used everywhere.

  Personally, in terms of object creation, ioc is a singleton tool. Its function is to prevent programmers from creating new Service objects everywhere in the Controller, centrally manage them, allocate them uniformly, and reduce memory overhead.

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325294442&siteId=291194637