Spring Boot 第一个应用

创建工程

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

maven配置

本地库 or 阿里云镜像;

第一个应用

创建Controller

//使用@RestController!!!
@RestController
public class HelloController {
}

创建方法

//使用@RequestMapping!!!
 @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String say(){
        return  "hello spring boot";
    }

配置

application.properties语法

例子
其中,context-path必须以/开头;

appliacation.yml语法(推荐使用)

server:
 port: 8080
 context-path: /girl
cupSize: B
age: 18
content: "cupSize: ${cupSize}, age: ${age}"

其中,:后必须跟着空格;
访问端口:8080
访问路径:http://localhost:8080/girl/hello

使用配置文件参数

单个参数

@RestController
public class HelloController {

 //可配置文件中配置多个参数,这里引用多个;
 @Value("${cupSize}")
 private String cupSize;

 @Value("${age}")
 private Integer age;

 @Value("${content}")
 private String content;

 @RequestMapping(value="/hello",method = RequestMethod.GET)
    public String say(){
      //return  cupSize;
      return  content;
    }
}

多个有关联的参数(参数分组!!!)

//配置文件中:

server:
 port: 8080
 context-path: /girl
girl:
 cupSize: B
 age: 18
//新建类:

@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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
//controller中
@RestController
public class HelloController {

  //调用配置参数类
  @Autowired
  private GirlProperties girlProperties;

  @RequestMapping(value="/hello",method = RequestMethod.GET)
  public String say(){
    return  girlProperties.getCupSize(); //使用配置参数
  }
}

测试、生产环境使用不同配置

设置 测试、生产环境配置文件
这里写图片描述

设置要使用的配置文件
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值