天天看点

SpringBoot學習笔记(一)创建与发布创建第一个springBoot工程打包发布SpringBoot的优点:eclipse插件可快速创建springBoot项目springBoot自动配置

文章目录

  • 创建第一个springBoot工程
    • 先创建一个Maven工程。再导入响应的依赖。
    • pom.xml
    • SbHelloApplication.java
    • HelloController.java
    • 访问:http://localhost:8080/hi/hello
  • 打包发布
  • SpringBoot的优点:
  • eclipse插件可快速创建springBoot项目
  • springBoot自动配置
    • @SpringBootApplication注解

创建第一个springBoot工程

先如下图创建第一个SpringBoot程序—HelloWord。

先创建一个Maven工程。再导入响应的依赖。

SpringBoot學習笔记(一)创建与发布创建第一个springBoot工程打包发布SpringBoot的优点:eclipse插件可快速创建springBoot项目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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<!-- 这是一个父项目,依赖管理 -->
	<!-- 在该父项目的父项目org.springframework.boot.spring-boot-dependencies定义了依赖的版本号 -->
	<!-- 它是springboot的版本仲裁中心,导入依赖默认则不需要写版本号。但没有被dependences收录的则需要进行版本声明-->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.hx</groupId>
	<artifactId>SB_Hello</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SB_Hello</name>
	<description>SB_Hello</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
           

spring-boot-start:场景启动器

spring-boot-start-web:帮我们导入了web模块正常运行所需要的依赖

springBoot将所有的功能场景进行抽取,做成一个starts(启动器),在开发时,需要什么功能就导入响应的启动器。

SbHelloApplication.java

@SpringBootApplication		
//核心注解:标注在类上,表示这是springBoot的启动程序,说明这是一个springBoot应用 
//springBoot应当运行该类的main方法来启动springBoot应用。
public class SbHelloApplication {

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

}
           

HelloController.java

package com.hx.hello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/hi")
@Controller
public class HelloController {
	
	@ResponseBody
	@RequestMapping("/hello")
	public String hello() {
		return "<h1>Hello World</h1>";
	}
}
           

访问:http://localhost:8080/hi/hello

显示Hello World表示成功!

打包发布

使用maven的install将springBoot程序打包成可执行的

xxx.jar

包。并使用

java -jar xxx.jar

运行即可。

SpringBoot的优点:

  1. 快速创建独立运行的Spring项目并集成主流框架
  2. 使用嵌入式Servlet容器,应用无需打成war包。无需开发人员手动配置Tomcat服务器。 简化部署。
  3. starters自动依赖与版本控制
  4. 大量的自动配置,简化开发,可以修改默认值
  5. 无需配置xml,无代码生成,开箱即用
  6. 准生产环境的运行时应用监控
  7. 与云计算集成

eclipse插件可快速创建springBoot项目

SpringBoot學習笔记(一)创建与发布创建第一个springBoot工程打包发布SpringBoot的优点:eclipse插件可快速创建springBoot项目springBoot自动配置

idea自带SpringBoot快速创建

SpringBoot學習笔记(一)创建与发布创建第一个springBoot工程打包发布SpringBoot的优点:eclipse插件可快速创建springBoot项目springBoot自动配置

springBoot自动配置

@SpringBootApplication注解

标注在某个类上,表示该类是一个springBoot配置程序

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration	//SpringBoot配置类。配置类也是容器中的一个组件
@EnableAutoConfiguration	//启用自动配置。SpringBoot帮我们自动配置。告诉springBoot开启自动配置功能。
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
	······
}
           
  • @SpringBootConfiguration

    SpringBoot配置类。

      @Configuration

      配置类也是容器中的一个组件

  • @EnableAutoConfiguration

    启用自动配置。SpringBoot帮我们自动配置。告诉springBoot开启自动配置功能。

  • @AutoConfigurationPackage

    自动配置包。将主配置类(@SpringBootApplication标注类)下的所在包下的所有子包中的所有组件扫描到spring容器中。

    SpringBoot學習笔记(一)创建与发布创建第一个springBoot工程打包发布SpringBoot的优点:eclipse插件可快速创建springBoot项目springBoot自动配置

  @Import(AutoConfigurationPackages.Registrar.class)

  spring底层注解,给容器中导入一个组件,导入的组件由

AutoConfigurationPackages.Registrar.class

指定

SpringBoot學習笔记(一)创建与发布创建第一个springBoot工程打包发布SpringBoot的优点:eclipse插件可快速创建springBoot项目springBoot自动配置
  • @Import(AutoConfigurationImportSelector.class)

    自动配置导入选择器。选择导入哪些组件,将所需导入的组件以全类名的方式返回。这些组件就会被添加到容器中。会给容器导入自动配置类(xxxAutoConfiguration),就会给容器导入该场景需要的所有组件,并配置好。免去了手动配置的注入组件的步骤。