SpringBoot入门学习—— 三、项目属性配置

三、项目属性配置

第一种:application.properties

访问http://127.0.0.1:8081/girl/hello

第二种:application.yml

填写属性需要空格,idea中颜色会变

访问网址:http://127.0.0.1:8082/girl/hello

example:

编写属性cupSize,使用value注解注入controller

访问http://127.0.0.1:8080/cup

创建一个类GirlProperties,使用@ConfigurationProperties(prefix = "name")映射配置参数,在controller中通过@Autowired注入,相应的GirlProperties要加@Component注解

GirlProperties中需给属性添加getter和setter

GirlProperties

package com.tonny;

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;

@RestController
public class HelloController {

    @Autowired
    GirlProperties girlProperties;

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String say(){
        return "Hello Spring Boot!";
    }

    @RequestMapping(value="/cup",method= RequestMethod.GET)
    public String cup(){
        return girlProperties.getCupSize();
    }

}

controller

访问127.0.0.1:8080/cup

多配置切换

欢迎进群交流258897306或关注公众号“IT群英汇

猜你喜欢

转载自blog.csdn.net/qq_42308454/article/details/82907022
今日推荐