SpringBoot框架的搭建以及常用启动器

SpringBoot是做什么的

Spring Boot是由Pivotal团队(Spring项目组)提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者

SpringBoot的优点是什么

从最根本上来讲,Spring Boot就是一些库的集合,它能够被任意项目的构建系统所使用。可以简化基于Spring框架的开发和整合工作从而提高了开发效率。

还有如下优点:
1、嵌入的Tomcat,无需部署WAR文件
2、简化Maven配置
3、文件自动配置Spring(约定大于配置的思想)
4、基本上可以达到零配置(在开发过程中需要配置过多的配置文件

搭建SpringBoot项目

SpringBoot项目是基于maven或者(gradle)构建的。在Spring的官方网站中可以下载一个搭建好的SpringBoot项目
进入spring官网
在这里插入图片描述
进入开始项目导航页面
在这里插入图片描述
点击Spring初始化,得SpringBoot初始项目例子
在这里插入图片描述
填写 maven项目的 GAV信息,然后生产项目

打开下载下来的项目,我们主要查看它的POM文件

<?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>

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

	<groupId>com.yxd.springboot</groupId>
	<artifactId>hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>hello</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</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>

SpringBoot启动器汇总

SpringBoot通过启动器,让项目得到相应的支持

spring Boot应用启动器基本的一共有44种,具体如下:

1)spring-boot-starter 
这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。

2)spring-boot-starter-actuator 
帮助监控和管理应用。

3)spring-boot-starter-amqp 
通过spring-rabbit来支持AMQP协议(Advanced Message Queuing Protocol)。

4)spring-boot-starter-aop 
支持面向方面的编程即AOP,包括spring-aop和AspectJ5)spring-boot-starter-artemis 
通过Apache Artemis支持JMS的API(JavaMessage Service API)。

6)spring-boot-starter-batch 
支持Spring Batch,包括HSQLDB数据库。

7)spring-boot-starter-cache 
支持SpringCache抽象。

8)spring-boot-starter-cloud-connectors 
支持Spring Cloud Connectors,简化了在像Cloud FoundryHeroku这样的云平台上连接服务。

9)spring-boot-starter-data-elasticsearch 
支持ElasticSearch搜索和分析引擎,包括spring-data-elasticsearch。

10)spring-boot-starter-data-gemfire 
支持GemFire分布式数据存储,包括spring-data-gemfire。

11)spring-boot-starter-data-jpa 
支持JPA(Java Persistence API),包括spring-data-jpa、spring-orm、hibernate。

12)spring-boot-starter-data-MongoDB 
支持MongoDB数据,包括spring-data-mongodb。

13)spring-boot-starter-data-rest 
通过spring-data-rest-webmvc,支持通过REST暴露Spring Data数据仓库。

14)spring-boot-starter-data-solr 
支持Apache Solr搜索平台,包括spring-data-solr。

15)spring-boot-starter-freemarker 
支持FreeMarker模板引擎。

16)spring-boot-starter-groovy-templates 
支持Groovy模板引擎。

17)spring-boot-starter-hateoas 
通过spring-hateoas支持基于HATEOAS的RESTful Web服务。

18)spring-boot-starter-hornetq 
通过HornetQ支持JMS。

19)spring-boot-starter-integration 
支持通用的spring-integration模块。

20)spring-boot-starter-jdbc 
支持JDBC数据库。

21)spring-boot-starter-jersey 
支持Jersey RESTful Web服务框架。

22)spring-boot-starter-jta-atomikos 
通过Atomikos支持JTA分布式事务处理。

23)spring-boot-starter-jta-bitronix 
通过Bitronix支持JTA分布式事务处理。

24)spring-boot-starter-mail 
支持javax.mail模块。

25)spring-boot-starter-mobile 
支持spring-mobile。

26)spring-boot-starter-mustache 
支持Mustache模板引擎。

27)spring-boot-starter-Redis 
支持Redis键值存储数据库,包括spring-redis。

28)spring-boot-starter-security 
支持spring-security。

29)spring-boot-starter-social-facebook 
支持spring-social-facebook

30)spring-boot-starter-social-linkedin 
支持pring-social-linkedin

31)spring-boot-starter-social-twitter 
支持pring-social-twitter

32)spring-boot-starter-test 
支持常规的测试依赖,包括JUnitHamcrestMockito以及spring-test模块。

33)spring-boot-starter-thymeleaf 
支持Thymeleaf模板引擎,包括与Spring的集成。

34)spring-boot-starter-velocity 
支持Velocity模板引擎。

35)spring-boot-starter-web 
S支持全栈式Web开发,包括Tomcat和spring-webmvc。

36)spring-boot-starter-websocket 
支持WebSocket开发。

37)spring-boot-starter-ws 
支持Spring Web ServicesSpring Boot应用启动器面向生产环境的还有2种,具体如下:

1)spring-boot-starter-actuator 
增加了面向产品上线相关的功能,比如测量和监控。

2)spring-boot-starter-remote-shell 
增加了远程ssh shell的支持。

最后,Spring Boot应用启动器还有一些替换技术的启动器,具体如下:

1)spring-boot-starter-jetty 
引入了Jetty HTTP引擎(用于替换Tomcat)。

2)spring-boot-starter-log4j 
支持Log4J日志框架。

3)spring-boot-starter-logging 
引入了Spring Boot默认的日志框架Logback4)spring-boot-starter-tomcat 
引入了Spring Boot默认的HTTP引擎Tomcat5)spring-boot-starter-undertow 
引入了Undertow HTTP引擎(用于替换Tomcat

在pom.xml添加父工程

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

在pom文件中配置SpringBoot启动器

<dependencies>
	  <!-- 配置启动器 -->
	  <!-- 配置web启动器,让我们的项目的得到 tomcat和 springmvc的支持-->
	 <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>

编写控制层代码(Controller)

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("hello")
public class HelloController 
{
    
    
	@RequestMapping("query")
	@ResponseBody //将返回的Java对象直接转为JSON
	public Map<String,Object> queryHello()
	{
    
    
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("myname", "杨晓冬");
		map.put("myage", 35);
		
		return map;
	}
	
}

编写SpringBoot启动类

编写启动的类的时候,应该注意它的位置,它的位置应该位于其它的包的上层,因为它会自动扫描它所有的子包组件(同一个包下也行,同层不行)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//作为SpringBoot的启动类必须加注解@SpringBootApplication
@SpringBootApplication
public class AppStarter {
    
    
	public static void main(String[] args) {
    
    
		//加载启动类(会自动扫描和创建启动类所有包下所有的子包中的spring组件)
		SpringApplication.run(AppStarter.class, args);
	}
}

启动SpringBoot项目

启动springboot项目只要直接运算作为启动类中的主方法就可以。
这样,项目会自动扫描和创建项目中的Spring组件(在启动类所在包子包中的),并自动启动tomcat插件作为这些组件的载体服务器
在这里插入图片描述

输入查询路径即可

http://127.0.0.1:8080/spbo1/hello/query

猜你喜欢

转载自blog.csdn.net/zhx__/article/details/106603387
今日推荐