日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

springboot1——第一个springboot程序

發(fā)布時間:2025/3/15 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot1——第一个springboot程序 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

需要用到的軟件:

  • jdk1.8
  • maven3.6.1
  • springboot:最新版
  • IDEA
    官方:提供了一個快速生產(chǎn)的網(wǎng)站,idea集成了這個網(wǎng)站

1.進入spring boot官網(wǎng)進行快速程序的初始化,配置一些基本信息


2.加入web這個依賴,springboot則主動幫你配置好web.xml,tomcat和dispatchservlet,然后生成一個zip文件


3.導(dǎo)入項目




4.主程序入口:
主程序自動生產(chǎn)的
@springBootApplication:標注一個主程序,說明這是一個springboot應(yīng)用

5.編寫一個業(yè)務(wù)邏輯的類

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@Controller public class HelloworldController {@ResponseBody@RequestMapping("/hello")public String hello(){return "hello";} }

6.進行單元測試

繼續(xù)對于這個程序進行深究
6.1.首先是pom.xml部分:

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

此項目有一個父項目,
parent: 集成spring-boot-starter-parent的依賴管理,控制版本與打包等

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

spring-boot-starter:spring-boot場景啟動器;
spring-boot-starter-web:用于實現(xiàn)HTTP接口(該依賴中包含了SpringMvc),幫我們導(dǎo)入了web模塊正常運行所依賴的組件;官網(wǎng)對它的描述是:使用springMvc構(gòu)建Web(包括RESTful)應(yīng)用程序的入門者,使用tomcat作為默認嵌入式容器。
Spring Boot將所有的功能場景都抽取出來,做成一個個的starters(啟動器),只需要在項目里面引入這些starter相關(guān)場景的所有依賴都會導(dǎo)入進來。要用什么功能就導(dǎo)入什么場景的啟動器

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency>

spring-boot-starter-test用于編寫單元測試的依賴包
6.2. 主程序類,入口類

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /* @SpringBootApplication:標注這個類是一個spring boot應(yīng)用 */ @SpringBootApplication public class FirstDemoApplication {public static void main(String[] args) {SpringApplication.run(FirstDemoApplication.class, args);}}

@SpringBootApplication: Spring Boot應(yīng)用標注在某個類上說明這個類是SpringBoot的主配置類,SpringBoot就應(yīng)該運行這個類的main方法來啟動SpringBoot應(yīng)用;

進入SpringBootApplication,可以看到這個類有很多的注解

@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 {

其中:
@SpringBootConfiguration: Spring Boot的配置類:標注在某個類上,表示這是一個Spring Boot的配置類;
  @Configuration: 配置類上來標注這個注解;
  
@EnableAutoConfiguration告訴SpringBoot開啟自動配置功能;這樣自動配置才能生效;
  @AutoConfigurationPackage:自動配置包
    @Import(AutoConfigurationPackages.Registrar.class):
Spring的底層注解@Import,給容器中導(dǎo)入一個組件;
導(dǎo)入的組件由AutoConfigurationPackages.Registrar.class將主配置類(@SpringBootApplication標注的類)的所在包及下面所有子包里面的所有組件掃描到Spring容器;
  @Import(EnableAutoConfigurationImportSelector.class);給容器中導(dǎo)入組件?
   EnableAutoConfigurationImportSelector:導(dǎo)入哪些組件的選擇器;
將所有需要導(dǎo)入的組件以全類名的方式返回;這些組件就會被添加到容器中;會給容器中導(dǎo)入非常多的自動配置類(xxxAutoConfiguration);就是給容器中導(dǎo)入這個場景需要的所有組件,并配置好這些組件;


Properties properties=PropertiesLoaderUtils.loadProperties(resources);

所有的資源都加載到配置類中

7.用idea直接創(chuàng)建一個spring boot的項目




總結(jié)

以上是生活随笔為你收集整理的springboot1——第一个springboot程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。