Spring Annotated (LIST&MAP) Special injection function

Source: Internet
Author: User

A new project has been recently taken over and has not been maintained by the original developer. The project framework is developed based on spring boot. Two of these spring annotations took a lot of time to figure out how it was used, and it also involved a special injection of spring annotations.

First, see the code that directly injects a list and a map. The sample code is as follows:

@Autowiredprivate List<DemoService> demoServices;@Autowiredprivate Map<String,DemoService> demoServiceMap;

The above is the demo after two code samples. I was a little confused when I saw this, and after the global search, we didn't find the object that defines a list and map. However, when debug runs, it turns out that they all have values. This thing is a little bit magical. Search on the internet has also gained little.

Finally, when debugging the list, suddenly the inspiration flashes, if there is only one object then the list of values there is only one. Then the test was tested and the results were found to be true. When a demoservice is instantiated, another class takes a generic injection list,spring successfully putting the instantiated object into the list. After the idea is opened, it's better to talk about the map. Spring will use the name of the service as the key and the object as the value package into the map.

The specific case code is as follows

Demoservice Code:

package com.secbro.learn.service;import org.springframework.stereotype.Service;/** * Created by zhuzs on 2017/5/8. */@Servicepublicclass DemoService {    publicvoid  test(){        System.out.println("我被调用了");    }}

Democontroller Code:

Packagecom. Secbro. Learn. Controller;Importcom. 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("=============== Split line =============");for (Demoservice demoservice:demoservices) {Demoservice. Test();} return"Success";}}

After running, Access Http://localhost:8080/demo/test executes the following results:

我被调用了===============分割线=============我被调用了

Originally, in Imperceptible spring has helped us do a lot of things, but we do not know just.

Spring Annotated (LIST&MAP) Special injection function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.