《读书笔记》SpringBoot编程思想

时间:2019-09-08 00:41:38   收藏:0   阅读:100

一、 springboot总览

1.springboot特性

2.准备运行环境

二、理解独立的spring应用

1.应用类型

2.@RestController

3.官网创建springboot应用

https://start.spring.io/

4.基础的start依赖

5.springboot打包

6.springboot的jar文件

三、理解固话的Maven依赖

1.spring-boot-starter-parent与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文件中的属性进行修改

<properties>
   <tomcat.version>8.5.6</tomcat.version></properties>
</properties>
<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三个注解的特性,可以理解为前者等同包含于三个后者:

其中@SpringBootConfiguration属于@Configuration的派生注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplicaion{
    ...
}

3. 派生注解@Component

4. @SpringBootApplicaion属性别名

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

六、理解Production-Ready

七、走向注解驱动编程

1. Spring核心注解场景分类

1.1 模式注解

在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 装配注解

@ComponentScan:扫描知道package下标注Spring模式注解的类,如果不添加此注解,则其他的添加注解的类
不会被springboot扫描到,更不会装入spring容器中。

1.3 依赖注入注解

:) @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定义注解

1.5 其他

2.spring注解编程模型

2.1 元注解

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

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

八、spring注解驱动设计模式

原文:https://www.cnblogs.com/songzhen/p/11484036.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!