SpringBoot应用与原理之SpringBoot入门

一 本章概述

带你使用IntelliJ IDEA开发和部署第一个基于SpringBoot的Web应用
介绍一些基于SpringBoot应用的特性,例如SpringBoot的全局配置,应用打包,将项目上传至Github等等。

二实现基于SpringBoot的web应用

在介绍SpringBoot之前,我们先使用IntelliJ IDEA创建一个SpringBoot项目,项目最终会(project)包含了SpringBoot集成主流框架的所有模块(module),这里首先创建一个名为springboot的项目,同时包含springboot-web模块。

2.1 初始化project

1 创建项目
创建项目

2 基于spring initializr模板创建springboot项目
基于spring initializr模板

3 设置项目元数据
设置项目元数据

4 选择 SpringBoot的版本
选择 SpringBoot的版本

5 设置项目名称和路径
设置项目名称和路径

6 项目结构一览
项目结构一览

因为springboot作为project,而不会有任何的业务代码,业务代码都会存放在对应的模块(module)中,所以可以删除IntelliJ IDEA自动创建的src目录。

2.2 初始化Module

1 新建Module
新建Module

2 设置module元数据
设置module元数据

3 选择SpringBoot的Web模块
选择SpringBoot的Web模块

4 设置模块名称和路径信息
设置模块名称和路径信息

2.3 实现基于SpringBoot的Web应用

其实实现起来非常简单,只需要编写一个Controller,提供一个访问接口(方法)即可。

package com.ekeyfund.framework.springboot.web.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * IndexController
 *
 * @author tony 18601767221@163.com
 * @create 2017-09-14-下午4:51
 * @see
 * @since JDK1.8u141
 */
@RestController
public class IndexController {


    @GetMapping("/index")
    public String index(){

        return "My First SpringBoot Application";
    }
}

同时通过application.properties给tomcat服务器设置端口,上下文和编码信息

server.port=9999
server.context-path=/springboot-web
server.tomcat.uri-encoding=UTF-8

然后运行SpringbootWebApplication类的main方法
启动SpringBoot应用

或者可以通过使用SpringBoot通过配置Maven Plugin的方式使用,完整的配置如下:
只需要在properties中添加变量start-class,然后在plugin里指定即可。

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <start-class>com.ekeyfund.framework.springboot.web.SpringbootWebApplication</start-class>
    </properties>

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                    <layout>ZIP</layout>
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

然后可以使用springboot的插件 springboot:run运行
spring boot run

通过浏览器访问:http://127.0.0.1:9999/springboot-web/index

效果如下图所示:
效果如下图所示

三 SpringBoot Web应用解析

3.1 入口类和@SpringBootApplication

之前在创建springboot-web模块时自动生成了SpringbootWebApplication类,该类作为SpringBoot应用的启动类,负责运行SpringBoot项目。

package com.ekeyfund.framework.springboot.web;

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

/**
 * SpringBoot-web模块的启动类
 */
@SpringBootApplication
public class SpringbootWebApplication {

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

@SpringBootApplication是一个组合注解,包含了@SpringBootConfiguration(作用相当于 @Conifiguration)
@EnableAutoConfiguration(根据类路径的jar包依赖为当前项目进行自动配置)和@ComponentScan(组件扫描装配)

如果想禁用特定的自动配置,可以使用@SpringBootApplication注解的exclude参数

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //禁用数据源自动配置

3.2 SpringBoot应用的配置文件

springboot应用使用一个全局的配置文件application.properties或者application.yml(个人比较喜欢properties,后面的配置都是基于properties而不是yml)放置在src/main/resources目录下。
之前在application.properties文件中修改过tomcat8.5的配置信息,后期开发时还可以将数据源,ORM,日志,消息中间件,缓存中间件等等以及项目的业务相关配置在该文件中。
而在开发的过程中往往会涉及到不同的环境(dev,fat,uat,pro)会有不同的配置,这时可以结合maven的profiles配置和springboot的profiles来实现。

通常情况下application.properties配置不用考虑环境的不同而会出现不同的配置。application.properties配置文件更改后如下:

spring.profiles.active=@profiles.active@
server.tomcat.uri-encoding=UTF-8

以springboot-web工程为例,首先添加四个不同环境的配置文件,分别是application-dev.properties,application-fat.properties,application-uat.properties和application-pro.properties,配置内容如下:

application-dev.properties

server.port=9999
server.context-path=/springboot-web

application-fat.properties

server.port=9989
server.context-path=/springboot-web

application-uat.properties

server.port=9979
server.context-path=/springboot-web

application-pro.properties

server.port=80
server.context-path=/

然后在springboot-web的pom.xml中添加profiles配置
分别定义四个环境 dev,fat,uat,pro,默认启动开发环境

<profiles>

        <!-- 定义四个不同环境,分别是开发环境dev,测试环境fat,集成环境uat,生产环境pro-->
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>

            <!-- 默认激活开发环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>


        <profile>
            <id>fat</id>
            <properties>
                <profiles.active>fat</profiles.active>
            </properties>

        </profile>


        <profile>
            <id>uat</id>
            <properties>
                <profiles.active>uat</profiles.active>
            </properties>


        </profile>

        <profile>
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>


    </profiles>

然后处理各个环境的application.properties,在build节点中处理需要过滤和包含的配置文件

<build>
        <resources>
            <!-- 指定打包时需要过滤的文件-->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>application-dev.properties</exclude>
                    <exclude>application-fat.properties</exclude>
                    <exclude>application-uat.properties</exclude>
                    <exclude>application-pro.properties</exclude>
                </excludes>


            </resource>

            <!-- 指定打包时需要包含的配置文件-->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <!--${profiles.active}会根据指定的profile动态替换-->
                    <include>application-${profiles.active}.properties</include>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>

    </build>

完成上述配置之后可以使用maven命令来实现打包测试

MacBookPro:springboot tony$ mvn clean package -Dmaven.test.skip=true -P fat

使用 mvn clean package -Dmaven.test.skip=true -P fat表示打包项目,环境为fat。
-P后面的选项可以是之前配置的profile,例如dev,fat,uat,pro。

打包完成后可以去对应的目录下解压查看对应环境的配置。

fat环境

测试案例

SpringBoot支持使用java -jar启动应用,因此这里只需要进入项目所在的target目录,直接使用java -jar 启动即可


MacBookPro:target tony$ pwd
/Users/tony/Documents/WS/java/training/springboot/springboot-web/target
MacBookPro:target tony$ java -jar springboot-web-0.0.1-SNAPSHOT.jar

观察启动日志
观察启动日志

访问地址:http://127.0.0.1:9989/springboot-web/index,效果如下图所示
效果如下图所示

3.3 SpringBoot的pom.xml

首先分析项目springboot的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.ekeyfund.framework</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>springboot</name>
    <description>SpringBoot Application</description>


    <!--
        当继承spring boot的pom之后会具备以下特性
        1.java默认的编译级别为1.6
        2. 源码的编码为UTF-8
        3.添加其他依赖可以省略版本的声明,默认会集成spring boot1.5.7的pom文件中的版本声明
        4.可以在application.properties中使用@..@结合maven的pom.xml中 profiles 配置的properties激活maven的profile

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




    <modules>
        <module>springboot-web</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- spring boot 考虑到最低的兼容性,默认的编译级别为1.6 这里改为1.8-->
        <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>

接下来重点说明几个的节点
定义项目打包的方式,因为springboot作为project,而业务代码通常是写在Module(例如springboot-web)中,因此定义为pom

<package>pom</package>

当继承spring boot的pom之后会具备以下特性
1.java默认的编译级别为1.6
2. 源码的编码为UTF-8
3.添加其他依赖可以省略版本的声明,默认会集成spring boot1.5.7的pom文件中的版本声明
4.可以在application.properties中使用@..@结合maven的pom.xml中 profiles 配置的properties激活maven的profile

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

添加spring boot的测试依赖,因为几乎所有的模块代码都需要测试,为了提高配置的复用性,因此定义在springboot中。

<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>

然后分析springboot-web的pom.xml
得益于maven的继承特性,springboot-web的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.ekeyfund.framework</groupId>
    <artifactId>springboot-web</artifactId>
    <name>springboot-web</name>
    <description>SpringBoot Web Application</description>

    <parent>
        <groupId>com.ekeyfund.framework</groupId>
        <artifactId>springboot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <start-class>com.ekeyfund.framework.springboot.web.SpringbootWebApplication</start-class>
    </properties>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                    <layout>ZIP</layout>
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <!-- 指定打包时需要过滤的文件-->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>application-dev.properties</exclude>
                    <exclude>application-fat.properties</exclude>
                    <exclude>application-uat.properties</exclude>
                    <exclude>application-pro.properties</exclude>
                </excludes>


            </resource>

            <!-- 指定打包时需要包含的配置文件-->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <!--${profiles.active}会根据指定的profile动态替换-->
                    <include>application-${profiles.active}.properties</include>
                    <include>application.properties</include>
                </includes>
            </resource>



        </resources>

    </build>


    <profiles>

        <!-- 定义四个不同环境,分别是开发环境dev,测试环境fat,集成环境uat,生产环境pro-->
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>

            <!-- 默认激活开发环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <id>fat</id>
            <properties>
                <profiles.active>fat</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <profiles.active>uat</profiles.active>
            </properties>

        </profile>

        <profile>
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
    </profiles>
</project>

这里主要关注springboot-web模块pom.xml文件的依赖和构建配置

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

spring-boot-starter-web会将开发web应用所依赖的类库以及Tomcat添加到项目的依赖中。
从IntelliJ IDEA的Maven插件视图中可以看出详细的依赖
spring-boot-starter-web

4 项目上传至GitHub

在将项目上传至Github之前需要先到官网注册账号,同时需要在IntelliJ IDEA中登录,还要配置好Git的ssh key,可以参考SpringBoot应用与原理之开发环境搭建
的版本控制章节。

4.1 .gitignore插件的使用

在项目上传至github之前需要先处理那些不需要的文件和目录,这里使用IntelliJ IDEA的.gitignore插件实现。
关于插件的安装非常简单,只需要找到设置->插件->浏览插件仓库
然后输入名字安装即可(需要翻墙),安装完成之后重启就可以使用。
这里写图片描述

在使用IntelliJ IDEA创建SpringBoot项目时,默认帮我们创建了一个.gitignore文件,安装完.gitignore插件之后,可以使用菜单将指定的文件或者目录添加到.gitignore文件中。

而之前的springboot项目有两个.gitignore文件,这里方便管理,把springboot-web模块的.gitignore文件删掉,只使用springboot目录下的.gitignore文件。

添加.gitignore文件如图所示
添加.gitignore文件如图所示

完整的.gitignore文件配置如下

###springboot ignore files
/.mvn/
/mvnw.cmd
/mvnw
/.idea/

##通用配置
*.idea
*.iml
*.DS_Store
*/target/

##springboot-web ignore files
/springboot-web/target/
/springboot-web/.mvn/
/springboot-web/mvnw
/springboot-web/mvnw.cmd
/springboot-web/springboot-web.iml

配置完成之后可以使用IntelliJ IDEA的隐藏忽略文件,效果如下
gitignore配置

4.2 将项目上传至github

IntelliJ IDEA集成了GitHub,可以很方便的将项目上传至Github,如下图所示
将项目上传至github

只需要填写仓库名称,远程名称和项目描述即可
注意这里的仓库不能是原来Github中已经有的仓库,必须是新创建的
仓库名称和项目描述

初始化提交文件列表
初始化提交文件列表

当点击OK时,遇到一个403的错误,如下图所示,创建仓库虽然成功,但是代码没有上传至github。
403

因为默认的配置是采用https协议上传文件
https上传文件

需要对.git目录下的config文件修改,使用ssh协议上传代码,将url的值修改为远程仓库的地址即可。
远程仓库地址

然后使用Intellij IDEA集成的Git插件将代码push即可
这里写图片描述

推送到远程的master分支
push mote

访问github地址https://github.com/ittimeline/springboot,查看提交的内容
提交的内容

重要说明:
移动终端更好的阅读方案,关注个人微信公众号: ittimeline,优快云文章内容会实时同步到该公众号中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值