Detailed description of Spring annotation (List & Map) Special injection function, spring Annotation
Detailed description of special injection functions of Spring annotations (List & Map)
I recently took over a new project and no longer maintained by the original developer. The project framework is developed based on spring boot. Two Spring annotations take a lot of time to figure out how to use them. This also involves a special injection function of spring annotations.
First, we can see that a List and a Map are directly injected into the code. The sample code is as follows:
@Autowiredprivate List<DemoService> demoServices;@Autowiredprivate Map<String,DemoService> demoServiceMap;
The above is the demo after the two sample codes. At that time, we can see some samples. After global search, no List and Map objects are defined. However, after debug is run, it is found that they all have values. This is amazing. Searching on the Internet also has little gains.
Finally, when debugging the List, the idea suddenly flashed. If there was only one object, wouldn't there be only one value in the List. So we started testing and verification and found that it was true. After instantiating a DemoService, another class uses generic injection List. Spring successfully puts the instantiated object into the List. After the idea is opened, we can better talk about Map. Spring uses the service name as the key, and the object is encapsulated into the Map as the value.
The sample code is as follows:
DemoService code:
Package com. secbro. learn. service; import org. springframework. stereotype. service;/*** Created by zhuzs on April /5/8. * // @ Servicepublic class DemoService {public void test () {System. out. println ("I was 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. * // @ 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 ("========================"); for (DemoService demoService: demoServices) {demoService. test () ;}return "success ";}}
After running, access http: // localhost: 8080/demo/test and the execution result is as follows:
I was called ================================ I was called
It turns out that Spring has helped us do a lot without knowing it.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!