SpringBoot初始教程之项目结构(一)
2017-07-12 17:34
375 查看
SpringBoot初始教程之项目结构
1 简介
spring Boot makes it easy to create stand-alone, production-grade Spring based Applicationsthat you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Features
Create stand-alone Spring applications
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
Provide opinionated ‘starter’ POMs to simplify your Maven configuration
Automatically configure Spring whenever possible
Provide production-ready features such as metrics, health checks and externalized configuration
Absolutely no code generation and no requirement for XML configuration
上述是官方的英文讲解、大概翻译后如下:
SpringBoot在建立生产中的独立程序上非常简便、只需要一些简便的配置就能运行起来。大致有如下特点:
创建独立的Spring applications
能够使用内嵌的Tomcat, Jetty or Undertow,不需要部署war
提供starter pom来简化maven配置
自动配置Spring
提供一些生产环境的特性,比如metrics, health checks and externalized configuration
绝对没有代码生成和XML配置要求
2. 快速开始
<?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"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-1</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.1.RELEASE</version> </plugin> </plugins> </build> </project>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
30
31
32
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
30
31
32
这块统一用spring-boot-starter-parent做为parent 所以
spring-boot-starter-web不用填写版本号
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent>1
2
3
4
5
6
7
1
2
3
4
5
6
7
下面这个插件是用来运行Springboot的,通常有两种方式可以运行SpringBoot,一种是通过直接运行main方法,另外一种是通过使用下面的插件运行。
两种方式有差别,一旦项目中需要访问资源的时候就需要通过插件运行,否则无法访问到资源
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.1.RELEASE</version> </plugin>1
2
3
4
5
6
7
1
2
3
4
5
6
7
上面是maven配置,接下来需要配置整个程序的入口,
AppApplication
@SpringBootApplication public class AppApplication { public static void main(String[] args) throws Exception { SpringApplication.run(AppApplication.class, args); } }1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
下面写了一个简易的controller,只要运行main方法或者插件,就能够正常访问了。这块需要注意,controller跟AppApplication放在一个包下面,
如果不再一个下面需要配置扫描的包
@RestController public class IndexController { @GetMapping("/index") public ResponseEntity helloWord() { return ResponseEntity.ok("hello word"); } }1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
3.配置详解
@SpringBootApplication这个注解是一个组合注解,聚合了多个注解的功能,具体源码如下
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)) public @interface SpringBootApplication { //排除自启动项 Class<?>[] exclude() default {}; //排除自动启动的beanName String[] excludeName() default {}; //扫描包 @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") String[] scanBasePackages() default {}; //扫描类 @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") Class<?>[] scanBasePackageClasses() default {}; }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
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
@EnableAutoConfiguration这个注解是用来启动SpringBoot中的自动配置项目,这个注解是必须加上的,否则无法正常使用因为SpringBoot
默认配置了很多配置项,如下图所示配置在这里面的配置项都会自动进行配置,一部分需要引用其他jar包配置才会生效
application.yaml 文件详解
application.yaml是整个应用程序的配置文件,SpringBoot自动加载,SpringBoot提供针对各种组件的都可以通过application.yaml进行配置,后续再进行详解。 相关文章推荐
- SpringBoot初始教程之项目结构(一)
- SpringBoot初始教程之项目结构(一)
- SpringBoot基础教程及框架整合(二)----项目结构简介
- Maven快速使用教程(二) spring boot 项目构建
- 【SpringBoot教程二】:RESTfull API简单项目的快速搭建
- SpringBoot初始教程之日志处理(二)-1
- SpringBoot初始教程之Tomcat、Jetty优化以及Https配置(九)
- SpringBoot学习之路:02.第一个程序Hello World及项目结构介绍
- SpringBoot初始教程之热部署(五)
- Maven快速使用教程(二) spring boot 项目构建
- SpringBoot初始教程之统一异常处理(三)
- SpringBoot初始教程之日志处理(二)
- SpringBoot初始教程之统一异常处理(三)
- IDEA新建Springboot项目(图文教程)
- SpringBoot项目结构介绍
- SpringBoot基础教程及框架整合(一)----IDEA创建SpringBoot项目
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- SpringBoot初始教程之数据库连接池(druid)(六)
- SpringBoot初始教程之数据库连接池(druid)
- [Gradle中文教程系列]-跟我学Gradle-8.7.多模块项目之 - spring boot + gradle + 构建公共jsp页面的多模块项目