Spring Boot 2.X 学习日记——我的第一个web应用

什么是Spring Boot?

Spring Boot能够让我们更加轻松容易的构建基于Spring的Web应用。它的出现,使我们基本告别了SpringMVC时代的那些恶心的XML配置,我们基本能够零配置的构建一个属于自己的Web应用。

Spring Boot和SpringMVC相比,主要有以下几点不同:

  1. 启动方式不同。Spring Boot的项目,主要是通过java -jar的命令启动的。
  2. 不需要XML配置文件。Spring Boot的项目,所有的Bean都是通过在@Configuration标识的类中配置的。
  3. Starter。在Spring Boot中,官方提供了大量的Starter对第三方框架进行了一定程度的封装,我们在使用第三方框架时,只需引入对应的Starter就足够了,里面已经有了基础的Bean注册,如果不满足于实际生产,我们当然能够覆盖这些已注册的Bean。

创建项目

环境要求

IntelliJ IDEA

gradle 4.4+

jdk 1.8+

目录结构

-charpter1
	-src
		-main
			-java #代码
				-com.boot.first	#包
			-resources	#静态资源文件
		-test

build.gradle依赖

group 'spring-boot'
version '1.0-SNAPSHOT'
description '我的第一个spring-boot项目'

buildscript {
    ext {
        springBootVersion = '2.1.1.RELEASE'
    }

    repositories {
        maven {
            url('http://maven.aliyun.com/nexus/content/groups/public')
        }
    }

    dependencies {
        //spring-boot提供的gradle插件
        //用于spring的版本控制以及项目打包
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    maven {
        url('http://maven.aliyun.com/nexus/content/groups/public')
    }
}

dependencies{
    compile 'org.springframework.boot:spring-boot-starter-web'
}

主函数

tips:一个Spring Boot项目中,不可拥有多个main函数,否则在打包时,spring-boot-maven-plugin 将找不到主函数(主动指定打包主函数入口除外…

/**
 *	我的第一个Spring Boot应用
 *	Author : Ivan
 **/
@SpringBootApplication
@RestController
public class BootApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class,args);
    }

    @GetMapping("/")
    public String hello(){
        return "Hello World!";
    }
}

测试

运行BootApplication.main()方法后,启动日志如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2019-03-25 23:35:56.391  INFO 12179 --- [           main] com.boot.first.BootApplication           : Starting BootApplication on fangjiaxindeMacBook-Pro.local with PID 12179 (/Volumes/Elements/work/spring-boot/chapter1/out/production/classes started by ivan in /Volumes/Elements/work/spring-boot/chapter1)
2019-03-25 23:35:56.398  INFO 12179 --- [           main] com.boot.first.BootApplication           : No active profile set, falling back to default profiles: default
2019-03-25 23:35:57.456  INFO 12179 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-03-25 23:35:57.477  INFO 12179 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-25 23:35:57.477  INFO 12179 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2019-03-25 23:35:57.485  INFO 12179 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/ivan/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-03-25 23:35:57.561  INFO 12179 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-25 23:35:57.561  INFO 12179 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1103 ms
2019-03-25 23:35:57.765  INFO 12179 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-25 23:35:57.981  INFO 12179 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-25 23:35:57.985  INFO 12179 --- [           main] com.boot.first.BootApplication           : Started BootApplication in 2.07 seconds (JVM running for 3.258)

从启动日志中,我们可以清晰的看到,该项目是监听端口号为8080,上下文为‘’:

2019-03-25 23:35:57.981  INFO 12179 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

那么,假如8080已被占用了,我们需要让该项目监听其它端口,怎么解决呢?其实吧,我们只需要在resources文件夹下新增application.yml(当然也可以用.properties文件),然后在该文件中添加如下配置即可:

server:
  port: 8888 #将启动端口由 8080 修改为 8888
  servlet:
    context-path: /demo #将上下文路径修改为/demo

接下来,让我们访问下http://localhost:8888/demo/试下吧
在这里插入图片描述
全文代码

总结

以上,笔者简单的介绍了如何用Spring Boot构建一个Web应用。这里和以往使用SpringMVC构建Web应用最大的一个不同点就是启动方式的不同,Spring Boot因为内置了Web容器(默认为tomcat),所以我们再也不用自行安装Web容器了。

接下来,笔者将继续带着大家一起学习Spring Boot的其它内容。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值