文章预览
Spring Boot 简介
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
简化Spring应用开发的一个框架
整个Spring技术栈的一个大整合
J2EE开发的一站式解决方案
微服务
微服务:每一个功能元素最终都是一个可独立替换和独立升级的软件单元。
详情参考:
微服务文档
环境准备
jdk1.8:Spring Boot 推荐jdk1.7及以上
maven3.x:maven 3.3以上版本
IntelliJIDEA:或者STS
SpringBoot 1.5.9.RELEASE:1.5.10
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设置
把maven整合到idea。
使用SpringBoot创建一个HellWorld应用
功能:浏览器发送hello请求,服务器接受请求并处理,响应
Hello World
字符串。
1、创建一个maven工程(spring-boot-01-helloworld)
项目目录:
2、在pom.xml中导入spring boot相关的依赖
< parent>
< artifactId> spring-boot-dependencies</ artifactId>
< groupId> org.springframework.boot</ groupId>
< version> 1.5.10.RELEASE</ version>
</ parent>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
</ dependencies>
3、编写一个主程序
HelloWorldMainApplication:
package com. keafmd;
import org. springframework. boot. SpringApplication;
import org. springframework. boot. autoconfigure. SpringBootApplication;
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main ( String[ ] args) {
SpringApplication. run ( HelloWorldMainApplication. class , args) ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
4、编写相关的Controller
HelloController:
package com. keafmd. 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 World!" ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
5、运行主程序
运行结果:
打开浏览器访问:http://localhost:8080/hello
OK,至此,第一个SpringBoot的HelloWorld就大功告成了。【amazing~】
简化部署
1、我们在pom.xml文件中假如以下代码:
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
< executions>
< execution>
< goals>
< goal> repackage</ goal>
</ goals>
</ execution>
</ executions>
</ plugin>
</ plugins>
</ build>
2、然后,我们将应用打包
3、然后再target文件夹下就可以看到
spring-boot-01-helloworld-1.0-SNAPSHOT.jar
4、复制到桌面(随便哪,个人选择),打开cmd窗口,切换到jar包所在位置,我的是桌面,然后输入:
java -jar spring-boot-01-helloworld-1.0-SNAPSHOT.jar
,运行效果如下。
5、打开浏览器访问:http://localhost:8080/hello,同样可以看到HelloWord
这样的部署就变得十分简单了。
以上就是SpringBoot入门教程(超详细)的全部内容。
看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “
一键三连
” 了吗,没错点它[哈哈]
加油!
共同努力!
Keafmd
………………………………