《Spring Boot 编程思想》 Spring Boot概览

理解独立的Spring应用

Spring的Web服务包括两种:
ServletWeb服务和Reactive Web(通过Reactive Web容器实现,Spring5.0 WebFlux)
使用ServletWeb和Reactive Web不一样的地方:
1、设置setApplicationType(WebApplicationTyp)类型不同
2、starter不同

Spring boot利用spring-boot-maven-plugin可运行的fat jar,可以使用java -jar target/user-server-0.0.1-SNAPSHOT.jar(线上环境)或者mvn spring-boot:run(开发环境)找执行

打包的结果如下图:
在这里插入图片描述
上面一个jar打入了依赖包,下面一个只有本地资源。

理解固话的Maven依赖

1、spring-boot-dependencies是spring-boot-starter-parent的parent
2、不依赖spring-boot-starter-parent时在dependencyManagemenge中依赖spring-boot-dependencies想要使用spring-boot-maven-plugin打包需要单独配置他的版本号,repackage元素。
3、spring-boot-starter-web依赖引入了spring-boot-starter-tomcat 使用了Tomcat 容器

例如依赖spring-boot-starter-parent:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
</parent>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如果依赖spring-boot-dependencies:
dependencyManagemenge中依赖spring-boot-dependencies

 <dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
maven插件:
 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.5.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>
        </plugins>
    </build>

当你不需要将包打成fat jar时你需要把spring-boot-maven-plugin换成maven-compiler-plugin如:

<plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
   </plugin>

嵌入式web容器

切换web容器方式可参考Spring boot2.0官方文档75.1 User Another Web server
使用Reactive Web容器,不仅需要添加依赖,还需要激活这个容器,比如添加webflux依赖
在这里插入图片描述
WebServerInitializedEvent
在远行时怎么获取Http端口:
监听WebServerInitializedEvent事件

@EventListener(WebServerInitializedEvent.class)
	public void onWebServerReady(WebServerInitializedEvent webServerInitializedEvent){

	}

tomcat,jetty,undertow都支持ReactiveWeb的特性。

理解自动装配

@SpringBootApplication等同于使用下面三个注解:在这里插入图片描述
三个注解分别代表:
声明@Configuration配置类
激活自动装配
扫面@Component组件

@SpringBootApplication继承@ConfigurationCGLIB提升特性

自动装配机制:
Spring提供Bean生命周器管理和Spring编程模型。Spring Boot在Spring4.0的继承上添加了约定配置化导入@Configuration类的方式。使Spring能够装配@Configuration类

Spring Boot扫描所有jar的spring.factories文件进行自动装配。Spring5.0引入@Index对@Componet

条件化装配:

  @Bean
    @Conditional(MagicExesitCondition.class)
    public MagicBean magicBean(){
        return new MagicBean();
    }
**
 * 是否配置了magic边领 条件判断实现类
 *
 * @author ranran
 */
public class MagicExesitCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().containsProperty("magic");
    }
}

理解Production-Ready特性

Spring boot actuator:提供健康检查,审计,指标收集
启用actuator

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

默认暴露的只有health和info如果需要暴露其他的端点需要做外部化配置/或者启动参数:
properties文件:
management.endpoint.web.exposure.include=*
或者
启动参数使用-Dmanagement.endpoint.web.exposure.include=beans,evn,conditions
对应路径类似:/actuator/beans
启动参数示例:
在这里插入图片描述
配置management.endpoint.web.exposure.include=*后可在idea看见可用的actuator属性
在这里插入图片描述
外部化配置:(externalized configuration)
外部化配置主要使为了使代码在不同的环境运行,外部化主要实现方式为:properties文件,yaml文件,环境变量,命令行参数:
参数使用方式@value、Enviroment,@ConfigurationProperties(可以绑定到结构化对象,starter中经常使用)
17种外部化配置按照顺序,在前面的优先
规约大于配置
从Spring3.0开始@Configuration是xml的替代物,@Bean是元素的替代物。并且提供@Import来导入@Configuration

总结
Spring Boot的5大特性:
SpringApplication
自动装配
外部化配置
Spring Boot Actuator
嵌入式容器

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值