springBoot startup: 常用配置(一)

helloworld

  1. IDEA 创建 SpringBoot 工程。


    springBoot startup: 常用配置(一)_第1张图片
    springBoot.PNG
  2. name 随意,选择组件的时候,先只选择最基本的 web


    springBoot startup: 常用配置(一)_第2张图片
    start.PNG
  3. 创建新类,并写入以下代码


    springBoot startup: 常用配置(一)_第3张图片
    helloworld.PNG
@RestController
public class helloController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say(){
        return "Hello Spring Boot";
    }
}
  1. 点击run,brower open http://localhost:8080/hello

启动方式

  1. 进入项目所在目录, mvn spring-boot:run
mvn build
cd target
java -jar xxx.jar

配置文件:application.properties

配置端口及网址前缀

springBoot startup: 常用配置(一)_第4张图片
setting.PNG
#配置端口
server.port=8080
#网址前缀
server.context-path=/zzjackweb3

现在打开 http://localhost:8080//zzjackweb3/hello 才能看到第一节的网址

使用 yml 格式进行配置

yml 格式比 properties 更加灵活,所以改为 yml。把刚才的配置以yml格式重写


springBoot startup: 常用配置(一)_第5张图片
setttingYml.PNG

常用配置

可以配置一些常量在全局使用,比如在配置文件中继续添加,比如

showValue: i am a constant value

在 HelloController 中添加注解

@RestController
public class HelloController {

// 新加注解部分
    @Value("${showValue}")
    private String showValue;

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String say(){
// 可以在方法中调用配置的这个 showValue
        return showValue;
    }
}

yml 可以像模板引擎一样,使用一些简单的语法

server:
  port: 8081
  context-path: /zzjackweb3
showValue: i am a constant value
valueNum: 1
content: "${showValue},and my value is ${valueNum}"

把 content 添加到文件中

@RestController
public class IndexController {
    
    @Value("${content}")
    private String content;
  
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say(){
        return content;
}
}

配置分组

因为目前学到的,使用一个配置就添加一个注解,十分麻烦。

  1. 现在有方法可以直接使用某一个配置。比如直接配置classone 这一组
classone:
  showValue: i am a constant value
  valueNum: 1
  content: "${showValue}, and my value is ${valueNum}"
  1. 新建 class 文件 ,classOne。把 classone 相关配置加载进来。
    必须实现get 和 set 方法。如果只实现get方法,读取配置会为null
    可以通过快捷键,alt + insert ,来快速实现 get and set 方法
    可以通过添加注解@NotEmpty,来保证配置一定有值
@Component
@ConfigurationProperties(prefix = "classone")
public class ClassOne{
  @NotEmpty
  private String showValue;
  private Integer valueNum;
 
  public void setShowValue(){
    return showValue;
} 
  public String getShowValue(){
    return showValue;
}
}
  1. 如果编辑器提示,看看是不是有插件没配置。可以把这个插件配置到 maven 中。

  org.springframework.boot
  spring-boot-configuration-processor
  true

  1. 修改 IndexController
@RestController
public class IndexController{
    @Autowired
    private ClassOne classone;
    
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say(){
        return classone.getShowValue();
  }
}

多环境配置

  1. 比如我们要配置开发环境dev 和 线上环境prod 两个,新建两个文件,注意application-dev.yml,中间不能使用下划线!!!
    springBoot startup: 常用配置(一)_第6张图片
    yaml-file.png
  2. application.yml 中像图中这么写就表示使用 dev 配置,如果改为 prod,就表示使用 prod 配置。
  3. 如果以命令行参数方式启动,这样写就不用更改 application.yml 的配置了。
// target 目录下会的多出一个 java 包
mvn install
//  运行 jar 包
java -jar target/xxx.jar --spring.profiles.active=prod

你可能感兴趣的:(springBoot startup: 常用配置(一))