我们在SpringBoot学习之路(一)已经充分了解了SpringBoot诞生的必然性,那么我们来尝试一下新建一个SpringBoot的Demo来体验一下由SpringBoot带来的快速开发。
1.这是我们Demo截图,新建一个meavn项目.
2.接下来是各个文件的内容.作用都有注释.
pom.xml
<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>
<!-- SpringBoot父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<groupId>com.warpfuture</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 导入依赖 -->
<dependencies>
<!-- SpringBoot的web支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- SpringBoot的插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 资源文件拷贝插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
HelloApplication.java
package com.warpfuture.springboot.demo;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller//控制层SpringMVC
@SpringBootApplication//SpringBoot的应用(根据依赖的jar包自动配置)
//排除某个框架自动配置(例如redis)@SpringBootApplication(exclude= {RedisAutoConfiguration.class})
@Configuration//表示该类是一个xml配置
public class HelloApplication {
@RequestMapping("hello")
@ResponseBody
public String hello(){
return "hello world by-JMX!";
}
public static void main(String[] args) {
SpringApplication app=new SpringApplication(HelloApplication.class);
//关闭Spring的字体Mode
app.setBannerMode(Banner.Mode.OFF);
//SpringApplication运行的类必须带@SpringBootApplication
app.run(args);
}
}
application.properties
#SpringBoot全局配置文件,必须命名为application.properties或者application.yml
#修改Tomcat端口号
server.port=8080
#修改Servlet的dispatcherServlet拦截规则
server.servlet-path=*.html
3.OK,Demo已经搭建完毕,我们直接运行 HelloApplication.java这个java文件.控制台打印出
代表我们的Demo已经启动成功.
4.打开浏览器在地址栏里面输入 http://127.0.0.1:8080/hello.html 网页显示如下
看,一个简单的SpringBoot的Demo就搭建成功了。。。。。
Demo源码:点击打开链接