程序员开发实例大全宝库

网站首页 > 编程文章 正文

Spring Boot介绍及快速入门案例(spring boot.)

zazugpt 2024-10-24 19:32:07 编程文章 23 ℃ 0 评论

概念

Spring Boot可以很容易地创建独立的、基于Spring的生产级别的应用程序,您只需“运行”即可。使用Spring Boot创建Java应用程序,可以通过使用Java -jar或传统的war部署进行启动。我们还提供了一个运行“spring脚本”的命令行工具。

对Spring平台和第三方库使用了特定的方式来进行了默认配置,你可以毫不费力地使用。大多数Spring引导应用程序仅需要最少的Spring配置。

特点

  • 创建独立的Spring应用程序
  • 嵌入Tomcat、Jetty或Undertow 的Servlet容器(不需要部署WAR文件)
  • 提供自动化配置的“starter”依赖项来简化构建配置
  • 尽可能地自动配置Spring和第三方库
  • 提供可用于生产环境的特性,如指标、运行状况检查和外部化配置
  • 完全不需要生成代码,也不需要XML配置

入门示例

示例使用spring boot 快速创建一个Web应用环境并启动运行。

基本环境:

java:jdk1.8

maven:3.6.3

eclipse:Oxygen.3a Release (4.7.3a)

  • 创建工程

通过eclipse创建一个基本工程,如下所示:

然后点击Finish完成创建。

然后创建包:com.spring.springboot.demo

  • 引入springboot的maven构建配置内容

在pom.xml文件加入springboot引用父starter,并配置相应的版本号,内容如下:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.4 </version>
  </parent>

然后引入对spring web starter的引用:

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

完成的pom.xml文件如下:

<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.spring.demo</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot</name>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.4 </version>
  </parent>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>
</project>
  • 创建示例类及启动类

创建一个示例Controller类,IndexController.java,包路径:com.spring.springboot.demo.controller,文件内容如下:

package com.spring.springboot.demo.controller;

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

@RestController
public class IndexController {

	
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("/")
	public String index() {
		return "hello SpringBoot";
	}
}

然后创建启动类型Application.java,包路径:com.spring.springboot.demo,文件内容如下:

package com.spring.springboot.demo;

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

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new SpringApplication(Application.class).run(args);
	}

}
  • 启动示例及查看效果

右键Application.java,然后运行。

启动日志:


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

2021-04-08 17:57:13.779  INFO 14656 --- [           main] com.spring.springboot.demo.Application   : Starting Application using Java 1.8.0_151 on DESKTOP-N02C8QG with PID 14656 (D:\project\oauth\client\springboot\target\classes started by Administrator in D:\project\oauth\client\springboot)
2021-04-08 17:57:13.784  INFO 14656 --- [           main] com.spring.springboot.demo.Application   : No active profile set, falling back to default profiles: default
2021-04-08 17:57:18.206  INFO 14656 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-04-08 17:57:18.239  INFO 14656 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-04-08 17:57:18.239  INFO 14656 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-04-08 17:57:18.615  INFO 14656 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-04-08 17:57:18.616  INFO 14656 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4690 ms
2021-04-08 17:57:19.508  INFO 14656 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-08 17:57:20.114  INFO 14656 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-04-08 17:57:20.161  INFO 14656 --- [           main] com.spring.springboot.demo.Application   : Started Application in 7.409 seconds (JVM running for 8.61)

然后访问http://localhost:8080

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表