Springwww1869922comBoo19908836661app

这节课,我们来学习一下SpringBoot的环境配置,在SpringBoot中,所有的配置都写在application.properties中:

手把手的Spring Boot Web 项目教程,Spring Boot的环境配置
我们启动项目,默认端口是8080,我们现在给他配置一个8088:

server.port=8088
手把手的Spring Boot Web 项目教程,Spring Boot的环境配置
运行启动类,然后在浏览器地址栏访问上一节中的控制器:

手把手的Spring Boot Web 项目教程,Spring Boot的环境配置
启动成功了。

然后访问这个Controller

package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;br/>@RestController
public class HelloController {

@RequestMapping("hello")
public String hello(){
return "<font style='font-size:28px;'>Hello Spring Boot</font>";
}
}

手把手的Spring Boot Web 项目教程,Spring Boot的环境配置
可见,一样能够成功访问!

接下来,我们再来介绍一种yml配置方式,这里编写一个yml文件,文件名还是application

手把手的Spring Boot Web 项目教程,Spring Boot的环境配置
server:
port: 8088
context-path: demo
这样写的好处就是,有一个层级关系,不需要每一行都写全了。需要注意的是,port: 和

8088之间必须要有一个空格,不能挤在一起,否则是识别不了的!!!

启动项目,报错:

java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'

at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadIntoGroup(ConfigFileApplicationListener.java:476)

at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:465)

at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:386)

at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:225)

at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:195)

at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:182)

at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:168)

at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)

at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)

at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)

at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)

at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)

at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:296)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)

at com.example.demo.DemoApplication.main(DemoApplication.java:10)

注意哦,这里有个坑,我们采用yml文件配置项目的时候,二级配置这里,不能直接一个tab键,那样会有4个空格,而实际上,我们这里只能有两个空格。

猜你喜欢

转载自blog.51cto.com/14254839/2368132
app