04-001 springboot 入门

1、springboot简介

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

2、微服务

2014 Martin Fowler 提出微服务的设计思想与理念

微服务:一种架构风格,一个应用应该是一组小型服务,每个服务都有自己的进程,通过HTTP的方式进行互通;
功能元素拆分开,然后动态组合,节省调用资源,每一个功能元素都是可以独立替换、升级的软件单元;

单体应用:all in one,开发测试简单,部署也简单,扩展(scale)也简单

详细参照微服务文档.

部署和运维有很大调整。
每个功能单元都是独立的。

3、maven设置(p4)

4、HelloWorld

  1. 创建maven工程
  2. 导入springboot依赖
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
    </dependencies>
  1. 主程序
@SpringBootApplication
public class MainApplication {

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

}
  1. 写一个controller
@Controller
@ResponseBody
public class HelloController {

    @RequestMapping("/hello")
    public void hello(){
        System.out.println("hello");
    }

}
  1. 运行主程序测试

  2. 打包部署

 <build>
        <plugins>
<!--            这个插件可以将应用打包成一个可以执行的jar包-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

简化部署,maven》lifecycle》package直接打包,target目录下jar包可以通过 java -jar 命令直接运行,有内嵌的Tomcat

5 、探究

  1. pom文件
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
   </parent>

它的父项目是
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
真正管理springboot应用里面的依赖版本

springboot的版本仲裁中心:默认不用写版本;没有在dependencies里面管理的一定要声明

导入依赖

<dependency>
	 <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>

场景启动器,导入了web模块正常运行需要的相关的依赖

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

  1. 主程序类
@SpringBootApplication
public class MainApplication {

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

@SpringBootApplication:springboot应用标注在某个类上说明这个类是springboot的主配置类,springboot就应该运行这个类的main
底层原理深入可以反复学习。

组合注解
@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 {

标注在某个类上,表示这是一个springboot的配置类;
@Configuration:配置类上来标注这个注解
配置类—配置文件,配置类也是容器中的组件;

@EnableAutoConfiguration
开启自动配置功能;以前需要配置的,现在springboot帮我们配置,自动配置生效

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
**@AutoConfigurationPackage**
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {

@AutoConfigurationPackage:作用将主配置类的所在宝及下面所有子包里面的所有组件扫描到spring容器

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {

@Import是spring的底层注解,作用给容器中导入组件,
AutoConfigurationImportSelector:导入哪些组件的选择器
将所需组件以全类名的方式返回,这些组件就会被添加到容器中;
会给容器中导入非常多的自动配置类(xxxAutoConfiguration),就是给容器中导入这个场景需要的所有组件,并配置好这些组件;

有了自动配置类,免去手写配置注入功能组件的工作;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classloader);

springboot在启动的时候从类路径下的META-INF/spring.factories 中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前需要手动配置的东西,自动配置类帮我们配置;

J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-2.2.2.RELEASE.jar包
C:\Users\xuke1.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.2.2.RELEASE\spring-boot-autoconfigure-2.2.2.RELEASE.jar!\org\springframework\boot\autoconfigure

可以参考spring注解版

6、使用Spring Initializer 快速创建springboot项目

IDE都支持使用spring的创建向导,快速创建
选择需要模块,联网自动下载

eclipse:new Spring Starter Project

默认生成的springboot项目:
主程序已经生成好;
resources文件夹目录结构:

  • static:静态资源,css /html/images
  • templates: 保存所有模板页面;springboot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面;可以使用模板引擎(freemaker、thymeleaf);
  • application.properties:应用配置文件,可以修改一些默认配置。

学习整理于springboot.

发布了53 篇原创文章 · 获赞 0 · 访问量 382

猜你喜欢

转载自blog.csdn.net/weixin_40778497/article/details/103923616