Spring boot快速入门

1、Spring boot 的特点

1 为开发者提供Spring快速入门体验
2 内嵌tomcat和jetty容器,不需要部署WAR文件到WEB容器就可以独立运行应用
3 提供许多基于Maven的pom配置模板来简化工程配置
4 提供实现自动化配置的基础设施
5 提供可以直接在生产环节中使用的功能
6 开箱急用,没有代码生成,也无需配置XML文件,支持修改默认值来满足特定需求

2、快速入门

配置pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5 
 6     <parent>
 7         <groupId>org.springframework.boot</groupId>
 8         <artifactId>spring-boot-starter-parent</artifactId>
 9         <version>1.3.3.RELEASE</version>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12     <artifactId>test</artifactId>
13     <name>Spring4.x</name>
14     <!--添加一个Boot web启动器,spring-boot-starter-web内部封装了spring-web、spring-webmvc、jackson-databind等模块依赖 -->
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20     </dependencies>
21 </project>

Demo类

 1 package com.smart.web;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController
 9 @EnableAutoConfiguration
10 public class Demo{
11     @RequestMapping("/")
12     public String inde(){
13         return "欢迎超级无敌大帅哥登录";
14     }
15 
16     public static void main (String[] args) throws Exception {
17         SpringApplication.run(Demo.class,args);
18     }
19 }

直接运行Demo类会启动一个运行于8080端口的内嵌Tomcat服务,在浏览器中访问“http://localhost:8080”,即可看到如下页面

3、安装配置

这里是给予Maven环境的配置:Spring boot依赖Maven3.2+,如果基于Maven环境配置,则需要确定环境以及配置好Maven,如未配置可参考http://www.cnblogs.com/lwx521/p/7663070.html

pom.xml配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5 
 6     <parent>
 7         <groupId>org.springframework.boot</groupId>
 8         <artifactId>spring-boot-starter-parent</artifactId>
 9         <version>1.3.3.RELEASE</version>
10     </parent>
11     <modelVersion>4.0.0</modelVersion>
12     <artifactId>chapter3</artifactId>
13     <name>Spring4.x</name>
14     <packaging>war</packaging>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-jetty</artifactId>
23         </dependency>
24 </dependencies>
25 <build>
26         <plugins>
27             <plugin>
28                 <groupId>org.springframework.boot</groupId>
29                 <artifactId>spring-boot-maven-plugin</artifactId>
30             </plugin>
31   </plugins>
32     </build>
33 </project>
原文地址:https://www.cnblogs.com/lwx521/p/7688751.html