码迷,mamicode.com
首页 > 编程语言 > 详细

springboot 通过@value注解引用自定义properties

时间:2019-08-22 15:56:27      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:int   ice   nan   ssi   color   属性   获取   sys   lis   

有两种方式获取值

首先在类的头部添加 @PropertySource 注解

1、使用@Value注解获取值

    @Value("${tenant_id}")
    private String tenantId;

在方法中使用:

  System.out.println("tenantId:" + tenantId);

输出:

技术图片

 

2、注入环境变量对象,存储注入的属性值

    @Autowired
    private ClassService classService;    

 在方法中使用:

    System.out.println("env:" + env.getProperty("accessToken"));

输出:

技术图片

 

 完整代码

@Controller
@PropertySource({"generator.properties"})
@RequestMapping("/manager/class")
public class ClassController {
    @Value("${tenant_id}")
    private String tenantId;

    @Autowired
    private Environment env;  // 注入环境变量对象,存储注入的属性值

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("manager:class:class")
    public String list(@RequestParam Map<String, Object> params){
        System.out.println("tenantId:" + tenantId);
        System.out.println("env:" + env.getProperty("accessToken"));
        return "";
    }
}    

 

项目结构

技术图片

 

springboot 通过@value注解引用自定义properties

标签:int   ice   nan   ssi   color   属性   获取   sys   lis   

原文地址:https://www.cnblogs.com/xzp0222/p/11394768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!