Spring Boot 简化了Spring 应用开发,不需要配置就能运行Spring 应用, **Spring Boot 管理Spring 容器、第三方插件,并提供很多默认系统级的服务。**大部分Spring 应用,无论是简单的Web系统, 还是构建复杂系统,都只需要少量配置和代码就能完成。
一、Spring Boot 通过Starter 来提供系统级服务
Spring Boot 已经提供了一系列Starter , 你也可以开发自己的Starter 。比如需要开发一个Web 应用, 只要在pom.xml 中声明一下即可。
<dependency>
<groupid>org.springframework.boot</groupid>
< artifactid>spring-boot-starter-web</artifactid>
</dependency>
如果你的应用中用到了Redis,则使用spring-boot-starter-data-redis, Spring Boot 会自动为你配置好Spring 需要的各种配置文件、Red is 的jar 包、依赖包, 以及合适的版本, 下表是SpringBoot 提供的常用Starter。
名称 | 作用 |
---|---|
spring-boot-starter-web | Web开发支持,默认使用tomcat8 |
spring-boot-starter-aop | AOP开发支持,使用AspectJ |
spring-boot-starter-jdbc | Spring JDBC |
spring-boot-starter-data-jpa | JPA方式访问数据库,使用Hibernate作为JPA实现 |
spring-boot-starter-data-elasticserach | 集成Elasticsearch,默认访问localhost:9200 |
spring-boot-starter-redis | 集成Redis,使用JRedis,默认链接localhost:6379 |
spring-boot-starter-cache | 缓存,支持多种缓存方式如本地、Redis、Ehcache等 |
spring-boot-devtools | 应用程序快速启动工具,提升开发体验 |
spring-boot-starter-data-mongodb | 集成MongoDB,默认访问mongodb://localhost/test |
spring-boot-starter-data-neo4j | 集成neo4j , 默认访问localhost:7474 |
spring-boot-starter-data-gemfire | 集成分布式缓存 |
spring-boot-starter-data-soIr | 基于Apache lucene 的搜索平台,默认访问http: //localhost:8983/solr |
s pring-boot-s tarter-data-cassandra | 集成Cassandra,默认访问localhost:7474 |
spring-boot-stater-data-ldap | 集成ldap |
spring-boot-starter-activemq | 消息集成ActiveMQ 支持 |
spring-boot-starter-amqp | 消息集成AMQP 协议支持,如支持RabbitMQ |
spring-boot-starter-jta-atomikos | 分布式事务支持,使用atomikos |
spring-boot-stater-jta-bitronix | 一个开源的分布式事务支持 |
spring-boot-starter-test | 包含JUnit 、Spring Test、Hamcrest 、Mockito 等测试工具 |
spring-boot-starter-webservices | webservice 支持 |
spring-boot-starter-websocket | websocket 支持 |
spring-boot-starter-jersey | REST 应用和Jersey 支持 |
spring-boot-starter-freemarker | Freemaker 支持 |
二、Spring Boot 相比Spring的优点
1、实现约定大于配置
不像Spring 那样“地狱般的配置体验”, Spring Boot 不需要配置或者极少配置,就能使用Spring 大量的功能。
2、提供了内置的Tomcat 或者Jetty 容器
3、通过依赖的jar 包管理、自动装配技术,容易支持与其他技术体系、工具集成。
4、支持热加载,开发体验好。也支持Spring B oot 系统监控,方便了解系统运行状况。
三、Hello Spring Boot 搭建过程
需要JDK8.0及以上版本,Maven3及以上。本例基于IntelliJ IDEA 2018.2工具搭建。
具体创建方法请参考 《第一个 Spring Boot2.x 程序快速搭建》
https://blog.csdn.net/LUCKWXF/article/details/94472728
package com.bee.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Ch1Application {
public static void main(String[] args) {
SpringApplication.run(Ch1Application.class, args);
}
}
为了在浏览器中浏览,再创建一个Controller类。
package com.bee.sample.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloworldController {
@RequestMapping ("/say.html")
public @ResponseBody String sayhello(){
return "Hello Spring Boot!";
}
}
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 https://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.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bee.sample</groupId>
<artifactId>ch1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>ch1</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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>
注意:如果tomcat配置有provided,要删除掉。
否则 SpringBoot项目启动后 迅速 执行结束,控制台打印: No active profile set, falling back to default profiles:
原配置:
org.springframework.boot spring-boot-starter-tomcat **provided**修改后:删除scope 标签
org.springframework.boot spring-boot-starter-tomcat启动应用,信息如下:
:: Spring Boot :: (v2.1.8.RELEASE)
2019-09-10 11:45:12.103 INFO 17784 --- [ main] com.bee.sample.Ch1Application : Starting Ch1Application on LAPTOP-QA29FUD4 with PID 17784 (F:\SpringBootPrj\ch1\target\classes started by wangxf in F:\SpringBootPrj\ch1)
2019-09-10 11:45:12.106 INFO 17784 --- [ main] com.bee.sample.Ch1Application : No active profile set, falling back to default profiles: default
2019-09-10 11:45:13.663 INFO 17784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-10 11:45:13.687 INFO 17784 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-10 11:45:13.687 INFO 17784 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-10 11:45:13.788 INFO 17784 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-10 11:45:13.789 INFO 17784 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1635 ms
2019-09-10 11:45:13.961 INFO 17784 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-10 11:45:14.133 INFO 17784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-10 11:45:14.136 INFO 17784 --- [ main] com.bee.sample.Ch1Application : Started Ch1Application in 2.415 seconds (JVM running for 2.82)
在浏览器中访问地址:
第二节 Spring Boot 2 精髓学习笔记(二)—Maven技术
第三节 Spring Boot 2 精髓学习笔记(三)—Spring 核心技术