SpringBoot作用
简化依赖,各种启动器starter
实现快速开发,借助注解,自动配置
内嵌Servlet容器,独立运行
无缝的整合各种主流的开发框架
SpringBoot引入的关键注解
1 @Configuration
org.springframework.context.annotation.Configuration
作用于类上,相当于一个xml配置文件
2.@Bean
org.springframework.context.annotation.Bean;
作用在修饰了@Configuration的类的方法上,相当于在xml文件中配置Bean
3.@ConpnentScan
org.springframework.context.annotation.ComponentScan
作用扫描包,代替如下配置
<!-- 扫描控制层注解 -->
<context:component-scan base-package="com.szsxt.yt.controller" />
<!-- 扫描service层注解 -->
<context:component-scan base-package="com.szsxt.yt.service"/>
SpringBoot搭建
1. 创建项目
借助Spring初始化器
创建maven项目,添加依赖
2 依赖配置:
spring官方的解释叫什么stater poms,它可以提供dependency management,也就是说依赖管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.15.RELEASE</version>
<relativePath/>
</parent>
web启动器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
依赖关系:
spring-boot-starter-web:
spring-boot-starter:
spring-boot
spring-boot-autoconfigure
spring-boot-starter-logging
javax.annotation-api
spring-core
spring-boot-starter-json
spring-boot-starter-tomcat
javax.annotation-api
tomcat-embed-core
tomcat-embed-el
tomcat-embed-websocket
spring-web
spring-webmvc
如果需要解析编译jsp,需要加入tomcat-embed-jasper依赖
SpringBoot启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
包含了:
spring-boot-starter:
spring-boot
spring-boot-autoconfigure -- 自动配置
spring-boot-starter-logging
javax.annotation-api - javax注解 @Resource
spring-core - spring核心包
2.3 SpringBoot入口类:
// SpringBootApplication-注解修饰的类是SpringBoot启动类
// 它的目的就是开启Spring Boot的自动配置
// 类名: org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
// @SpringBootConfiguration + @EnableAutoConfiguration
// @SpringBootConfiguration = @Configuration
// 扫描包
@ComponentScan(basePackages = "com.sboot.controller")
public class BootApplication {
public static void main(String[] args) {
// 调用SpringApplication的run方法就启动内嵌的spring容器
// 实现扫描所有的带有@Configuration的类,实现容器装配bean
SpringApplication.run(BootApplication.class);
}
}
2.4 启动
1. 入口类运行
2. 插件运行
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
spring-boot:run
spring-boot:start
3. 启动器
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和AspectJ。
5)spring-boot-starter-artemis
通过Apache Artemis支持JMS的API(Java Message Service API)。
6)spring-boot-starter-batch
支持Spring Batch,包括HSQLDB数据库。
7)spring-boot-starter-cache
支持Spring的Cache抽象。
8)spring-boot-starter-cloud-connectors
支持Spring Cloud Connectors,简化了在像Cloud Foundry或Heroku这样的云平台上连接服务。
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
支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及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 Services。