SpringBoot之配置文件及自定义参数

今天对SpringBoot配置文件application.properties中部分配置以及profile多环境简单说明。

SpringBoot 默认加载配置文件application.properties,通过配置profiles属性实现多环境部署。

1.配置文件application.properties部分说明

#自定义服务器端口
server.port=8990

# 多环境配置文件激活属性(dev/prod/test)
spring.profiles.active=dev

这里写图片描述

2.自定义参数注入

在application.properties文件中定义参数如下:

#项目基本属性
com.venus.config.project.name=Learn
com.venus.config.project.version=v1

将配置文件通过@Value注解形式注入进来

/**
 * 自定义参数注入
 * @author Alan Liu
 * Created by on 2016/11/26 0026.
 */
@Component
public class BaseConfig {
    
    

    //获取不到配置的值,默认赋值Test
    @Value("${com.venus.config.project.name}")
    private String name;
    @Value("${com.venus.config.project.version}")
    private String version;
    ....
    此处省略setter和getter
}

3. @Autowired注入实例

接下来看看参数有没有被注入进来呢。。。

/**
 * Spring Boot 配置文件 以及多环境profile配置
 * @author Alan Liu
 * Created  on 2016/11/26 0026.
 */
@Controller
public class PropertiesCtr {
    
    

    @Autowired
    BaseConfig baseConfig;

    @RequestMapping("/base-config")
    @ResponseBody
    public String test01(){
        String returnMsg = baseConfig.getName() + ":"+ baseConfig.getVersion();
        return returnMsg;
    }
}

你看。。。
这里写图片描述
哇哦,竟然给注入进来了。。。

猜你喜欢

转载自blog.csdn.net/liuziyingbeidou/article/details/53427955