springboot系列之-helloword入门

时间:2023-03-09 06:09:45
springboot系列之-helloword入门

springboot系列之-helloword入门

一. What: Spring Boot是什么?
以1.4.3.RELEASE为例,官方介绍为:http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/html/getting-started-introducing-spring-boot.html 。

简单来说,使用Spring Boot能够非常方便,快速地开发一个基于Spring框架,可独立运行的应用程序。

具体来说,Spring Boot集成了一些在项目中常用的组件,比如:嵌入式服务器,安全等。而且在使用Spring Boot开发应用程序时,完全不需要XML配置。

二. Why: 为什么Spring Boot能如此易用?
//TODO: 随后再详细介绍

三. How: 如何建立一个简单的Spring Boot应用程序?

环境要求:
1. JDK: Although you can use Spring Boot with Java 6 or 7, we generally recommend Java 8 if at all possible. 
2. Maven: Spring Boot is compatible with Apache Maven 3.2 or above.

示例项目:

1. 新建一个maven项目,并在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>org.chench</groupId>
<artifactId>springboot-hellworld</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <name>springboot-hellworld</name>
<url>http://maven.apache.org</url> <!-- 使用spring boot框架:声明spring boot为父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<!-- 开发web应用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<!-- 打包插件: 可将项目打包为独立执行的jar文件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2. 新建HelloWordApplication:

 @RestController
@EnableAutoConfiguration
public class HelloWordApplication { @RequestMapping("/")
public String helloworld() {
return "hello,world";
} public static void main(String[] args) {
SpringApplication.run(HelloWordApplication.class, args);
}
}

3.运行HelloWordApplication:
如果是在eclipse等开发环境中,直接运行: "Run As" -> "Java Application" 。
或者,直接将项目打包为可独立执行的jar文件: mvn clean package,将生成文件: springboot-hellworld-version.jar 。 执行如下命令启动项目: java -jar springboot-hellworld-version.jar

启动之后,在浏览器地址栏输入: http://localhost:8080/

springboot系列之-helloword入门

完毕!