01-SpringBoot起步

Spring Boot起步

本节内容

  • SpringBoot概述
  • SpringBoot特征
  • 建立一个简单的Spring Boot应用程序
  • Spring Boot运行过程

SpringBoot概述

SpringBoot应用程序简单示例

入口:SimpleWebApp.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SimpleWebApp {
public static void main(String[] args) {
SpringApplication.run(SimpleWebApp.class, args);
}
}

代码说明:

  • 这是一个Spring Boot入口程序
  • 利用@SpringBootApplication和以及在main方法中调用单例类SpringApplication是SpringBoot程序得以执行
  • 这里的run方法接收两个参数,一个是包括@SpringBootApplicatio注解的类,一个应用程序参数数组

控制器:SimpleWebController.java

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SimpleWebController {
	@RequestMapping("/")
	public String greetings(){
		return "<h1> Spring Boot Rocks in Java too!</h1>";
	}
}

代码说明:

  • @RestController表明这是一个Spring MVC控制器
  • @RequestMapping表明http请求的入口

运行Spring boot程序

使用Spring Boot CLI运行:

$ spring run *.java

使用maven运行:

$ mvn spring-boot:run

使用浏览器测试:http://localhost:8080/

将显示内容:Spring Boot Rocks in Java too!”.

Spring Boot应用场景

  • 建立云原生应用程序(Cloud Native Applications)
  • 节约开发和发布时间
  • 像Spring Boot Actuator(Spring中对应用程序进行监控和管理的模块)这样的非功能性需求
  • 内置web容器(tomcat,jetty)的web应用程序

Spring Boot使开发者从笨重的spring框架中解脱出来,更关注业务逻辑的开发

Spring Boot特性

  • SpringApplication单例类用于初始化Spring Boot应用程序
  • 不需要任何xml配置就可以建立Spring Boot应用
  • SpringApplicationBuilder单例类有助于你建立多层次的应用程序上下文
  • 可以通过多种方式配置Spring应用程序的事件和监听
  • 提供更简单的依赖配置和管理,对应web应用程序只需要在pom中包含spring-boot-start-web依赖即可
  • 通过Spring Boot Actuator做到非功能性需求开箱即用,监控应用程序的健康情况,内存情况等等
  • 通过@Enable注解来配置,包含或启用某些技术,比如:databases (SQL and NoSQL), caching,scheduling, messaging, Spring integration, batching等

建立第一个Spring Boot应用程序

安装Spring Boot CLI

Spring Boot Command Line Interface (CLI)可以帮助你建立Spring Boot应用程序

windows系统
  1. 下载zip包并解压:

下载地址:http://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.0.RELEASE/

  1. 配置环境变量:
    • JAVA_HOME指向jdk(jdk需要1.8以上版本,通过java -version 检查配置情况
    • SPRING_HOME指向Spring Boot CLI解压位置,并保证path路径中包含% SPRING_HOME%\bin,设置这些内容的目的是能够访问spring.bat以及spring脚本(配置成功后可以通过 spring --version查看spring 版本
  2. 建立基础项目结构
建立基于maven的项目
$ spring init  myapp
建立基于gradle的项目
$ spring init --build gradle myapp

说明

该命令行调用https://start.spring.io上的web服务,建立一个叫myapp的项目

项目结构类似下图:

在这里插入图片描述

如果包含更多特性,比如:web,jpa,maven等支持

$ spring init -dweb,data-jpa,h2,thymeleaf --build maven myapp --force

说明

这里的–force选项是强制覆盖以前的myapp目录

基于maven的spring boot项目

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>myapp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.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</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>

说明

  • parent依赖:包含spring框架的依赖,比如:Framework ( spring-core ), Spring Test ( spring-test)
  • starter部分是spring boot的实际依赖,包含web应用程序的相关依赖,比如:spring-core , spring-web , spring-webmvc , embedded Tomcat server
  • plugins部分包含打包jar,war的插件在使用mvn package命令时会使用此部分插件

使用Spring Initializr建立Spring Boot项目

通过网站http://start.spring.io使用Spring Initializr服务建立Spring Boot项目:

在这里插入图片描述

Spring Initilizr完整版界面:

在这里插入图片描述

第一个Spring Boot应用程序

需求

Spring Boot记事本

界面原型如下:

在这里插入图片描述

开发工具

使用sts建立Spring boot项目

sts下载地址:https://spring.io/tools/sts/all

开发步骤
  1. 打开sts,选择File > New > Spring Starter Project

在这里插入图片描述

输入内容:

fieldvalue
Namespring-boot-note
TypeMaven
PackagingJar
Java Version1.8
LanguageJava
Groupcom.train
Artifactspring-boot-note
Version0.0.1-SNAPSHOT
DescriptionDemo project for Spring Boot
Packagecom.train.springboot
  1. 选择所使用的技术
  • Web (Web)

  • Template Engines:Thymeleaf,处理和生成html,xml,JavaScript,CSS,比JSP更高效

  • sql(JPA,H2)

在这里插入图片描述

  1. 项目结构:

在这里插入图片描述

  1. 查看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.train</groupId>
	<artifactId>spring-boot-note</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-note</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.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-data-jpa</artifactId>
		</dependency>
		<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>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</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>
  1. 建立Note类
package com.train.springboot.domain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
public class Note {
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	private String title;
	private Date created;
	private String summary;
	
	@Transient
	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	
	
	public Note() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	public Note(String title, String summary,String date) throws ParseException {
		super();
	
		this.title = title;
		this.created = format.parse(date);
		this.summary = summary;
	}


	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
	public String getSummary() {
		return summary;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	
	public String getCreatedAsShort() {
		return format.format(created);
	}
	
	@Override
	public String toString() {
		return "Note [id=" + id + ", title=" + title + ", created=" + created + ", summary=" + summary + "]";
	}
	
}

说明

  • @Transient表示该属性无需持久化
  1. 编写持久层,需要编写接口并继承自JpaRepository
package com.train.springboot.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import com.train.springboot.domain.Note;
public interface NoteRepository extends JpaRepository<Note, Long>{
}

说明

  • JpaRepository是一个标记接口,Spring Data Repository引擎会识别它,并通过代理类实现CRUD(Create,Read,Update,Delete)

  • 通过一定的命名规范,你可以定制方法,比如findByTitleLike,findBySummary,findByTitleAndSummaryIgnoringCase

  • 所有行为默认都是支持事务的。

  • JpaRepository也有一些常规行为,比如排序,分页等

  1. 编写web控制器
package com.train.springboot.web;

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.train.springboot.repository.NoteRepository;

@Controller
public class NoteController {
	
	@Autowired
	private NoteRepository repo;
	
	@RequestMapping("/")
	public String index(Model model) {
		model.addAttribute("notes",repo.findAll());
		return "index";
	}
	
}
  1. 编写页面index.html(src/main/resources/templates)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring boot notes</title>
<link rel="stylesheet" type='text/css' media='all'
	href="css/bootstrap.min.css"></link>
<link rel="stylesheet" type='text/css' media='all'
	href="css/bootstrap-glyphicons.css"></link>
</head>
<body>
	<div class='container'>
		<h1>Spring Boot Notes</h1>
		<div class="panel panel-default" th:each="entry,status:${notes}">
  			<div class="panel-heading">
    			<h3 class="panel-title"><span th:text="${entry.title}">TITLE</span></h3>
  			</div>
  			<div class="panel-body">
    			<p> <span th:text="${entry.summary}">SUMMARY</span> </p>
  			</div>
  			<div class="panel-footer">
  				<p>
					<small class="text-muted">
						<i class="glyphicon glyphicon-time"></i>
						<span th:text="${entry.createdAsShort}">CREATED</span> 
					</small>
				</p>
			</div>
		</div>
	</div>
</body>
</html>

说明

  • xmlns:th="http://www.thymeleaf.org" html标签的xml命名空间属性说明使用Thymeleaf引擎
  • th:each指令:取得待遍历的集合,将每次迭代的对象放到entry中,索引放到status中
  • th:text指令:用于取得entry中的属性
  1. 添加Bootstrap css样式文件,文件夹位置:/spring-boot-note/src/main/resources/static/css

  2. 打开主应用程序,在建立项目时自动生成

package com.train.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootNoteApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootNoteApplication.class, args);
	}
}

修改该类,加入初始化数据:

package com.train.springboot;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.train.springboot.domain.Note;
import com.train.springboot.repository.NoteRepository;

@SpringBootApplication
public class SpringBootNoteApplication {
	
	@Bean
	InitializingBean saveData(NoteRepository repo) {
		return () ->{
			repo.save(new Note("周末爬山","本周末和朋友们爬上了大岭山","2018-10-11"));
			repo.save(new Note("逛沃尔玛","到沃尔玛购物,买一堆吃的","2018-11-10"));
			repo.save(new Note("读书时间","读《红楼梦》两小时","2018-11-12"));
			repo.save(new Note("打两小时游戏","大吉大利,今晚吃鸡","2018-11-18"));
		};
	}

	public static void main(String[] args) {
		SpringApplication.run(SpringBootNoteApplication.class, args);
	}
}

说明

  • saveData方法返回InitializingBean,当spring引擎建立SpringBootJournalApplication实例并初始化时会调用saveData方法
  1. 在sts中,选择SpringBootNoteApplication类,右键Run As > Spring Boot App,即可运行项目,打开浏览器,输入http://localhost:8080 可查看页面显示效果。

  2. 进一步扩展,做出服务接口,当访问http://localhost:8080/notes时返回json数据

    修改内容:修改NoteController控制器,添加一个新方法处理/notes请求,返回JSON数据

    package com.train.springboot.web;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.train.springboot.domain.Note;
    import com.train.springboot.repository.NoteRepository;
    
    @Controller
    public class NoteController {
    	
    	@Autowired
    	private NoteRepository repo;
    	
    	@RequestMapping(value="/notes",produces= {MediaType.APPLICATION_JSON_UTF8_VALUE})
    	public @ResponseBody List<Note> getNotes(){
    		return repo.findAll();
    	}
    	
    	@RequestMapping("/")
    	public String index(Model model) {
    		model.addAttribute("notes",repo.findAll());
    		return "index";
    	}
    	
    }
    

    使用浏览器进行测试:http://localhost:8080/notes

    返回结果:

    [{"id":1,"title":"周末爬山","created":"2018-10-10T16:00:00.000+0000","summary":"本周末和朋友们爬上了大岭山","createdAsShort":"2018-10-11"},{"id":2,"title":"逛沃尔玛","created":"2018-11-09T16:00:00.000+0000","summary":"到沃尔玛购物,买一堆吃的","createdAsShort":"2018-11-10"},{"id":3,"title":"读书时间","created":"2018-11-11T16:00:00.000+0000","summary":"读《红楼梦》两小时","createdAsShort":"2018-11-12"},{"id":4,"title":"打两小时游戏","created":"2018-11-17T16:00:00.000+0000","summary":"大吉大利,今晚吃鸡","createdAsShort":"2018-11-18"}]
    

Spring Boot是如何工作的

当我们运行SpringBootNoteApplication应用程序时,该类被标记一个@SpringBootApplication注解。该注解代码与下面类似:

package org.springframework.boot.autoconfigure;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
	Class<?>[] exclude() default {};
	String[] excludeName() default {};
	@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
	String[] scanBasePackages() default {};
	@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
	Class<?>[] scanBasePackageClasses() default {};
}

从上述源代码可以看出@SpringBootApplication是一个复合注解,包含了@Configuration ,@EnableAutoConfiguration ,@ComponentScan几个注解。

Spring Boot使用@SpringBootApplication注解进行自动配置,并识别你的所有组件。首先检查你的类路径,由于依赖了spring-boot-starter-web,它会将应用程序配置为web应用程序。它也会识别NoteController类,由于该类标记了@Controller和@RequestMapping注解,会将NoteController类当作web控制器。

spring-boot-starter-web中包含了Tomcat服务器依赖,Spring boot将使用tomcat作为web服务器并运行应用

Spring Boot自动配置小结
  • Spring Boot自动配置是Spring Boot的重要特性
  • Spring Boot根据你的类路径(包括pom.xml),注解,以及任何Java配置声明自动配置你的Spring Boot应用程序。
  • Spring Boot并不生成任何配置代码,而是在Java中通过代理类完成自动配置过程
  • @SpringBootApplication注解是一个复合注解,它包含@EnableAutoConfiguration ,@Configuration , 以及@ComponentScan注解,因此也属于自动配置
SpringApplication类
  • 用于在main方法中启动Spring boot应用程序,你需要传递要执行的类
  • 也可以通过在main方法中添加如下类似代码来执行
SpringApplication app = new SpringApplication(SpringBootSimpleApplication.class);
//add more features here.
app.run(args);

如何运行Spring Boot应用程序

  1. 在eclipse中,可以直接运行含有main方法的程序

  2. 在eclipse中,通过项目上右键,run as>maven build…>目标中输入 package进行打包,然后输入spring-boot:run 运行,或者打包后,在命令行下找到jar包(位于工作空间target下)通过如下命令运行:

    $ java -jar target/spring-boot-note-0.0.1-SNAPSHOT.jar
    
  3. 在安装了maven环境后,使用如下命令建立独立的应用程序:

$ mvn package

该命令会在目标文件夹建立jar文件,然后,可以通过如下命令执行jar文件:

$ java -jar target/spring-boot-note-0.0.1-SNAPSHOT.jar

最后,你可以通过浏览器进行测试:http://localhost:8080

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值