《读书笔记》SpringBoot编程思想

一、 springboot总览

1.springboot特性

  • 独立的spring应用
    springboot可以以jar包的形式独立运行,使用 java -jar xxx.jar 就可以成功运行项目,
  • 内嵌servlet容器
    使我们在应用项目的主程序中运行main函数即可快速运行。
  • 内嵌web容器
    直接嵌入tomcat、jetty等web容器(不需要部署war文件)
  • 提供固话的starter依赖
    简化maven配置,使常见的依赖聚集在一起,形成单条依赖
  • 组件自动装配
    Spring Boot会根据我们项目中类路径的jar包/类,为jar包的类进行自动配置Bean,大大简化了配置
  • 应用监控
    springboot提供了基于HTTP、ssh、telnet对运行时的项目进行监控。
  • 不需要配置xml
    可以完全不使用xml配置,只需要自动配置和Java config

2.准备运行环境

  • JDK1.8
  • MAVEN

二、理解独立的spring应用

1.应用类型

  • 非web应用 : 主要用于服务提供、调度任务、消息处理
  • web应用 : 内嵌servlet或web容器,对外提供HTTP服务

2.@RestController

  • @RestController注解用作类的请求控制
  • @RequestMapping注解用作方法的请求映射
  • @ResponseBody注解用作方法的返回对象映射
  • 当有@RestController注解时,不需要添加@ResponseBody注解,可以认为@RestController= @Controller + @ResponseBody

3.官网创建springboot应用

https://start.spring.io/

4.基础的start依赖

  • spring-boot-starter-parent : srpingboot的父级依赖
    • 默认使用java8,也可手动添加指定版本
    <properties>
        <java.version>1.8</java.version>
    </properties>
    • 默认使用UTF-8编码,可手动添加配置修改
    <properties>
        <project.build.sourceEncoding>GBK</project.build.sourceEncoding>
    </properties>
    • 省略version信息,可不指定version
    • 识别插件配置
      比如 exec plugin, surefire
      能够识别 application.properties 和 application.yml 类型的文件

5.springboot打包

  • 构建jar文件前提,需要在spring-boot-maven-plugin到pom.xml中
  • Spring Boot Maven plugin能够将Spring Boot应用打包为可执行的jar或war文件当运行“mvn package”进行打包时,会打包成一个可以直接运行的 JAR 文件,使用“java -jar”命令就可以直接运行
  • 可以在POM中,指定生成 的是Jar还是War jar 默认为jar
  • spring-boot-maven-plugin的命令
    • spring-boot:repackage,默认goal。在mvn package之后,再次打包可执行的jar/war
    • spring-boot:run,运行Spring Boot应用,与java -jar xxx.jar命令无异

6.springboot的jar文件

  • springboot的fat jar文件除了包含传统的java jar中的资源外,还包含依赖的jar文件,他是一个独立归档的应用文件
  • jdk默认支持文件(file)、http、jar等协议,故jdk内建了对应协议的实现,这些实现类均放在sun.net.www.protocol包下,并且类名必须为Handler,
    • FILE:sun.net.www.protocol.file.Handler
    • JAR:sun.net.www.protocol.jar.Handler
    • HTTP:sun.net.www.protocol.http.Handler
    • HTTPS:sun.net.www.protocol.https.Handler
    • FTP:sun.net.www.protocol.ftp.Handler
  • 以上这些类均为java.net.URLStreamHandler的实现类,如果需要扩展springboot的启动jar文件,则需要把org.springframework.boot.loader.jar.Handler添加到java.protocol.handler.pkgs中,并覆盖原sun.net.www.protocol.jar.Handler

三、理解固话的Maven依赖

1.spring-boot-starter-parent与spring-boot-dependencies

  • spring-boot-starter-parent继承于spring-boot-dependencies,也就是说spring-boot-starter-parent的管理jar包的能力源于spring-boot-dependencies
  • 如果不想使用spring-boot-starter-parent来实现dependencyManagement(依赖管理),而是通过自己手动指定jar包的版本号,可通过以下配置spring-boot-dependencies来为每个jar包设置依赖
  • 如果通过spring-boot-dependencies来管理依赖,那么不能使用property的形式覆盖原始的依赖项,要达到同样的效果,需要在dependencyManagement里面的spring-boot-dependencies之前添加依赖的东西
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.3.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

四、理解嵌入式Web容器

1. tomcat容器

spring boot 的web应用开发必须使用spring-boot-starter-web,其默认嵌入的servlet容器是Tomcat。

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.4.3.RELEASE</version>
</parent>
 
<dependencies>
   <!-- TOMCAT -->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>

嵌入的servlet容器版本在pom的以下父依赖项中定义,比如上面的version1.4.3引入了Tomcat版本8.5.6。
如果想改变tomcat版本,也可以更改pom.xml或application.properties文件中的属性进行修改

  • application.properties 文件修改:
<properties>
   <tomcat.version>8.5.6</tomcat.version></properties>
</properties>
  • pom.xml文件修改:
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-core</artifactId>
   <version>${tomcat.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-el</artifactId>
   <version>${tomcat.version}</version>
</dependency>
<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-websocket</artifactId>
   <version>${tomcat.version}</version>
</dependency>

如果想使用其它servlet容器,则需要先移除tomcat容器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2. jetty作为嵌入式servlet容器

将默认的嵌入式容器tomcat切换至jetty

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

3. undertow作为嵌入式servlet容器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

五、理解自动装配

1. 激活自动化装配

官网提出激活自动化装配需要将注解@EnableAutoConfiguration 和 @SpringBootApplicaion,将两者选其一标注在@Configuration类上,@Configuration声明被标注为配置类

2. 理解@SpringBootApplicaion注解

@SpringBootApplicaion是一个聚合注解,类似的还有@RestController等
@SpringBootApplicaion被用于激活@EnableAutoConfiguration、@ComponentScan、@Configuration三个注解的特性,可以理解为前者等同包含于三个后者:

  • @EnableAutoConfiguration:负责激活SpringBoot自动装配机制
  • @ComponentScan:激活@Component的扫描
  • @Configuration:声明被标注为配置类
其中@SpringBootConfiguration属于@Configuration的派生注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplicaion{
    ...
}

3. 派生注解@Component

  • @Component
    • @Configuration
      • @SpringBootConfiguration
        @Repository、@Service、@Controller均属于@Component派生注解

4. @SpringBootApplicaion属性别名

@AlisaFor注解能够将一个或多个注解的属性‘别名’到某个注解中
@SpringBootApplicaion(scanBasePackages = 'com.song.xxx')

六、理解Production-Ready

七、走向注解驱动编程

1. Spring核心注解场景分类

1.1 模式注解

  • @Repository:数据仓库模式
  • @Compoent:通用组件模式
  • @Service:服务模式
  • @Controller:Web控制器模式
  • @Configuration:配置类模式
在applicationContext.xml文件中加一行:<context:component-scan base-package="com.song.xxx"/>后
@Component、@Repository、@Service、@Controller都是将类实例化注入到spring管理器中,名字只是一
个分类,实质作用是一样的

@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Service一般标注在业务接口实现类上,用于标注业务层组件
@Controller用于标注控制层组件

@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,
作用为:配置spring容器(应用上下文)
用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被
AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext
类进行扫描,并用于构建bean定义,初始化Spring容器

1.2 装配注解

  • @ImportResource:替换xml元素
  • @Import:限定@Autowired依赖注入的范围
  • @ComponentScan:springboot中的@ComponentScan相当于spring配置文件中的
@ComponentScan:扫描知道package下标注Spring模式注解的类,如果不添加此注解,则其他的添加注解的类
不会被springboot扫描到,更不会装入spring容器中。

1.3 依赖注入注解

  • @Autowired:Bean的依赖注入
  • @Qualifer:细粒度的@Autowired依赖查找
  • @Resource(Java注解):Bean依赖注入
:) @Autowired默认按类型装配
:) @Autowired默认情况下必须要求依赖对象必须存在,如果要允许null值,可以设置
   它的required属性为false,如:@Autowired(required=false)
:) 如果接口有多个实现类,spring并不知道用哪个实现类,这个时候可以结合@Qualifer注解,
   注意@Qualifier注解括号里面的必须是Person接口实现类的类名:@Qualifier("StudentService")

:) @Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配
:) @Resource指定了name或者type则根据指定的类型去匹配bean:
   @Resource(name = "teacher") / @Resource(type = Student.class)
:) @Resource属于java注解,@Autowired和@Qualifer属于spring注解,建议使用@Resource注解,
   以减少代码和Spring之间的耦合。

1.4 Bean定义注解

  • @Bean:替换xml中
  • @DependsOn:替换xml中
  • @Lazy:替换xml中
  • @Primary:替换xml中
  • @Role:替换xml中
  • @Lookup:替换xml中

1.5 其他

  • @AliasFor:别名注解属性,实现复用的目的
  • @Indexed:提示spring模式注解的扫描效率
  • @Profile:配置化条件装配
  • @Conditional:编程条件装配

2.spring注解编程模型

2.1 元注解

能申明在其他注解上的注解,例如:@Documented、@Component

2.2 spring模式注解

@Component作为一种由spring容器托管的通用模式组件,任何被@Component标注的组件均为组件扫描的候选对象,类似地,凡是被@Component元标注的注解,如@Service所标注的任何组件,也被视作组件的候选对象。

扫描二维码关注公众号,回复: 7225802 查看本文章

2.3 spring组合注解

例如:
@TransacrionlService组合了@Transacrion和@Service这两个注解
@SpringBootApplication既是模式注解,也是组合注解

2.4 spring注解属性别名和覆盖

较低层注解能覆盖其元注解的同名属性
@Component
|-@Service
|-@TransacrionlService
其中@TransacrionlService可以覆盖@Service
@Service可以覆盖@Component

八、spring注解驱动设计模式

猜你喜欢

转载自www.cnblogs.com/songzhen/p/11484036.html