springboot详细图解,spring中文开发手册

首页 > 机动车 > 作者:YD1662023-11-09 18:59:46

Spring Boot 项目文件介绍

一、解析 pom.xml 文件:

(1)让我们来看看默认生成的 pom.xml 文件中到底有些什么:

<?xml version="1.0"encoding="UTF-8"?> <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xpwi</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/><!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

(2)我们可以看到一个比较陌生一些的标签 ,这个标签是在配置 Spring Boot 的父级依赖:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/><!-- lookup parent from repository --> </parent>

有了这个,当前的项目才是 Spring Boot 项目,spring-boot-starter-parent 是一个特殊的 starter ,它用来提供相关的 Maven 默认依赖,使用它之后,常用的包依赖就可以省去 version 标签。

关于具体 Spring Boot 提供了哪些 jar 包的依赖,我们可以查看本地 Maven 仓库下:\repository\org\springframework\boot\spring-boot-dependencies\2.1.0.RELEASE\spring-boot-dependencies-2.1.0.RELEASE.pom 文件来查看,挺长的…

二、应用入口类 SpringbootApplication.java

Spring Boot 项目通常有一个名为 *Application 的入口类,入口类里有一个 main 方法, 这个 main 方法其实就是一个标准的 Javay 应用的入口方法。

@SpringBootApplication 是 Spring Boot 的核心注解,它是一个组合注解,该注解组合了:@Configuration、@EnableAutoConfiguration、@ComponentScan;若不是用 @SpringBootApplication 注解也可以使用这三个注解代替。

其中,@EnableAutoConfiguration 让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置,例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行自动配置。

Spring Boot 还会自动扫描 @SpringBootApplication 所在类的同级包以及下级包里的 Bean ,所以入口类建议就配置在 grounpID arctifactID 组合的包名下(这里为 com.xpwi.springboot 包)

三、Spring Boot 的配置文件:

Spring Boot 使用一个全局的配置文件 application.properties 或 application.yml,放置在【src/main/resources】目录或者类路径的 /config 下。

Spring Boot 不仅支持常规的 properties 配置文件,还支持 yaml 语言的配置文件。yaml 是以数据为中心的语言,在配置数据的时候具有面向对象的特征。

Spring Boot 的全局配置文件的作用是对一些默认配置的配置值进行修改。

修改 properties 配置文件实例:

(1)打开 resources 下的 application.properties

springboot详细图解,spring中文开发手册(21)

(2)在这里我们可以设置访问的端口,将 Tomcat 默认端口设置为 8080 (默认的不修改) ,并将默认的访问路径从 “/” 修改为 “/cn” 时,再访问 http://localhost:8080/ 是什么都没有的,此时要访问 hello 是要使用 http://localhost:8080/cn/hello

springboot详细图解,spring中文开发手册(22)

注意:如果是 yml 需要在 “:” 后加一个空格,幸好 IDEA 很好地支持了 yml 文件的格式有良好的代码提示;

我们可以自己配置多个属性

(3)使用 yml 文件作为配置文件,我们直接把 .properties 后缀的文件删掉,使用 .yml 文件来进行简单的配置

springboot详细图解,spring中文开发手册(23)

(4)在然后使用在我们的 HelloController.java 类中使用 @Value 来获取配置属性,代码(请看注释):

packagecom.xpwi.springboot; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.web.bind.annotation.RequestMapping; importorg.springframework.web.bind.annotation.RestController; /** * 测试控制器 * *@author: @肖朋伟CSDN *@create: 2018-11-18 */ @RestController publicclassHelloController{ // 获取.yml 文件中值 @Value("${name}") privateString name; // 获取 age @Value("${csdnUrl}") privateString csdnUrl; //路径映射,对应浏览器访问的地址,访问该路径则执行下面函数 @RequestMapping("/hello") publicStringhello(){ returnname " CSDN 博客:" csdnUrl; } }

(5)重启 Spring Boot ,输入地址:http://localhost:8080/hello 能看到正确的结果:

springboot详细图解,spring中文开发手册(24)

上一页23456下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.