日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot——HelloWorld

發布時間:2024/9/21 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot——HelloWorld 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

功能:瀏覽器發送hello請求,服務器接收請求并處理,響應HelloWorld字符串

一、創建一個maven工程(jar)


創建好的樣子


二、導入Springboot相關的依賴

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
三、編寫一個主程序:作用就是啟動Springboot應用

/** * @SpringBootApplication用來標注一個主程序,說明這是一個Springboot應用 */ @SpringBootApplication public class HelloWorldMainApplication {public static void main(String[] args) {//Spring應用啟動起來 SpringApplication.run(HelloWorldMainApplication.class,args); } }
四、編寫相關的Controller、Service

@Controller public class HelloController {@ResponseBody @RequestMapping("/hello")public String hello(){return "Hello World"; } }
五、啟動程序并在瀏覽器訪問



六、簡化部署

<!--這個插件可以將應用打包成一個可執行的jar包--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>


點擊maven的Lifecycle下的package進行打包,打完之后左邊就會出現打好的包


將這個應用打成jar包,直接使用java -jar的命令進行執行


七、項目探究

1)POM文件

①父項目

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent>

他的父項目是

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>

這個是來真正管理Springboot應用里面的所有依賴?

也可以稱為Springboot的版本仲裁中心:

以后我們導入依賴默認是不需要寫版本的(沒有在這個dependencies里面管理的依賴是需要聲明版本號的)

②導入的依賴(啟動器)

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


spring-boot-starter:spring-boot場景啟動器

web:幫我們導入了web模塊正常運行所需要的依賴的組件

Springboot將所有的業務場景都抽取出來做成一個個的starters(啟動器),需要的時候,只需要在項目中引入這些starter,相關場景的所有依賴都會導入進去。要用什么功能就導入什么場景的啟動器。

2)主程序類(主入口類)

/** * @SpringBootApplication用來標注一個主程序,說明這是一個Springboot應用 */ @SpringBootApplication public class HelloWorldMainApplication {public static void main(String[] args) {//Spring應用啟動起來 SpringApplication.run(HelloWorldMainApplication.class,args); } }

@SpringBootApplication:這個注解表示Springboot應用,標注在某個類上說明這個類是Springboot的主配置類,Springboot就應該來運行這個類的main方法來啟動這個Springboot應用。

@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} )} )

@SpringBootConfiguration是Springboot的配置類,

????????標注在某個類上表示這是一個Springboot的配置類。

????? ? 底層是@Configuration:配置類上標注這個注解。

????????????????????配置類------>配置文件(以前的)。配置類也是容器中的一個組件:@Component

@EnableAutoConfiguration:開啟自動配置功能。

????? ? 以前需要配置的東西(如 包的掃描),Springboot幫我們自動配置了。@EnableAutoConfiguration告訴Springboot開啟自動配置功能,這樣自動配置才能夠生效。

????? ? 底層包含的注解:

@AutoConfigurationPackage @Import({EnableAutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration {

????????@AutoConfigurationPackage:自動配置包

????? ? 底層是@Import({Registrar.class}),這是Spring的底層注解@Import,給容器中導入一個組件。導入的主鍵由{Registrar.class},

????????????將主配置類(@SpringbootApplication標注的類)的所有包及下面所有子包里面的所有組件掃描Spring容器中。

????????@Import({EnableAutoConfigurationImportSelector.class}):給容器中導入一些組件

????????????{EnableAutoConfigurationImportSelector.class}:導入哪些組件的選擇器。

????????? ? 將所需要導入的組件以全類名的方式返回(數組的方式)。這些組件就會被添加到全類名中。

????????????給容器中導入非常多的自動配置類(xxxAutoConfiguration):就是給容器中導入這個場景所需要的所有組件,并配置好這些組件。


有了這些自動配置類,就免去了我們手動編寫配置注入功能組件等的工作

這些自動配置類是通過下面的方法獲取到的:

this.getCandidateConfigurations(annotationMetadata, attributes);

該方法下面主要實現:

SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());

作用就是從類路徑META-INF/spring.factories中獲取EnableAutoConfiguration指定的值;

Springboot在啟動的時候從類路徑META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,將這些值作為自動配置類導入到容器中,自動配置類就生效了,幫我們進行自動配置工作。以前我們需要自己配置的東西,自動配置類都幫我們做了。

J2EE的整體整合解決方案和自動配置都在下面的spring-boot-autoConfigure:1.5.9.RE:EASE.jar包中:



轉載于:https://www.cnblogs.com/huangzhe1515023110/p/9276037.html

總結

以上是生活随笔為你收集整理的Springboot——HelloWorld的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。