博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3章 项目属性配置
阅读量:4599 次
发布时间:2019-06-09

本文共 5354 字,大约阅读时间需要 17 分钟。

数据库的配置

删掉static和templates这两个目录

 

 application.properties

server.port=8081server.context-path=/girl

application.yml

server:  port: 8082  context-path: /girl

 

 

package com.imooc.girl;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/** * Created by zhongzh * 2018-05-20 08:33 */@RestControllerpublic class HelloController {    @Value("${cupSize}")    private String cupSize;//通过一个注解把配置文件里面的内容给它注到cupSize这个属性变量里面来了    @RequestMapping(value = "/hello",method = RequestMethod.GET)    public String say(){        //return "Hello Spring Boot!";        return cupSize;    }}
server:  port: 8080cupSize: B

server:  port: 8080cupSize: Bage: 18
package com.imooc.girl;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/** * Created by zhongzh * 2018-05-20 08:33 */@RestControllerpublic class HelloController {    @Value("${cupSize}")    private String cupSize;//通过一个注解把配置文件里面的内容给它注到cupSize这个属性变量里面来了    @Value("${age}")    private Integer age;    @RequestMapping(value = "/hello",method = RequestMethod.GET)    public String say(){        //return "Hello Spring Boot!";        return cupSize + age;    }}

 

package com.imooc.girl;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/** * Created by zhongzh * 2018-05-20 08:33 */@RestControllerpublic class HelloController {    @Value("${cupSize}")    private String cupSize;//通过一个注解把配置文件里面的内容给它注到cupSize这个属性变量里面来了    @Value("${age}")    private Integer age;    @Value("${content}")    private String content;    @RequestMapping(value = "/hello",method = RequestMethod.GET)    public String say(){        //return "Hello Spring Boot!";        //return cupSize + age;        return content;    }}
server:  port: 8080cupSize: Bage: 18content: "cupSize: ${cupSize}, age: ${age}"
这样就不用每一次都写一个@Value的注解了。@ConfigurationProperties这个全新的注解它就可以把对应的girl这个前缀下面这些属性给它映射过来 。 application.yml
server:  port: 8080girl:  cupSize: B  age: 18      

 GirlProperties.java

package com.imooc.girl;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * Created by zhongzh * 2018-05-21 16:36 */@Component//你要注入配置还需要加另外一个注解@ConfigurationProperties(prefix = "girl")//获取前缀是girl的配置public class GirlProperties {    private String cupSize;    private Integer age;    public String getCupSize() {        return cupSize;    }    public void setCupSize(String cupSize) {        this.cupSize = cupSize;    }    public void setAge(Integer age) {        this.age = age;    }    public Integer getAge() {        return age;    }}

HelloController.java

package com.imooc.girl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/** * Created by zhongzh * 2018-05-20 08:33 */@RestControllerpublic class HelloController {    @Autowired    private GirlProperties girlProperties;    /*    @Value("${cupSize}")    private String cupSize;//通过一个注解把配置文件里面的内容给它注到cupSize这个属性变量里面来了    @Value("${age}")    private Integer age;    @Value("${content}")    private String content;*/    @RequestMapping(value = "/hello",method = RequestMethod.GET)    public String say(){        //return "Hello Spring Boot!";        //return cupSize + age;        //return content;        return girlProperties.getCupSize();    }}

这就是如何把配置给写到一个类里面去。这是一种比较推荐的方式,分组。把配置作为一个分组。在家里的时候喜欢B,出门的时候喜欢F。开发环境用的是B,生产环境用的是F。

解决开发环境和生产环境不同的配置的问题。作为一名优秀的工程师,不应该把时间花在频繁的改配置上。

 

application.yml

spring:  profiles:    active: dev

application-dev.xml

server:  port: 8080girl:  cupSize: B  age: 18

application-prod.xml

server:  port: 8081girl:  cupSize: F  age: 18

application.xml

spring:  profiles:    active: prod

之前讲过一种java -jar的启动方式。首先编译一下。

 

https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor

org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-configuration-processor
true

然后更新一下项目就行

@Value这个注解实现配置内容的注入。随着配置文件的增多建议大家一定要对配置文件做一个分组。这就要使用到下面两个注解@Component和@ConfigurationProperties来注入我们的配置。如何面对多环境来使用不同的配置文件。

 

 

 

                                                                                    

转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/9062519.html

你可能感兴趣的文章
多种方法求解八数码问题
查看>>
spring mvc ModelAndView向前台传值
查看>>
(黑客游戏)HackTheGame1.21 过关攻略
查看>>
Transparency Tutorial with C# - Part 2
查看>>
android 文件上传
查看>>
ASCII 码表对照
查看>>
javascript的DOM操作获取元素
查看>>
Shuffle'm Up(串)
查看>>
20145219 《Java程序设计》第06周学习总结
查看>>
C# 执行bat文件并取得回显
查看>>
基于YOLO的Autonomous driving application__by 何子辰
查看>>
javascript中的继承
查看>>
iOS-如何写好一个UITableView
查看>>
如何在Objective-C中实现链式语法
查看>>
select2 下拉搜索控件
查看>>
WebAPI常见的鉴权方法,及其适用范围
查看>>
08. 删除重复&海量数据
查看>>
重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++
查看>>
发布mvc遇到的HTTP错误 403.14-Forbidden解决办法
查看>>
jvm内存模型和内存分配
查看>>