SpringBoot入门—搭建HelloWord应用
目录
- Spring Boot 简介
- 环境准备
- 使用SpringBoot创建一个HellWorld应用
- 简化部署
Spring Boot 简介
Spring Boot 是所有基于 Spring 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。
环境准备
环境约束
- –jdk1.8:Spring Boot 推荐jdk1.7及以上;java versi on "1.8.0_112"
- –maven3.x:maven 3.3以上版本;Apache Maven 3.3.9
- –IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64、 STS
- –SpringBoot 1.5.9.RELEASE:1.5.9;
配置环境准备
- MAVEN设置
打开maven 的settings.xml配置文件,找到Profiles标签添 加;
<profile>
<id>jdk‐1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
- IDEA设置
我的Mac版本,与Windows略有区别,如果找不到相应按钮,后台私聊我。
整合maven进编译器;
![47f1e3241cfa6268a399848f4ac4bb99.png](https://i-blog.csdnimg.cn/blog_migrate/2d356ab4db28168fc4996aa60b78631a.jpeg)
![ad95bcc1fe28953058fefbc3934c5105.png](https://i-blog.csdnimg.cn/blog_migrate/bddf703da43aa69912a10b77857a18d3.jpeg)
使用SpringBoot创建一个HellWorld应用
首先,创建一个项目:
![56a59c033e1feca2aeb4ba9d472039fd.png](https://i-blog.csdnimg.cn/blog_migrate/f688593022781c2b504bfde60a945b44.png)
![105b144d1578e8edb662e1f8057e5b00.png](https://i-blog.csdnimg.cn/blog_migrate/9ec4b56315d18e626f16d072fafec824.jpeg)
![67f923a446be0a0d476d273f5a19c9dc.png](https://i-blog.csdnimg.cn/blog_migrate/366ebbdbcf807ccd75df6ec9147f5cc5.png)
这样项目就创建好了, 接下来在pom.xml中导入springboot相关依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
然后编译器会下载对应的依赖文件。
![a9b3d489b92fbb56f7c0d0177d7a099d.png](https://i-blog.csdnimg.cn/blog_migrate/59cb7fae25b097a1585e6ba44a3a3843.jpeg)
![8c4f9f2094ea66986223abc7ab5474aa.png](https://i-blog.csdnimg.cn/blog_migrate/ec2c945e81c2d720219ef968cf5e27c9.jpeg)
创建一个HelloWorldMainApplication类:
![86dc435b4faefd0c83ac8e56213cb44c.png](https://i-blog.csdnimg.cn/blog_migrate/973ad93233e51764328203d4fbdf1d7c.png)
代码如下:
package com.zzz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWordMainApplication {
public static void main(String[] args){
//spring应该启动起来
SpringApplication.run(HelloWordMainApplication.class,args);
}
}
然后创建Controller类:
![b06f56b38a5f417b315cc1d2757f711f.png](https://i-blog.csdnimg.cn/blog_migrate/fbbb262a718e580367c0492b78807fa2.png)
package com.zzz.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello Word !";
}
}
然后通过HelloWorldMainApplication类里的main方法,运行项目:
![7790ccec22e8e66467f6494788a3e73c.png](https://i-blog.csdnimg.cn/blog_migrate/d910f62df4ac3d74946bdac87e215956.jpeg)
编译完成后,打开浏览器:
访问http://localhost:8080/hello
出现下图结果,说明helloword项目就完成了!
![4d98463fad81fe489c03ecd9a7724422.png](https://i-blog.csdnimg.cn/blog_migrate/4e10c885bf56357b444b03dd27926a79.jpeg)
简化部署
- 引入插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 生成jar包
![a2b514c228f3da4ba695a730311d8183.png](https://i-blog.csdnimg.cn/blog_migrate/a6458442957e3fa55ecf7440cdfe756a.jpeg)
- cmd进入jar包所在目录,并执行jar包
![02dc04d8a95f9621e50c968e0da30342.png](https://i-blog.csdnimg.cn/blog_migrate/fdc8f2c751da6f03ab93bdaa90c62b71.jpeg)
![765778bbb0f7726666b2d1165e727e87.png](https://i-blog.csdnimg.cn/blog_migrate/4f4c081f24d91a1d0b2b7dd83763f477.jpeg)
4.运行以后,访问:http://localhost:8080/hello
出现下图说明成功:
![4d98463fad81fe489c03ecd9a7724422.png](https://i-blog.csdnimg.cn/blog_migrate/4e10c885bf56357b444b03dd27926a79.jpeg)