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工程相关推荐

  1. Spring Boot-Spring Tool Suit + Gradle 构建第一个Spring Boot 项目02

    概述 将工程托管到Github Gradle构建 为什么一个main函数就能启动web并提供这么多功能 幕后的 Spring Boot 分发器和 multipart 配置 视图解析器.静态资源以及区域 ...

  2. Spring Boot-Spring Tool Suit + Gradle 构建第一个Spring Boot 项目01

    文章目录 概述 使用Spring Tool Suite构建Spring Boot项目 下载STS 插件安装 搭建第一个Spring Boot项目 启动项目 概述 通常,构建一个Spring Boot项 ...

  3. Spring学习笔记:第一个Spring Boot程序HelloWorld

    Spring学习笔记:第一个Spring Boot程序HelloWorld 一.跟着 Spring 了解技术趋势 1.看看 Spring 5.x 的改变暗示了什么 2.Spring Boot 和 Sp ...

  4. 使用 Spring Boot CLI 运行第一个Spring boot程序

    简介 Spring Boot CLI是Spring Boot的命令行界面.它可以用来快速启动Spring.  它可以运行Groovy脚本.  Spring Boot CLI是创建基于Spring的应用 ...

  5. 构建第一个Spring Boot2.0应用之项目创建(一)

     1.开发环境 IDE: JAVA环境: Tomcat: 2.使用Idea生成spring boot项目 以下是使用Idea生成基本的spring boot的步骤. (1)创建工程第一步 (2)创建工 ...

  6. 构建第一个Spring Boot2.0应用之集成dubbo上---环境搭建(九)

    一.环境: Windows: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE Linux(C ...

  7. Spring Boot:构建一个RESTful Web应用程序

    介绍: REST代表表示状态传输 ,是API设计的体系结构指南. 我们假设您已经具有构建RESTful API的背景. 在本教程中,我们将设计一个简单的Spring Boot RESTful Web应 ...

  8. 创建并运行一个 Spring Boot 项目

    创建并运行一个 Spring Boot 项目 引言 第一个 Spring Boot 项目 1. 创建一个 spring boot 项目 第一步 第二步 第三步 第四步 2. 验证 第一步 第二步 3. ...

  9. 用 Docker 构建、运行、发布来一个 Spring Boot 应用

    原文同步至 http://waylau.com/docker-spring-boot/ 本文演示了如何用 Docker 构建.运行.发布来一个 Spring Boot 应用. Docker 简介 Do ...

最新文章

  1. 机器学习笔记(九)聚类
  2. 【图文并茂】DEV配置NTL库
  3. MAC电脑:安装mysql报ERROR 1045 (28000)Access denied
  4. matlab交替隐式迭代,jQuery关于隐式迭代的个人理解~
  5. SAP Spartacus的b2cLayoutConfig
  6. LeetCode 2165. 重排数字的最小值(计数)
  7. html5基础--audio标签元素
  8. 菲佣WPF——3(关于INotifyPropertyChanged的使用的想法)
  9. 用python写WordCount的MapReduce代码
  10. vmlinuz文件解压方法
  11. 文件系统系列之一:fat文件系统的结构分析
  12. python调整excel列宽_python - 有没有一种方法可以使用pandas.ExcelWriter自动调整Excel列的宽度? - 堆栈内存溢出...
  13. EXcel用法——如何冻结前两行,如何删除筛选的行
  14. React使用高德地图 (react-amap)(一)
  15. pb导入excel文件
  16. 单臂路由 二三层交换机、路由器简单组网
  17. 【html标签复习】
  18. 使用python获取美股行情数据
  19. 攻防世界 黑客精神unidbg破解
  20. 《构建之法》第一次作业

热门文章

  1. 数组索引必须为正整数或逻辑值是什么意思_贪心算法:K次取反后最大化的数组和...
  2. eclipse 不能将maven jar包导入到tomcat中问题
  3. vs2012打包和部署程序成可安装安装包文件(InstallShield
  4. Nginx+Tomcat windows环境下简单集群搭建
  5. 实战渗透之一个破站日一天
  6. 关于站库分离渗透思考总结
  7. Java学习小代码(1)编写三个数的排序程序
  8. 使用setInterval对ajax请求做轮询
  9. 关于使用layui中的tree的一个坑
  10. Elastic Search学习笔记5——基本操作