1、Spring boot 入门程序

Spring boot 入门程序

什么是Spring boot

Spring Boot 是由Pivotal团队提供的全新框架 。为了简化 Spring 应用的创建 、运行 、调试 、部署等一系列问题而诞生的产物 ,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置 ,我们只需遵循规范 ,引入相关的依赖就可以轻易的搭建出一个 WEB 工程 。

设计目的

  • 简化 Spring 应用的初始搭建以及开发过程
  • Spring Boot 整合了所有的框架
  • 提供一些常见的功能、如监控、WEB容器,健康,安全等功能

Spring Boot特性

  • 创建独立的Spring应用程序
  • 嵌入的Tomcat,无需部署WAR文件
  • 简化Maven配置
  • 自动配置Spring
  • 提供生产就绪型功能,如指标,健康检查和外部配置
  • 开箱即用,没有代码生成,也无需XML配置。

Spring Boot特性理解

  • 为基于Spring的开发提供更快的入门体验
    开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
  • 提供了一些大型项目中常见的非功能特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
  • Spring Boot并不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式。

开发环境

  • JDK1.8
  • Maven
  • IntelliJ IDEA

项目创建

IntelliJ IDEA 为例 :

方式一 :访问 https://start.spring.io/

填写完信息后下载压缩包 ,解压后导入 IntelliJ IDEA

方式二 : IntelliJ IDEA 创建

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

目录结构

在这里插入图片描述

pom 依赖

<?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>cn.ylx</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-helloworld</name>
    <description>springboot-helloworld</description>

    <!-- SpringBoot项目要继承的父类 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- SpringBoot项目要继承的父类 -->
    <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>
        <!-- 默认就内嵌了Tomcat 容器,如需要更换容器也极其简单-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 测试包,当我们使用 mvn package 的时候该包并不会被打入,因为它的生命周期只在 test 之内-->
        <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-starter :核心模块,包括自动配置支持、日志和YAML
  • spring-boot-starter-test :测试模块,包括junit、Hamcrest、Mokito

其他配置请看 : 官方文档

启动项分析

启动项分析

测试

写一个 HelloController :
HelloController

注意: @RestController 等价于 @Controller @ResponseBody

启动启动项:
在这里插入图片描述

浏览器输入:http://localhost:8080/hello
报错:
在这里插入图片描述

错误原因:
在这里插入图片描述

解决方法:
移动启动项至 Controller 上级目录。

启动成功控制台打印内容


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

2018-12-01 02:39:53.341  INFO 5404 --- [           main] cn.ylx.SpringbootHelloworldApplication   : Starting SpringbootHelloworldApplication on DESKTOP-10FVA01 with PID 5404 (C:\ylx\Git\springboot\springboot-helloworld\target\classes started by ys951 in C:\ylx\Git\springboot)
2018-12-01 02:39:53.344  INFO 5404 --- [           main] cn.ylx.SpringbootHelloworldApplication   : No active profile set, falling back to default profiles: default
2018-12-01 02:39:54.249  INFO 5404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-12-01 02:39:54.265  INFO 5404 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-12-01 02:39:54.265  INFO 5404 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.12
2018-12-01 02:39:54.271  INFO 5404 --- [           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: [C:\Java\jdk1.8.0_181\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;.;C:\Java\jdk1.8.0_181\bin;C:\Java\jdk1.8.0_181\jre\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin;C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin;C:\ylx\apache-maven-3.3.9\bin;G:\ys\hadoop-2.6.0-cdh5.7.6\bin;C:\Scala\scala\bin;D:\JAVAEE\VisualSVN Server\bin;D:\JAVAEE\TortoiseSVN\bin;C:\Users\ys951\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\SSH Communications Security\SSH Secure Shell;C:\Users\ys951\AppData\Local\GitHubDesktop\bin;.]
2018-12-01 02:39:54.404  INFO 5404 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-12-01 02:39:54.405  INFO 5404 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1018 ms
2018-12-01 02:39:54.428  INFO 5404 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-12-01 02:39:54.435  INFO 5404 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-12-01 02:39:54.435  INFO 5404 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-12-01 02:39:54.436  INFO 5404 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'formContentFilter' to: [/*]
2018-12-01 02:39:54.436  INFO 5404 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-12-01 02:39:54.598  INFO 5404 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-01 02:39:54.783  INFO 5404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-12-01 02:39:54.787  INFO 5404 --- [           main] cn.ylx.SpringbootHelloworldApplication   : Started SpringbootHelloworldApplication in 1.768 seconds (JVM running for 2.642)

github源码

猜你喜欢

转载自blog.csdn.net/weixin_42112635/article/details/84670370
今日推荐