Explain in detail the special injection function of Spring annotation (ListMap)

  • 2021-12-13 16:40:52
  • OfStack

Explanation of Spring annotations (List & Map) Special injection function

Recently, I took over a new project, which was no longer maintained by the original developer. The project framework is developed based on spring boot. Two of the Spring annotations took a lot of time to figure out exactly how to use them, which also involved a special injection function of spring annotations.

First of all, you can see that there are 1 List and 1 Map directly injected into the code. The sample code is as follows:


@Autowired
private List<DemoService> demoServices;

@Autowired
private Map<String,DemoService> demoServiceMap;

The above is demo after two code examples. At that time, I was a little confused after seeing this. After the global search, I didn't find an object defining an List and an Map. However, after debug runs, it is found that they all have value. This thing is somewhat magical. Searching on the Internet also yields little.

Finally, when debugging List, I suddenly got a flash of inspiration. If there is only one object, then there is only one value in List. So I began to test and verify, and found that it was true. After instantiating one DemoService, another class injects List with generics, and Spring successfully puts the instantiated object into List. After the train of thought is opened, it is better to say for Map. Spring encapsulates the name of service as key and the object as value into Map.

The specific example code is as follows

DemoService code:


package com.secbro.learn.service;

import org.springframework.stereotype.Service;

/**
 * Created by zhuzs on 2017/5/8.
 */
@Service
public class DemoService {

  public void test(){
    System.out.println(" I've been called ");
  }

}

DemoController code:


package com.secbro.learn.controller;

import com.secbro.learn.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by zhuzs on 2017/5/8.
 */
@Controller
@RequestMapping(value = "/demo")
public class DemoController {

  @Autowired
  private List<DemoService> demoServices;

  @Autowired
  private Map<String,DemoService> demoServiceMap;

  @ResponseBody
  @RequestMapping(value = "/test")
  public String test(){
    for(Map.Entry<String,DemoService> entry : demoServiceMap.entrySet()){
      entry.getValue().test();
    }
    System.out.println("=============== Partition line =============");
    for(DemoService demoService : demoServices){
      demoService.test();
    }

    return "success";
  }
}

After running, visit http://localhost: 8080/demo/test and the execution results are as follows:


 I've been called 
=============== Partition line =============
 I've been called 

It turns out that Spring has helped us do a lot of things before we know it, but we don't know it.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: