javascript
Spring Boot (1) 构建第一个Spring Boot工程
Spring boot簡介
spring boot是spring官方推出的一個全新框架,其設計目的是用來簡化新spring應用的初始搭建以及開發過程。
Spring boot特點
1.化繁為簡,簡化配置
2.嵌入的Tomcat,無需部署war文件
3.簡化maven配置
4.自動配置spring
5.開箱即用,沒有代碼生成,也無需xml配置。
6.微服務的入門級微框架
spring boot并不是對spring功能上的增強,而是提供了一種快速使用spring的方式。
?
開發工具:InteliJ IDEA 、 Maven
最簡單的創建方式:Spring Initializr
新建項目->左側選擇spring Initializr ->next->輸入group、java版本、maven project、打包方式等信息->next->選擇web->完成。
使用這種方式搭建spring boot項目可以自動完成一些簡單框架
目錄結構:
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 父節點依賴,引入這個之后相關的引用就不需要添加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相關的jar,內置tomcat服務器,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相關的內容 public class HelloController {@RequestMapping("/hello")public String hello(){return "hello";} }編輯BootApplication 不需要部署tomcat服務器,內置的tomcat服務器直接通過main方法運行
/*** 指定這是一個spring boot 應用程序*/ @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在啟動時會有以下內容,可以自定義在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配置文件,而現在是用java配置文件,效果是一樣的
@EnableAutoConfiguration:spring boot 核心功能,自動配置,根據當前引入的jar包進行自動配置,比如引入了jackson的包,那么就會自動配置json,所以可以使用@ResponseBody,引入了Spring boot 的web模塊,就會自動配置web.xml等web相關的內容,所以這些配置就不需要我們自己配置了
@ComponentScan:用注解配置實現自動掃描,默認會掃描當前包和所有子包,和xml配置自動效果一樣,@Filter是排除了兩個系統類
?
@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相關的內容 public class HelloController {@AutowiredString testStr;@RequestMapping("/hello")public String hello(){return testStr;} }在這里注入spring容器中的那個String類型的Bean,并打印到頁面
?
?
轉載于:https://www.cnblogs.com/baidawei/p/9100977.html
總結
以上是生活随笔為你收集整理的Spring Boot (1) 构建第一个Spring Boot工程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 合并重定向 command file
- 下一篇: python虚拟环境 virtualen