读书笔记《SpringBoot编程思想》

[toc]java

1、 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

2、理解独立的spring应用

1.应用类型

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

2.@RestController

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

3.官网建立springboot应用

https://start.spring.io/web

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 <packaging>jar</packaging> 默认为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

3、理解固话的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>

4、理解嵌入式Web容器

1. tomcat容器

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

<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文件中的属性进行修改apache

  • 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>

5、理解自动装配

1. 激活自动化装配

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

2. 理解@SpringBootApplicaion注解

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

  • @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')websocket

6、理解Production-Ready

7、走向注解驱动编程

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>
  • @Import:限定@Autowired依赖注入的范围
  • @ComponentScan:springboot中的@ComponentScan至关于spring配置文件中的context:component-scan
@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中<bean>
  • @DependsOn:替换xml中<bean depends-on="...">
  • @Lazy:替换xml中<bean lazy-init="true | false">
  • @Primary:替换xml中<bean primary="true | false">
  • @Role:替换xml中<bean role="...">
  • @Lookup:替换xml中<bean lookup-method="...">

1.5 其余

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

2.spring注解编程模型

2.1 元注解

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

2.2 spring模式注解

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

2.3 spring组合注解

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

2.4 spring注解属性别名和覆盖

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

8、spring注解驱动设计模式

相关文章
相关标签/搜索