springboot的@Value 的map注入

首先在properties里写好map注入的文件:

#注入map
maps={'a':'aa','b':'bb','c':'cc'}

之后使用@Value注入:

package com.baizhi.demo.controller;

import com.baizhi.demo.config.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author think
 */
@RestController
public class HelloController {
    
    

    @Value("#{${maps}}")
    private Map<String, String> maps;

    @RequestMapping("hello")
    public String hello(){
    
    
        maps.forEach((k,v)-> System.out.println("k= "+k+"   v="+v));


        return "hello,world";
    }
}

运行输出就可以了

猜你喜欢

转载自blog.csdn.net/qq_44739706/article/details/114151854