先介绍一下什么是spring boot.
Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置。
简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题---习惯大于约定。
Spring Boot默认使用tomcat作为服务器,使用logback提供日志记录。
一、加入maven依赖
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springb</groupId> <artifactId>springb</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!--对spring boot父项目的依赖--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core 启动项目需要调用改引用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.2.5.RELEASE</version> </dependency> <!-- Spring Boot 引用调用前端模板html页面 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>11.1.0.6.0</version> </dependency> <!-- devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现), 实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。 即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional><!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 --> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId><!-- <configuration> <fork>false</fork> 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart </configuration> --></plugin> </plugins> </build> </project>
二、写一个简单的Controller
package com.springboot; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.springboot.model.Student; import com.springboot.service.TestService; @Controller public class UserController { @Autowired private TestService tservice; @RequestMapping("/hello/{name}") public String hello(Model model) { // List<Student> list=tservice.getList(); return "hello"; } @RequestMapping("/sayhello/{name}") public String sayhello(Model model) { // List<Student> list=tservice.getList(); System.out.println(123); return "hello"; } }
四、启动java application类
package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class application extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(application.class); } public static void main(String[] args) { SpringApplication.run(application.class, args); } }
这里有两个新东西:@EnableAutoConfiguration 和 SpringApplication 。
@EnableAutoConfiguration 用于自动配置。简单的说,它会根据你的pom配置(实际上应该是根据具体的依赖)来判断这是一个什么应用,并创建相应的环境。
在上面这个例子中,@EnableAutoConfiguration 会判断出这是一个web应用,所以会创建相应的web环境。
SpringApplication 则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤:
直接用main启动就可以了。
- 创建一个合适的ApplicationContext实例 (取决于classpath)。
- 注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。
- 刷新application context,加载所有单例beans。
- 激活所有CommandLineRunner beans。
有趣的是spring boot 可以不再是使用jsp页面而可以选择使用html页面来作为你的前台页面。
在resource目录下创建个template目录,在该目录下添加test.html页面。