Spring如何获取配置在application.properties文件中属性的值?

通过 @Value("${com.springboot.name}")注解的方式:

application.properties 文件的内容如下:

com.springboot.name=qujianlei

com.springboot.title=studyhadoop

com.springboot.desc=${com.springboot.name} work hard ${com.springboot.title}

com.springboot.random.string=${random.value}

com.springboot.random.int=${random.int}

com.springboot.random.long=${random.long}

比方说我们现在要在 Controller 中获取 com.springboot.name 的值,只需像下面这样既可:

package com.qjl.bootstart.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qjl.bootstart.yml.User;
import com.qjl.bootstart.yml.YmlUtil;

/**
 * 类名称:SpringMVC的控制器
 * 全限定性类名: com.qjl.bootstart.controller.BootController
 * @author 曲健磊
 * @date 2018年8月4日上午10:38:23
 * @version V1

版权声明:本文为a909301740原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。