建站优化易下拉系统,网络营销有哪些方式,西安搬家公司联系电话,网站开发前景怎么样文章目录 第一个helloword项目新建 Spring Boot 项目Spring Boot 项目结构分析SpringBootApplication 注解分析新建一个 Controller大功告成,运行项目 简而言之#xff0c;从本质上来说#xff0c;Spring Boot 就是 Spring#xff0c;它做了那些没有它你自己也会去做的 Spri… 文章目录 第一个helloword项目新建 Spring Boot 项目Spring Boot 项目结构分析SpringBootApplication 注解分析新建一个 Controller大功告成,运行项目 简而言之从本质上来说Spring Boot 就是 Spring它做了那些没有它你自己也会去做的 Spring Bean 配置。
第一个helloword项目
新建 Spring Boot 项目
你可以通过 https://start.spring.io/ 这个网站来生成一个 Spring Boot 的项目。 注意勾选上 Spring Web 这个模块这是我们所必需的一个依赖。当所有选项都勾选完毕之后点击下方的按钮 Generate 下载这个 Spring Boot 的项目。下载完成并解压之后我们直接使用 IDEA 打开即可。
另外阿里也有一个类似的网站 https://start.aliyun.com/bootstrap.html 功能甚至还要更强大点支持生成特定应用架构的项目模板 2.你也可以直接通过 IDEA 来生成一个 Spring Boot 的项目具体方法和上面类似File-New-Project-Spring Initializr。
Spring Boot 项目结构分析
成功打开项目之后项目长下面这个样子 以 Application 为后缀名的 Java 类一般就是 Spring Boot 的启动类比如本项目的启动项目就是HelloWorldApplication 。我们直接像运行普通 Java 程序一样运行它由于 Spring Boot 本身就嵌入 servlet 容器的缘故我们的 web 项目就运行成功了 非常方便。
需要注意的一点是 Spring Boot 的启动类是需要最外层的不然可能导致一些类无法被正确扫描到导致一些奇怪的问题。 一般情况下 Spring Boot 项目结构类似下面这样
com- example- myproject- Application.java|- domain| - Customer.java| - CustomerRepository.java|- service| - CustomerService.java|- controller| - CustomerController.java|- config| - swagerConfig.java|Application.java是项目的启动类domain 目录主要用于实体Entity与数据访问层Repositoryservice 层主要是业务类代码controller 负责页面访问控制config 目录主要放一些配置类
SpringBootApplication 注解分析
HelloWorldApplication
SpringBootApplication
public class HelloWorldApplication {public static void main(String[] args) {SpringApplication.run(HelloWorldApplication.class, args);}}说到 Spring Boot 启动类就不得不介绍一下 SpringBootApplication 注解了这个注解的相关代码如下
package org.springframework.boot.autoconfigure;
Target(ElementType.TYPE)
Retention(RetentionPolicy.RUNTIME)
Documented
Inherited
SpringBootConfiguration
EnableAutoConfiguration
ComponentScan(excludeFilters {Filter(type FilterType.CUSTOM, classes TypeExcludeFilter.class),Filter(type FilterType.CUSTOM, classes AutoConfigurationExcludeFilter.class) })
public interface SpringBootApplication {......
}package org.springframework.boot;
Target(ElementType.TYPE)
Retention(RetentionPolicy.RUNTIME)
Documented
Configuration
public interface SpringBootConfiguration {}可以看出大概可以把 SpringBootApplication看作是 Configuration、EnableAutoConfiguration、ComponentScan 注解的集合。根据 SpringBoot 官网这三个注解的作用分别是
EnableAutoConfiguration启用 SpringBoot 的自动配置机制ComponentScan 扫描被Component (Service,Controller)注解的 bean注解默认会扫描该类所在的包下所有的类。Configuration允许在上下文中注册额外的 bean 或导入其他配置类。
所以说 SpringBootApplication就是几个重要的注解的组合为什么要有它当然是为了省事避免了我们每次开发 Spring Boot 项目都要写一些必备的注解。这一点在我们平时开发中也经常用到比如我们通常会提一个测试基类这个基类包含了我们写测试所需要的一些基本的注解和一些依赖。
新建一个 Controller
上面说了这么多我们现在正式开始写 Spring Boot 版的 “Hello World” 吧。
新建一个 controller 文件夹并在这个文件夹下新建一个名字叫做 HelloWorldController 的类。
RestController是 Spring 4 之后新加的注解如果在 Spring4 之前开发 RESTful Web 服务的话你需要使用Controller 并结合ResponseBody注解也就是说Controller ResponseBody RestController。对于这两个注解我在基础篇单独抽了一篇文章来介绍。
com.example.helloworld.controller
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(test)
public class HelloWorldController {GetMapping(hello)public String sayHello() {return Hello World;}
}默认情况下Spring Boot 项目会使用 8080 作为项目的端口号。如果我们修改端口号的话非常简单直接修改
application.properties配置文件即可。
src/main/resources/application.properties
server.port8333大功告成,运行项目
运行 HelloWorldApplication 运行成功控制台会打印出一些消息不要忽略这些消息它里面会有一些比较有用的信息。
2019-10-03 09:24:47.757 INFO 26326 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8333 (http) with context path 上面是我截取的一段控制台打印出的内容通过这段内容我们就知道了 Spring Boot 默认使用的 Tomact 内置 web 服务器项目被运行在我们指定的 8333 端口上并且项目根上下文路径是 “/”。
浏览器 http://localhost:8333/test/hello 如果你可以在页面正确得到 “Hello World” 的话说明你已经成功完成了这部分内容。