SpringBoot入门学习 - HelloWorld实例/微服务入门/一些注解详解

目录

0 学习资源

1 SpringBoot简介

补充:war/jar/ear包

2 微服务介绍

3 HelloWorld实例

3.1 环境

3.2 Maven配置: setting.xml

3.3 IDEA设置

3.4 HelloWorld实例编写

4 HelloWorld细节

4.1 pom.xml

4.2 主程序类,主入口类

1 @SpringBootApplication详解

2  @SpringBootApplication注解是一个组合注解


0 学习资源

视频课: https://www.bilibili.com/video/av38657363?from=search&seid=15095513327806987593

21天实战SpringBoot2.9 https://www.bilibili.com/video/av25425463/?spm_id_from=333.788.videocard.1

Reference Guide: https://docs.spring.io/spring-boot/docs/2.1.4.RELEASE/reference/htmlsingle/#using-boot-starter

1 SpringBoot简介

简化Spring应用开发的一个框架;整个Spring技术栈的一个大整合;J2EE开发的一站式解决方案

优点

缺点:入门容易,精通难

补充:war/jar/ear包

jar包和war包的区别:

war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块。而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令运行的.

它们都是压缩的包,拿Tomcat来说,将war文件包放置它的\webapps\目录下,启动Tomcat,这个包可以自动进行解压,也就是你的web目录,相当于发布了。

war包:通常是做好一个web应用后,通常是网站,打成包部署到容器中。

jar包:通常是开发时要引用通用类,打成包便于存放管理。

ear包:企业级应用,通常是EJB打成ear包。

所有的包都是用jar打的,只不过目标文件的扩展名不一样。

WAR是Sun提出的一种Web应用程序格式,与JAR类似,也是许多文件的一个压缩包。这个包中的文件按一定目录结构来组织:通常其根目录下包含有Html和Jsp文件或者包含这两种文件的目录,另外还会有一个WEB-INF目录,这个目录很重要。通常在WEB-INF目录下有一个web.xml文件和一个classes目录,web.xml是这个应用的配置文件,而classes目录下则包含编译好的Servlet类和Jsp或Servlet所依赖的其它类(如JavaBean)。通常这些所依赖的类也可以打包成JAR放到WEB-INF下的lib目录下,当然也可以放到系统的CLASSPATH中,但那样移植和管理起来不方便.

2 微服务介绍

2014 Martin fowler: https://www.martinfowler.com/microservices/ Micro services introduction

微服务:

  • 是一种架构风格
  • 一个应用程序应该是一组小型服务;可以通过HTTP的方式进行互通
  • 每一个功能元素最终都是一个可独立替换和独立升级的软件单元。

单体应用:

微服务vs单体应用

每一个功能元素最终能够都是一个可以独立替换和独立升级的软件单元

 

SpringBoot+Spring Cloud+Spring Cloud Data Flowg关系

3 HelloWorld实例

3.1 环境

  • jdk 1.8+
  • Maven 3.3+
  • IntelliJ IDEA 2017
  • SpringBoot 1.5.9

3.2 Maven配置: setting.xml

<profile>
	<id>jdk-1.8</id>
	<activation>
		<activateByDefault>true</activateByDefault>
		<jdk>1.8</jdk>
	</activation>
	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
	</properties>
</profile>

3.3 IDEA设置

3.4 HelloWorld实例编写

  • Step1:创建一个maven工程(.jar)
  • Step2:导入springboot相关依赖
  • Step3:编写一个主程序,启动springboot应用
import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

*@SpringBootApplication:标注一个主程序类,说明这是一个springboot应用

**/

@SpringBootApplication

public class HelloworldMainApp{

    public static void main(String[]args){
        //spring应用启动起来
        SpringApplication.run(HelloworldMainApp.class,args);
    }

}
  • Step4: 编写相关的controller和service
importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController{

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return"helloworld!!!";
    }

}
  • Step 5: 测试:运行主程序测试
  • Step6:部署
    • Ideal左侧Maven=>lifecycle==> click "package" ==>即可生成jar包=>直接使用jave -jar实行,即使部署地点没有安装tomcat也没事,因为jar包中已经带了依赖

4 HelloWorld细节

4.1 pom.xml详解

  • 父项目 - spring-boot-starter-parent
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>    
    <version>2.1.4.RELEASE</version>
</parent>
  •  spring-boot-starter-parent的父项目是:spring-boot-dependencies
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

spring-boot-dependencies中<property>定义了依赖的版本号 

spring-boot-dependencies来真正管理springboot中的所有依赖,称为springboot的版本仲裁中心。

以后我们导入依赖,默认是不需要写版本的,没有在dependency中管理的依赖需要声明版本

  • Starters启动器
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring-boot-starter-web: Spring-boot-starter是springboot场景启动器,帮我们导入了web模块正常运行所依赖的组件

Springboot将所有的功能场景都抽取出来,做成一个个的starters启动器,只需要在项目中引入这些starter相关的场景的所有依赖都导入进来,要用什么功能就导入什么场景的启动器

4.2 主程序类,主入口类

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

/**
*@SpringBootApplication:标注一个主程序类,说明这是一个springboot应用
**/
@SpringBootApplication
public class HelloworldMainApp{
    public static void main(String[]args){
        //spring应用启动起来
        Spring Application.run(HelloworldMainApp.class,args);
    }
}

@SpringBootApplication: 标注在某个类上,说明这个类是springboot的主配置类,springboot就应该运行这个类的main方法来启动springboot应用。

1 @SpringBootApplication详解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters={
    @Filter(type=FilterType.CUSTOM,classes={TypeExcludeFilter.class}),
    @Filter(type=FilterType.CUSTOM,classes={AutoConfigurationExcludeFilter.class}))
public @interface SpringBootApplication{

……..

}

2  @SpringBootApplication注解是一个组合注解

  • @SpringBootConfiguration: springboot的配置类,标注在某个类上,表示这是一个springboot的配置类
  • @Configuration: 配置类上来标注这个注解 -- spring底层注解
    • 所谓配置类---配置文件,配置类也是容器中的一个组件
  • @EnableAutoConfiguration: 开启自动配置功能
    • 需要配置的东西,spring boot可以帮我们配置:告诉springboot开启自动配置功能,这样自动配置才能生效
    • @AutoConfigurationPackage: 自动配置包
      • @Import({Registrar.class}) Spring底层注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration{
......
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值