SpringBoot是基于java来建立一个微服务的开源框架。它由Pivotal Team维护并用来创建一个独立和预配置好的spring应用程序。
什么是微服务?
微服务架构允许开发人员独立的开发和维护单独的应用服务,每个微服务由独立的进程和轻量级模型来支持应用程序。优势由如下:1
2
3
4
5- Easy deloyment 易于部署
- Simple scalability 容易扩展
- Compatible with Containers 容器兼容性好
- Minimum configuration 配置较少
- Lesser production time 生产时间段
什么是Spring Boot?
SpringBoot 为java开发着提供一个良好的平台来开发一个独立的即时使用的生产型应用,你可以开始使用最少的配置,而无需进行整个Spring配置设置。优势有如下:1
2
3- 易于理解和开发Spring应用程序
- 提高生产力
- 减少开发时间
SpringBoot有如下目标:1
2
3
4- To avoid complex XML configuration in Spring
- To develop a production ready Spring applications in an easier way
- To reduce the development time and run the application
- Offer an easier way of getting started with the application
为什么选择Spring Boot
Spring Boot 的好处如下:1
2
3
4
5
61. 能够灵活地配置Java Bean,XML configuraions,和Database Transactions
2. 它提供了强大地批处理和管理REST断点
3. 在SpringBoot中,所有东西都是自动配置,无需手动配置
4. 注解式应用程序
5. 简化依赖管理
6. 内嵌Servlet容器
如何执行
Spring Boot 基于@EnableAutoConfiguration注解使得自动配置你地应用程序,例如,如果你的classpath存在Mysql数据库信息,但是你并没有配置任何数据库连接,然后SpringBoot自动配置一个内存数据库。
这个SpringBoot应用程序入口是包含main方法以及@SpringBootApplication注解的类。
SpringBoot通过@ComponentScan注解自动扫描所有组件。
Spring Boot Starters
对于一个大工程来说,处理依赖配置是一个困难的任务,SpringBoot通过提供自已配置好的一系列依赖来为开发者提供方便。
比如,如果你想使用Spring和JPA进行数据库访问,那么在项目中包含spring-boot-start-data-jpa依赖项就足够了。
记住所有的Spring Boot starter命名规则是spring-boot-starter-,其中表示应用的类型,例如:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30Spring Boot Starter Actuator dependency is used to monitor and manage your application. Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Spring Boot Starter Security dependency is used for Spring Security. Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Spring Boot Starter web dependency is used to write a Rest Endpoints. Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Boot Starter Thyme Leaf dependency is used to create a web application. Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot Starter Test dependency is used for writing Test cases. Its code is shown below −
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test<artifactId>
</dependency>
Auto Configuration
Spring Boot自动配置通过项目中所加入的JAR 包依赖来控制你的Spring应用。你需要添加@EnalbeAutoConfiguration或者@SpringBootApplication注解到你的main函数入口类,然后,SpringBoot应用程序就会自动配置,看如下代码:1
2
3
4
5
6
7
8
9import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Spring Boot Application
SpringBoot应用程序的入口是包含@SpringBootApplication注解的应用程序,这个类应该有main函数来启动整个应用程序。其中@SpringBootApplication注解包含AutoConfiguration,ComponentScan,和Spring Boot Configuration。就是说,如果你添加了@SpringBootApplication注解就不需要添加@EnableAutoConfiguration,@ComponentScan和@SpringBootConfiguration注解。看如下代码:1
2
3
4
5
6
7
8
9import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Component Scan
Spring Boot用当初始化时会扫描所有的bean和包声明,你需要添加添加@ComponentScan注解来扫描你指定组件所在的包,看如下代码:1
2
3
4
5
6
7
8
9import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
()
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
评论加载中