Spring boot 小Demo
首先配置好Maven环境,并配置到你的开发工具中。
1.去官网下载Maven压缩包。
下载后缀名为zip的那个。
![](https://images2015.cnblogs.com/blog/733341/201703/733341-20170331172538242-347539165.png)
下载完成后解压到你的某个硬盘内。我的路径是D:\apache-maven-3.3.9
然后把这个路径添加到你电脑的环境变量中去。
打开你的DOS输入命令mvn --version测试成功界面为
![](https://images2015.cnblogs.com/blog/733341/201703/733341-20170331172613945-1269036478.png)
2.配置到你的开发工具当中
我这里用的是myeclipse8.6版本。
windows-preferences-maven
![](https://images2015.cnblogs.com/blog/733341/201703/733341-20170331172714586-1677496284.png)
添加你的maven路径并应用。
改写settings.xml在第53行左右,添加一个你自己创建的文件夹作仓库路径。
改写settings.xml在第53行左右,添加一个你自己创建的文件夹作仓库路径。
![](https://images2015.cnblogs.com/blog/733341/201703/733341-20170331172733352-1341487746.png)
完成后,为下图所示。
![](https://images2015.cnblogs.com/blog/733341/201703/733341-20170331172746617-558632189.png)
以上就是所有maven的配置。
下面就进入我们的Speing boot demo 了。
1.新建一个maven项目
项目加载完后大致为下图的构造,此处要注意,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 >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>
|
把需要用到的包加载进来。
![](https://images2015.cnblogs.com/blog/733341/201703/733341-20170331172920352-261572649.png)
如图,只需编写Example.java和index.jsp即可完成一个最基本的Demo
package SpringBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController ;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/" )
String home(){
return "Hello World!" ;
}
public static void main(String[] args) {
SpringApplication. run(Example.class, args);
}
}
|
index.jsp
<html>
<body>
<h2> Hello World!Spring Boot</h2 >
</body>
</html>
|
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name >Archetype Created Web Application </display-name>
</web-app>
|
右击项目运行,选择你写的Example.java.等待项目跑起。
![](https://images2015.cnblogs.com/blog/733341/201703/733341-20170331173102289-153165327.png)
跑完后在地址栏输入http://localhost:8080/index.jsp即可看到Html的内容
![](https://images2015.cnblogs.com/blog/733341/201703/733341-20170331173124133-582266517.png)
至此一个简单的Spring Boot 的小Demo就完成了。