手把手的Spring Boot Web 项目教程,Spring Boot的环境配置

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

我们启动项目,默认端口是8080,我们现在给他配置一个8088:

server.port=8088

运行启动类,然后在浏览器地址栏访问上一节中的控制器:

启动成功了。

然后访问这个Controller

package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
 
 @RequestMapping("hello")
 public String hello(){
 return "<font style='font-size:28px;'>Hello Spring Boot</font>";
 }
}

访问:
http://localhost:8088/hello

访问成功啦!

如果你要加上一个项目的名字,做如下配置:

server.context-path=/demo

注意:/demo的 “ / ”不能少!

重启服务,访问的地址则需要改为:

http://localhost:8088/demo/hello

可见,一样能够成功访问!

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

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个空格,而实际上,我们这里只能有两个空格。

一个空格都不能多!!

OK,修改yml文件(老老实实打两个空格吧,亲!):

server:
 port: 8088
 context-path: demo

图解:

重新启动项目,访问:

http://localhost:8089/demo/hello

成功!

本篇教程讲解了在SpringBoot项目中进行配置的两种方式,我个人推荐大家使用yml文件来配置SpringBoot项目。

声明:本站部分文章内容及图片转载于互联 、内容不代表本站观点,如有内容涉及侵权,请您立即联系本站处理,非常感谢!

(0)
上一篇 2019年3月13日
下一篇 2019年3月14日

相关推荐