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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring Boot (1) 构建第一个Spring Boot工程

發(fā)布時間:2024/1/17 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot (1) 构建第一个Spring Boot工程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Spring boot簡介

  spring boot是spring官方推出的一個全新框架,其設(shè)計目的是用來簡化新spring應(yīng)用的初始搭建以及開發(fā)過程。

Spring boot特點

  1.化繁為簡,簡化配置

  2.嵌入的Tomcat,無需部署war文件

  3.簡化maven配置

  4.自動配置spring

  5.開箱即用,沒有代碼生成,也無需xml配置。

  6.微服務(wù)的入門級微框架

spring boot并不是對spring功能上的增強,而是提供了一種快速使用spring的方式。

?

開發(fā)工具:InteliJ IDEA 、 Maven

最簡單的創(chuàng)建方式:Spring Initializr


新建項目->左側(cè)選擇spring Initializr ->next->輸入group、java版本、maven project、打包方式等信息->next->選擇web->完成。

使用這種方式搭建spring boot項目可以自動完成一些簡單框架

目錄結(jié)構(gòu):

  src.main.java 源碼文件夾 BootApplication啟動類、resources資源文件夾、test測試文件夾 pom.xml maven依賴如下:?

?pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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.spring</groupId><artifactId>boot</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>boot</name><description>Demo project for Spring Boot</description><!--sprin boot 父節(jié)點依賴,引入這個之后相關(guān)的引用就不需要添加version配置了,spring boot會選擇最合適的版本進行添加 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.2.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><!--spring-boot-starter-web : spring相關(guān)的jar,內(nèi)置tomcat服務(wù)器,jackson,MVC ,AOP 等 --><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>

新建一個HelloController.java

/*** RestController 等價于 @Controller 和 @ResponseBody*/ @RestController //引入spring boot的web模塊,就會自動配置web.xml等于web相關(guān)的內(nèi)容 public class HelloController {@RequestMapping("/hello")public String hello(){return "hello";} }

編輯BootApplication 不需要部署tomcat服務(wù)器,內(nèi)置的tomcat服務(wù)器直接通過main方法運行

/*** 指定這是一個spring boot 應(yīng)用程序*/ @SpringBootApplication public class BootApplication {public static void main(String[] args) {SpringApplication.run(BootApplication.class,args);} }

?

啟動BootApplication,默認端口8080 輸入地址http://localhost:8080/hello 即可訪問成功

?

也可以使用junit測試:?

  編輯src.test.java下的BootApplicationTests

package com.spring.boot;import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner;import java.net.URL;import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class BootApplicationTests {@LocalServerPortprivate int port;private URL base;@Autowiredprivate TestRestTemplate template;@Beforepublic void setUp() throws Exception {this.base = new URL("http://localhost:" + port + "/hello");}@Testpublic void hello() throws Exception {ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);assertEquals(response.getBody(), "Hello battcn");} }

?

自定義banner

springboot在啟動時會有以下內(nèi)容,可以自定義在resources目錄下添加指定命名文件即可:banner.txt、banner.jpg、banner.gif、banner.jpeg等等

. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.0.2.RELEASE)

  

?

@SpringBootApplication詳解

@SpringBootApplication public class BootApplication {public static void main(String[] args) {SpringApplication.run(BootApplication.class,args);} }

這個Run是一個單獨的項目啟動類。

@SpringBootApplication 是一個組合注解包括了@EnableAutoConfiguration及其他多個注解,這是一個項目啟動注解,如下:

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class} )} ) public @interface SpringBootApplication {

?

前四個注解:是元注解,用來修飾當前注解,就像public類的修飾詞,沒有實際的功能,如果不打算寫自定義注解,不需要了解

后三個注解:是真正起作用的注解,包括

  @SpringBootConfigration:當前類是一個配置類,就像xml配置文件,而現(xiàn)在是用java配置文件,效果是一樣的

  @EnableAutoConfiguration:spring boot 核心功能,自動配置,根據(jù)當前引入的jar包進行自動配置,比如引入了jackson的包,那么就會自動配置json,所以可以使用@ResponseBody,引入了Spring boot 的web模塊,就會自動配置web.xml等web相關(guān)的內(nèi)容,所以這些配置就不需要我們自己配置了

  @ComponentScan:用注解配置實現(xiàn)自動掃描,默認會掃描當前包和所有子包,和xml配置自動效果一樣,@Filter是排除了兩個系統(tǒng)類

?

@SpringBootConfiguration和@Bean

@SpringBootConfiguration public class Config {@Beanpublic String testStr(){return "Hello World";} }

?

@SpringBootConfiguration:說明這是一個配置文件類,他會被@ComponentScan掃描到

@Bean:就是在spring容器中聲明了一個bean,賦值為hello world,String方法類型就是bean的類型,hello方法名是bean的id

如果用xml配置文件來聲明bean:<bean id="hello" class="String"></bean>

HelloController.java

@RestController //引入spring boot的web模塊,就會自動配置web.xml等于web相關(guān)的內(nèi)容 public class HelloController {@AutowiredString testStr;@RequestMapping("/hello")public String hello(){return testStr;} }

在這里注入spring容器中的那個String類型的Bean,并打印到頁面

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/baidawei/p/9100977.html

總結(jié)

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

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