SpringBoot系列(1)
简介:
用来简化新Spring应用的初始搭建以及开发过程;
该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
特点:
1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件
3. 简化Maven配置
4. 自动配置Spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成和对XML没有要求配置
demo:
Hello World
一、准备工作
1、根据Maven工程特点,建立文档结果
myFirstProject
+-src
+-main
+-Java
+-resources
+-test
+-java
+-resources
2、再在src/main/java下依次建立文件夹com,example,myFirstProject,可以构成Maven工程包(package)-->com.example.myFirstProject,最后文档结构如图1
3、编制pom.xml,存于myFirstProject文件夹下,与src同级
<?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.example</groupId> <artifactId>myFirstproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.BUILD-SNAPSHOT</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- Add Spring repositories --> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
4、编制Application.java存于myFirstProject\src\main\java\com\example\myFirstProject下
package com.example.myFirstProject; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
5、编制Example.java,存于myFirstProject\src\main\java\com\example\myFirstProject下
package com.example.myFirstProject; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home(){ return "Hello World!"; } @RequestMapping("/hello/{myName}") String index(@PathVariable String myName){ return "Hello"+myName+"!!!"; } }
二、Maven工程导入
在eclipse中,依次点击file-->import-->Maven-->Existing Maven Projects-->Next-->Browse-->定位到myFirstProject文件夹-->确定--Finish
三、运行
1、在eclipse的工程myFirstProject上右击鼠标,选择Run as-->Java Application.
三、运行
1、在eclipse的工程myFirstProject上右击鼠标,选择Run as-->Java Application,
3、再次点击“OK”按钮
4、打开浏览器,输入http://localhost:8080
5、在浏览器中,输入http://localhost:8080/hello/SpringBoot