1、Spring Boot介绍
1)独立运行的spring项目,运行一个spring boot项目只需要通过java -jar **.jar来运行。
2)Spring使用java配置和注解配置组合,Spring Boot不需要任何xml配置即可实现Spring配置。
3)SpringBoot的优缺点:
优点: (1)快速构建项目
(2)对主流开发框架的无配置集成。
(3)项目可独立运行,无须外部依赖servlet容器。
(4)极大地提高了开发、部署的效率。
(5)与云计算的天然集合。
缺点: (1)如果你不认同spring容器,这也许是它的缺点。
2、Spring Boot的快速搭建
打开浏览器,在地址栏输入 http://start.spring.io/
3、使用IDEA来新建Spring Boot项目
A、第一个SpringBoot应用
(1)新建Spring Initializr项目
(2)填写项目信息 --java version :1.7 ( java version版本一定要根据jdk的版本来配)
(3)选择web
(4)next---finish
(5)代码如下:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>girl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>girl</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</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-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>
GirlApplication.java(在src/main/java/com.example包中)
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication//开启自动配置
public class GirlApplication {
//main方法作用:作为项目启动的入口
public static void main(String[] args) {
SpringApplication.run(GirlApplication.class, args);
}
}
GirlController.java(在src/main/java/com.example包中)
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GirlController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String say() {
return "Hello Spring Boot!";
}
}
运行GirlApplication.java
运行结果: