搭建一个简单的 Spring Boot 应用采用 Spring Initializr 搭建安装并配置maven环境搭建Spring BootSping Boot 配置文件简介application.properties 配置文件Spirng Boot Java 配置Spring Boot xml 配置
搭建一个简单的 Spring Boot 应用
采用 Spring Initializr 搭建
安装并配置maven环境
先安装maven并配置好环境,下载地址:http://maven.apache.org/download.cgi。解压后,将目录下的bin路径添加至系统环境变量
修改setting.xml配置文件(在压缩路径的conf文件夹下)
alimaven aliyun maven http://maven.aliyun.com/nexus/content/groups/public/ central nexus-osc central Nexus osc http://maven.oschina.net/content/groups/public/ nexus-osc-thirdparty thirdparty Nexus osc thirdparty http://maven.oschina.net/content/repositories/thirdparty/
打开cmd命令窗口,输入mvn -v
搭建Spring Boot
Spring Initializr 是官方提供的一种快捷搭建 Spring Boot 应用的方式。只需要打开网址: https://start.spring.io/ 就可以看到:
我们可以看到上面可以选择构建工具、语言、Spring Boot 版本、group 和 artifact 以及依赖。这里我们选择使用 Maven 构建,语言 java,Spring Boot 版本 2.4,group 为 com.demo,artifact 为 springboot,依赖我们选择 web。点击 Generate Project,我们会得到一个 springboot.zip 的压缩包。
将压缩包下载解压。接着切换工作空间到 springboot 目录,然后在 src/main/java 目录下新建包路径 com.demo.springboot.controller。
在包中建立新类 DemoController.java,代码如下:
package com.demo.springboot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController // RestController 相当于同时使用 @Controller 和 @ResponseBody 注解 public class DemoController{ @RequestMapping("demo") public String demo(){ return "Hello demo"; } }
然后打开 terminal,运行 mvn spring-boot:run,这里使用 Spirng Boot 的 maven 插件启动。
经过漫长的等待... ...
浏览器输入http://localhost:8080/demo
Sping Boot 配置文件简介
application.properties 配置文件
官方提供的压缩包中的 src/main/resources 目录下有一个 application.properties 配置文件(如果没有就手动创建一个),这就是 Spring Boot 默认的配置文件,可以对其进行更改来修改 Spring Boot 的配置。在修改之前,首先需要知道 properties 文件的格式。properties 文件的内容都是以键值对的形式出现:键 = 值。比如要修改 Spring Boot 的端口,那么就在配置文件中填写:
#端口 server.port = 808
properties 文件中每个属性占有一行。properties 中除了填写 Spring Boot 的配置信息外也可以自定义属性,比如在其中添加:
demo.springboot = Hello_demo
如何在 Spring Boot 中使用它们呢?打开 DemoController.java,修改为下面的代码:
package com.demo.springboot.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController // RestController public class DemoController{ // 使用 @Value 注解注入属性值 @Value("${demo.springboot}") private String demo; @RequestMapping("demo") public String demo(){ return demo; } }
注意: 这里只能访问 application.properties 中的属性,其他自定义的配置文件中的属性是访问不到的,还需要其他的处理。
首先在 src/main/resources 目录下建立 my.properties 文件,内容为:
my.test = test_my
修改 DemoController.java:
package com.demo.springboot.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController // 加载 classpath 目录下的 my.properties 文件 @PropertySource(value = "classpath:my.properties") public class DemoController{ // 使用 @Value 注解注入属性值 @Value("${my.test}") private String test; @RequestMapping("demo") public String demo(){ return test; } }
Spirng Boot Java 配置
Spring Boot 除了可以使用 application.properties 配置之外,也可以使用 Java 来自定义配置,就比如在 application.properties 配置文件 节中说到的修改访问端口为 8080,也可以通过 Java 代码来实现。
建立包 com.demo.springboot.config,在包目录下建立 ServletConfig.java。
package com.demo.springboot.config; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.context.annotation.Configuration; // @Configuration 表示该类为配置类,该注解可以被 @ComponentScan 扫描到 @Configuration public class ServletConfig implements WebServerFactoryCustomizer{ @Override public void customize(ConfigurableServletWebServerFactory factory){ // 设置端口为 8080 factory.setPort(8080); } }
然后将 application.properties 中的 server.port = 8080 注释掉.
Spring Boot xml 配置
Spring Boot 已经不推荐使用 xml 作为配置方式,如果一定要使用,可以通过 @ImportResource 注解来完成。
待续...
领取专属 10元无门槛券
私享最新 技术干货