您的位置:首页 > 编程语言 > Java开发

Spring-Boot(一) 环境搭建

2017-02-26 00:00 113 查看
摘要: Spring发展到现在,在java框架中一直在坚守,也在不断的改进。
Spring-Boot的出现,并不是什么新技术,而是简化我们的开发步骤和配置,让我们愉快的玩耍吧。
http://nealma.com/2016/04/20/spring-boot-1-how-to-use/
### 前言

Spring发展到现在,在java框架中一直在坚守,也在不断的改进。
Spring-Boot的出现,并不是什么新技术,而是简化我们的开发步骤和配置,让我们愉快的玩耍吧。

开发环境:
OS: Mac 10.11.6
IDE: IDEA 2016.2
Build: Maven

### Run

Spring-Boot是从run开始的,所以你的项目必须包含类似以下的一段代码
我习惯于放在com.domain.project包下

```java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```

### POM依赖

```xml
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
```

当然也可以不依赖,使用scope=import,没有尝试过。

```xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</parent>
```

### 使用Spring-Boot-Maven插件

将项目打包成“flat jars”,独立可执行jar包

```xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
```

### Starter POMs

starts是开箱即用,方便的的依赖集合,不用在像spring那样一个一个的配置依赖了
只需要引入如下就可以跑起来了

```
...

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency

...

```

### 跑起来

```
mvn clean spring-boot:run
```

### 多个@SpringBootApplication处理

现实工作中,不止一个项目,而是多个子项目并存,这个时候,你有多个start-class,就要用到下面给每个子项目设定start-class
```
<properties>
<start-class>com.SpringBootWayApplication</start-class>
</properties>
```
否则,你在打包时会出现下面的错误,意思是“我找不到唯一的start-class,你给我指定下”,

```
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.0.RELEASE:repackage (default) on project xxx: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.4.0.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.x.Application, com.y.Application]

```
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: