学习目标

  • 0.学习目标
  • 1. 常见企业项目模块结构
  • 1.1.构建SpringMVC模块结构 ,常见 web+service+model项目结构
  • 1.1.1 创建 项目 spring-boot-teach2-parent
  • 1.1.2 新建maven module类型项目 springboot-teach2-web
  • 1.1.2 类似步骤创建springboot-teach2-model,springboot-teach2-service
  • 2. 配置文件详解
  • 2.1 application.properties 配置文件
  • 2.2. yml配置文件语法
  • 2.3 配置文件与配置类的属性映射
  • 1、使用注解 @Value 映射
  • 2、使用注解 @ConfigurationProperties 映射
  • 2.4. 静态资源访问控制
  • 2.4.1. 代码方式
  • 2.4.2 .配置文件方式
  • 3. 了解SpringBoot的启动方式
  • 3.1 jar包形式打包
  • 3.2 war包形式打包发布
  • 4. 热部署
  • 源码地址


0.学习目标

  • 常见企业项目模块结构
  • 了解SpringBoot的配置
  • 了解SpringBoot打包启动方式
  • 了解SpringBoot热部署

1. 常见企业项目模块结构

1.1.构建SpringMVC模块结构 ,常见 web+service+model项目结构

1.1.1 创建 项目 spring-boot-teach2-parent

springboot单体项目结构按模块划分 springboot 多模块_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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<artifactId>sample.springboot.teach2.parent</artifactId>
	<groupId>sample.springboot.teach2</groupId>
	<version>1.0.0</version>
	<description>Spring Boot Data Elasticsearch Sample</description>
	<packaging>pom</packaging>
	
	<properties>
		<main.basedir>${basedir}/../..</main.basedir>
	</properties>
 
</project>

1.1.2 新建maven module类型项目 springboot-teach2-web

springboot单体项目结构按模块划分 springboot 多模块_maven_02

springboot单体项目结构按模块划分 springboot 多模块_maven_03


其pom.xml代码如下

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>sample.springboot.teach2</groupId>
    <artifactId>sample.springboot.teach2.parent</artifactId>
    <version>2.0.0.RELEASE</version>
  </parent>
  <groupId>sample.springboot.teach2</groupId>
  <artifactId>springboot-teach2-web</artifactId>
  <version>1.0.0</version>
   <packaging>jar</packaging>
  
  <name>springboot-teach2-web</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
 

  </dependencies>
</project>

1.1.2 类似步骤创建springboot-teach2-model,springboot-teach2-service

创建完成后目录如下图

springboot单体项目结构按模块划分 springboot 多模块_xml_04

1.1.3 构建3个模块之间的依赖关系

springboot单体项目结构按模块划分 springboot 多模块_xml_05


于是分别在springboot-teach2-web和springboot-teach2-service项目的pom.xml中增加上一级的依赖,具体如下

springboot-teach2-web 的pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>sample.springboot.teach2</groupId>
    <artifactId>sample.springboot.teach2.parent</artifactId>
    <version>2.0.0.RELEASE</version>
  </parent>
  <groupId>sample.springboot.teach2</groupId>
  <artifactId>springboot-teach2-web</artifactId>
  <version>1.0.0</version>
   <packaging>jar</packaging>
  
  <name>springboot-teach2-web</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
  	<dependency>
  		<groupId>sample.springboot.teach2</groupId>
  		<artifactId>sample.springboot.teach2.service</artifactId>
  		<version>1.0.0</version>
  	</dependency>

  </dependencies>
</project>

springboot-teach2-service的pom.xml如下

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>sample.springboot.teach2</groupId>
    <artifactId>sample.springboot.teach2.parent</artifactId>
    <version>2.0.0.RELEASE</version>
  </parent>
  <groupId>sample.springboot.teach2</groupId>
  <artifactId>sample.springboot.teach2.service</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>
  <name>springboot-teach2-service</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>sample.springboot.teach2</groupId>
      <artifactId>sample.springboot.teach2.model</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>
</project>

springboot-teach2-model的pom.xml如下

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
   	<artifactId>sample.springboot.teach2.parent</artifactId>
	<groupId>sample.springboot.teach2</groupId>
	<version>1.0.0</version>
  </parent>
  <groupId>sample.springboot.teach2</groupId>
  <artifactId>sample.springboot.teach2.model</artifactId>
  <version>1.0.0</version>
  
  <name>springboot-teach2-model</name>
  <packaging>jar</packaging>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    	<dependency>
  		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  </dependencies>
</project>

ok,项目依赖已构建完毕,那么我们模拟查询用户流程走一遍。
在model项目中新建User类

package sample.springboot.teach2.model;

/**
 * @author robin.jiao
 * @date 2020/03/04
 */
public class User {

    private String name;
    private String password;

    //set get 方法省略

}

在service项目中新建UserService和UserServiceImple类,如下

UserService

package sample.springboot.teach2.service;

import java.util.List;

import sample.springboot.teach2.model.User;

public interface UserService {

    List<User> queryAll();
}

UserServiceImple

package sample.springboot.teach2.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import sample.springboot.teach2.model.User;


@Service
public class UserServiceImpl implements UserService {


    @Override
    public List<User> queryAll() {
        List<User> result = new ArrayList<User>();
        User user1 = new User();
        user1.setName("张三");
        user1.setPassword("111");
        result.add(user1);
        return result;
    }

}

在web项目中新建UserController

package sample.springboot.teach2.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import sample.springboot.teach2.model.User;
import sample.springboot.teach2.service.UserService;

/**
 * @author robin.jiao
 * @date 2020/03/04
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/all")
    public List<User> queryAll() {
        return userService.queryAll();
    }
}

并新建启动类

Teach2Appliction

package sample.springboot.teach2;

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

}

在web项目中的resources文件夹下增加配置文件 application.proerties,并且设置用资源文件

server.port: 8081

执行启动类,效果如下

springboot单体项目结构按模块划分 springboot 多模块_xml_06

2. 配置文件详解

SpringBoot配置文件可以归为以下两种:

application.properties
application.yml(application.yaml)

2.1 application.properties 配置文件

上一步创建 application.properties 配置文件去覆盖 SpringBoot 的默认配置, 我们知道 Tomcat 默认打开端口是8080,可以通过 application.properties 配置文件去修改这个默认配置
properties 文件是以键值对的形式编写,可以看到,通过修改application.properties 配置文件,已经将 Tomcat 端口改为 8081,覆盖了之前的配置,由于 properties 文件在以往用的也比较多,较为常见,这里就不做过多讲述,主要还是对 yml 配置文件进行学习。
application.yml(application.yaml) 配置文件

YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML文件是以数据为核心的,比传统的xml方式更加简洁。YML文件的扩展名可以使用.yml或者.yaml。

2.2. yml配置文件语法

yml 的配置可以分为下面几种,我们先在 resources 文件夹下面创建 application.yml 配置文件

配置普通数据
配置对象数据
配置Map数据
配置数组数据

配置普通数据:

语法: key: value
eg:

name: onestar

注意:value前面有一个空格

配置对象数据:

语法1:
    key:
        key1: value1
        key2: value2
语法2:
    key: {key1: value1,key2: value2}
eg:

user:
    name: oneStar
    age: 18
    addr: yichun
 
 
#或者
user: {name: oneStar,age: 18,addr: yihcun}

注意:key1前面的空格个数不限定,在yml语法中,相同缩进代表同一个级别

配置Map数据:

配置Map数据和配置对象数据相同
eg:

map:
    key1: value1
    key2: value2

配置数组数据:

语法1
    key:
        - value1
        - value2
语法2
    key: [value1,value2]
eg:

city:
  - beijing
  - tianjin
  - shanghai
  - chongqing
  
#或者
city: [beijing,tianjin,shanghai,chongqing]
 
#集合中的元素是对象形式
student:
  - name: oneStar
    age: 18
    score: 100
  - name: twoStar
    age: 28
    score: 88
  - name: threeStar
    age: 38
    score: 90

注意:value1与之间的 - 之间存在一个空格

2.3 配置文件与配置类的属性映射

配置文件中的配置,不管是 properties 文件还是 yml 文件,有一些配置 SpringBoot 会自动去识别,比如 server.port=8888 配置,但有一些我们自定义的配置,SpringBoot 并不能自动识别,怎样才能让 SpringBoot 认识自己配置的信息呢,这里就有两种方法:

使用注解 @Value 映射

使用注解 @ConfigurationProperties 映射

1、使用注解 @Value 映射

我们可以通过@Value注解将配置文件中的值映射到一个Spring管理的Bean的字段上,这里以 yml 文件进行说明

application.yml 配置如下:

user:
  name: oneStar
  age: 18

实体Bean代码如下:

@RestController
public class UserController {

@Autowired
private UserService userService;

@GetMapping("/user/all")
public List<User> queryAll() {
    return userService.queryAll();
}

@Value("${user.name}")
private String name;
@Value("${user.age}")
private int age;

@GetMapping("/get-user")
public String getUser() {
    return "name=" + name + ",age=" + age;
}

}

运行后,浏览器访问地址:http://localhost:8081/get-user结果如下:

2、使用注解 @ConfigurationProperties 映射

使用 Value 映射能够精确的匹配到某一属性,但如果有很多属性,就带来了不便,因此,还可以使用 @ConfigurationProperties 映射,通过注解@ConfigurationProperties(prefix=“配置文件中的key的前缀”)可以将配置文件中的配置自动与实体进行映射

application.yml 配置如下:

user:
  name: oneStar
  age: 19

实体Bean代码如下:

@Controller
@ConfigurationProperties(prefix = "user")
public class UserController {
    private String name;
    private int age;
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    @RequestMapping("/get-user")
    @ResponseBody
    public String quick(){
        return "name="+name+",age="+age;
    }
}

运行后,浏览器访问地址:http://localhost:8081/get-user 结果如下:

springboot单体项目结构按模块划分 springboot 多模块_xml_07



属性notExists对应的配置信息,在配置文件中没有定义,则返回默认的data
    属性name对应的配置信息 user.name 在application.properties文件中是oneStar,但是返回了user(测试环境为mac,mac系统的用户名为user,为啥叫user?因为某某人…)
    造成这个的根源是application.properties的配置被更高优先级的系统配置覆盖了

注意:使用@ConfigurationProperties方式可以进行配置文件与实体字段的自动映射,但需要字段必须提供set方法才可以,而使用@Value注解修饰的字段不需要提供set方法

2.4. 静态资源访问控制

在我们开发Web应用的时候,需要应用大量的js、css、图片等静态资源。
springboot静态资源访问可通过两种方式配置

2.4.1. 代码方式

代码方式需要启用@EnableWebMvc注解,同时实现WebMvcConfigurer接口

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/*").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/**").addResourceLocations("file:/Users/guoxiang/Documents/product-template/");
    }

2.4.2 .配置文件方式

SpringBoot默认提供静态资源目录需要置于classpath下,目录名称符合如下规则:
/static
/public
/resources
/META-INF/resources
代码如下

spring:
  resources:
    static-locations:
    - classpath:/static/
    - file:/XXX/template/

需要注意的是:
如果启动了EnableWebMvc,则配置文件会失效,两种方式只能选其一
在配置的路径后面,必须以“/”结尾,否则配置无效

3. 了解SpringBoot的启动方式

3.1 jar包形式打包

以上案例是以jar形式打包的,关于如何打包,可以使用eclipse的默认方法,点击项目右键,

Run as》Maven Install,打包结果目录如下

springboot单体项目结构按模块划分 springboot 多模块_spring_08

上传到linux服务器后,可以使用java 命令启动,如下

java -jar springboot-teach2-web-1.0.0.jar

3.2 war包形式打包发布

springboot-teach2-web项目的pom文件,将jar改为war,然后按照上一步方式打包。

springboot单体项目结构按模块划分 springboot 多模块_xml_09

打包结果如下:

springboot单体项目结构按模块划分 springboot 多模块_spring_10

将war上传到服务器tomcat的webapps目录, 启动tomcat即可

4. 热部署

在应用正在运行的时候升级软件,却不需要重新启动应用。SpringBoot热部署就是在项目正在运行的时候修改代码, 却不需要重新启动项目.

SpringBoot提供了热部署方案,大大提高了开发效率, 因为频繁的重启项目,势必会浪费很多时间, 有了热部署后,妈妈再也不用担心我修改代码重启项目了~
springboot-teach2-web项目中的 pom文件中导入spring-boot-devtools依赖

<!--SpringBoot热部署-->
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
</dependency>

使用热部署的项目,在集成dubbo时,常常与遇到一些问题,此处不详细展开。