随笔 - 434,  文章 - 0,  评论 - 463,  阅读 - 46万

这节课,我们来学习一下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项目。

posted on   剽悍一小兔  阅读(80)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示