您的位置:首页 > 编程语言 > Java开发

初尝Spring Boot

2017-01-04 15:05 417 查看
辛苦堆砌,转载请注明出处,谢谢!

        如果说Spring带来了Bean的管理上的便捷性,Spring Boot则是在其基础上进一步简化了项目配置,实现了尽可能多的自动配置。本文尝试使用Spring Boot,并通过一个简单的Demo,看看Spring Boot为我们的配置带来了哪些帮助。

        本人使用Eclipse开发,为了方便Spring开发,安装了Spring Tools Suits,如果没有安装,可以从Help->Eclipse Marketplace搜索安装。

        安装完成后,New->Project...->Spring->Spring Starter Project开始创建Spring Boot项目。



下一步之后,填写必要的信息



继续下一步,会让你选择需要使用的Starter,我们为了做示例,只选择Web和Thymeleaf,这里使用Thymeleaf是因为Spring Boot默认不支持JSP,如果需要使用JSP,网上可以查找文章,主要是要设置视图前缀和后缀,并且保证运行在tomcat容器中,而不能很方便的以Spring
Boot App的方式运行。



继续下一步,然后Finish即可。这样,我们就创建好了一个Spring Boot的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.yjp</groupId>
<artifactId>testspringboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>testspringboot</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


其中Spring Boot自动引入了spring-boot-starter-web,spring-boot-starter-thymeleaf和spring-boot-starter-test依赖,web和thymeleaf是我们选择的,test则是Spring
Boot为我们自动添加了测试相关的依赖。再简单看一下我们的项目目录



        java文件主要有两个,一个是TestspringbootApplication.java,一个是TestspringbootApplicationTest.java,前者就是对应我们的配置类,此时Spring
Boot已经为我们完成了必要的配置,而后者是测试类。另外,还有static,templates两个目录,分别放置静态资源和页面模版。最后application.properties属性文件,可以让我们定义应用的属性,还可以用来覆盖Spring Boot的默认配置,比如server.port等。
        接下来,我们做一个Hello World,看看如果想要构建一个Spring Boot应用程序,我们需要做些什么基本的工作。首先看看页面,放在templates目录下,使用Thymeleaf
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hello World</title>
</head>
<body>
<h1 th:text="${helloWorld}">Hello</h1>
</body>
</html>
然后是控制器:
package com.yjp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloWorldController {

@RequestMapping(method=RequestMethod.GET, value="/")
public String helloWorld(Model model) {
model.addAttribute("helloWorld", "Hello World");
return "hello_world";
}
}
处理"/"的GET请求,然后渲染视图hello_world,传过去模型参数Hello World,仅此而已。现在在项目上右键,Run As->Spring Boot App,然后通过浏览器访问即可。

        总之,可以看到,使用Spring Boot可以极大限度的减少我们的配置工作,但是否说之前的配置就是白学了,当然不是,如果我们需要覆盖Spring Boot的配置,方法之一就是我们重新实现我们自己的配置。另外一种方式是在application.properties中去做配置,Spring Boot给了我们很多属性可以在application.properties中进行覆盖,但我们也应当知道我们配置了什么。例如做Demo时我就在application.properties中重写了一项配置

spring.thymeleaf.cache=false

我们关闭了Thymeleaf的缓存,默认配置为true,也就是启用缓存,缓存不方便我们调试,为了实时看到页面的修改,我们在调试时可以将该项配置设置为false,来关闭缓存。

        Spring Boot还是能够在很大的程度上减小我们的配置工作的,很方便我们进行快速的开发。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: