标签:ike 核心 intel span 选项 path mic possible gap
Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手。
首先,认识一下这位大佬
2014 Martin Fowler https://martinfowler.com/tags/microservices.html
微服务:架构风格(服务微化)
一个应用是一组小型服务;可以通过HTTP的方式进行互通;
单体应用:All In One
微服务: 每一个功能元素都是一个可独立替换和独立升级的软件单元;
一个功能:
向服务器发送AreYouOk请求,服务器接收请求并处理,响应3Q,I‘m very ok!字符串;
/** * AreYouOkApplication * ·@SpringBootApplication 用来标注一个主程序类,说明这是一个Spring Boot应用 * @author 78698 */ @SpringBootApplication public class AreYouOkApplication { public static void main(String[] args) { /** * Spring Boot应用启动 */ SpringApplication.run(AreYouOkApplication.class, args); } }
/** * 描述:控制层类 * ·@Controller注解 标注此类为控制层类,同时让Spring Boot容器管理起来。 * ·@RequestMapping注解 是用来处理请求地址映射的注解,可用于类或方法上。 * 用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。 * @author 78698 */ @Controller public class AreYouOkController { @ResponseBody @RequestMapping("/areYouOk") public String areYouOk(){ return "3Q,I‘m very ok!"; } }
服务启动成功之后在浏览器中访问如下地址,服务响应返回:3Q,I‘m very ok!
http://localhost:8080/areYouOk
问:运行 Spring Boot 有哪几种方式?
将本应用打包成一个可执行的jar包,直接使用" java -jar "的命令运行
在pom.xml文件中:
<!-- 本插件可以将应用打包成一个可执行的jar包 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
问:Spring Boot 打成的 jar 和普通的 jar 有什么区别 ?
java -jar xxx.jar
命令来运行,这种 jar 不可以作为普通的 jar 被其他项目依赖,即使依赖了也无法使用其中的类。\BOOT-INF\classes
目录下才是我们的代码,因此无法被直接引用。如果非要引用,可以在 pom.xml 文件中增加配置,将 Spring Boot 项目打包成两个 jar ,一个可执行,一个可引用。问:spring-boot-starter-parent 有什么用 ?
答:我们都知道,新创建一个 Spring Boot 项目,默认都是有 parent 的,这个 parent 就是 spring-boot-starter-parent ,spring-boot-starter-parent 主要有如下作用:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> 的父项目是: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.2</version> </parent> 它来管理Spring Boot应用里面的所有依赖版本;
问:Spring Boot 中的 starter 到底是什么 ?
答:首先,这个 Starter 并非什么新的技术点,基本上还是基于 Spring 已有功能来实现的。首先它提供了一个自动化配置类,一般命名为 XXXAutoConfiguration
,在这个配置类中通过条件注解来决定一个配置是否生效(条件注解就是 Spring 中原本就有的),然后它还会提供一系列的默认配置,也允许开发者根据实际情况自定义相关配置,然后通过类型安全的属性注入将这些配置属性注入进来,新注入的属性会代替掉默认属性。正因为如此,很多第三方框架,我们只需要引入依赖就可以直接使用了。当然,开发者也可以自定义 Starter。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
spring-boot-starter-web:
spring-boot-starter:Spring Boot场景启动器;帮我们自动导入了web模块正常运行所依赖的组件;
spring boot将所有功能场景抽取出来,做成一个个starts启动器。只需要在项目中引入这些starts,相关场景的所有依赖都会导入进来。需要什么功能就引入其对应的starts。
比如:
Spring Boot集成Redis: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>4.2.
/** * AreYouOkApplication * ·@SpringBootApplication 用来标注一个主程序类,说明这是一个Spring Boot应用 * @author 78698 */ @SpringBootApplication public class AreYouOkApplication { public static void main(String[] args) { /** * Spring Boot应用启动 */ SpringApplication.run(AreYouOkApplication.class, args); } }
注解@SpringBootApplication 用来标注一个主程序类,说明这是一个Spring Boot应用;
Spring Boot通过运行这个类的main方法来启动Spring Boot应用。
问:Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?
package org.springframework.boot.autoconfigure; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.context.annotation.AnnotationBeanNameGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.core.annotation.AliasFor; import org.springframework.data.repository.Repository; /** * Indicates a {@link Configuration configuration} class that declares one or more * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience * annotation that is equivalent to declaring {@code @Configuration}, * {@code @EnableAutoConfiguration} and {@code @ComponentScan}. * * @author Phillip Webb * @author Stephane Nicoll * @author Andy Wilkinson * @since 1.2.0 */ @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 { /**略*/ }
标明该类使用Spring基于Java的注解,Spring Boot推荐使用基于Java而不是XML的配置。查看@SpringBootConfiguration源代码,可以看到它就是对@Configuration进行简单的“包装”,然后取名为SpringBootConfiguration。
我们对@Configuration注解并不陌生,它就是JavaConfig形式的SpringIoC容器的配置类使用的那个@Configuration。
其源代码如下:
package org.springframework.boot; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.AliasFor; /** * Indicates that a class provides Spring Boot application * {@link Configuration @Configuration}. Can be used as an alternative to the Spring‘s * standard {@code @Configuration} annotation so that configuration can be found * automatically (for example in tests). * <p> * Application should only ever include <em>one</em> {@code @SpringBootConfiguration} and * most idiomatic Spring Boot applications will inherit it from * {@code @SpringBootApplication}. * * @author Phillip Webb * @author Andy Wilkinson * @since 1.4.0 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { /** * Specify whether {@link Bean @Bean} methods should get proxied in order to enforce * bean lifecycle behavior, e.g. to return shared singleton bean instances even in * case of direct {@code @Bean} method calls in user code. This feature requires * method interception, implemented through a runtime-generated CGLIB subclass which * comes with limitations such as the configuration class and its methods not being * allowed to declare {@code final}. * <p> * The default is {@code true}, allowing for ‘inter-bean references‘ within the * configuration class as well as for external calls to this configuration‘s * {@code @Bean} methods, e.g. from another configuration class. If this is not needed * since each of this particular configuration‘s {@code @Bean} methods is * self-contained and designed as a plain factory method for container use, switch * this flag to {@code false} in order to avoid CGLIB subclass processing. * <p> * Turning off bean method interception effectively processes {@code @Bean} methods * individually like when declared on non-{@code @Configuration} classes, a.k.a. * "@Bean Lite Mode" (see {@link Bean @Bean‘s javadoc}). It is therefore behaviorally * equivalent to removing the {@code @Configuration} stereotype. * @return whether to proxy {@code @Bean} methods * @since 2.2 */ @AliasFor(annotation = Configuration.class) boolean proxyBeanMethods() default true; }
从@EnableAutoConfiguration源代码可以看出,其包含@Import注解。我们知道,@Import注解的主要作用就是借助EnableAutoConfigurationImportSelector将Spring Boot应用所有符合条件的@Configuration配置都加载到当前Spring Boot创建并使用的IoC容器中。
IoC容器就是我们所说的Spring应用程序上下文ApplicationContext。
学习过Spring框架就知道,Spring框架提供了很多@Enable开头的注解定义,比如@EnableScheduling、@EnableCaching等。这些@Enable开头的注解都有一个共同的功能,就是借助@Import的支持,收集和注册特定场景相关的bean定义。
Spring Boot在启动的时候从类路径的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就会生效,帮助我们进行自动配置工作。
其源代码如下:
package org.springframework.boot.autoconfigure; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.io.support.SpringFactoriesLoader; /** * Enable auto-configuration of the Spring Application Context, attempting to guess and * configure beans that you are likely to need. Auto-configuration classes are usually * applied based on your classpath and what beans you have defined. For example, if you * have {@code tomcat-embedded.jar} on your classpath you are likely to want a * {@link TomcatServletWebServerFactory} (unless you have defined your own * {@link ServletWebServerFactory} bean). * <p> * When using {@link SpringBootApplication @SpringBootApplication}, the auto-configuration * of the context is automatically enabled and adding this annotation has therefore no * additional effect. * <p> * Auto-configuration tries to be as intelligent as possible and will back-away as you * define more of your own configuration. You can always manually {@link #exclude()} any * configuration that you never want to apply (use {@link #excludeName()} if you don‘t * have access to them). You can also exclude them via the * {@code spring.autoconfigure.exclude} property. Auto-configuration is always applied * after user-defined beans have been registered. * <p> * The package of the class that is annotated with {@code @EnableAutoConfiguration}, * usually via {@code @SpringBootApplication}, has specific significance and is often used * as a ‘default‘. For example, it will be used when scanning for {@code @Entity} classes. * It is generally recommended that you place {@code @EnableAutoConfiguration} (if you‘re * not using {@code @SpringBootApplication}) in a root package so that all sub-packages * and classes can be searched. * <p> * Auto-configuration classes are regular Spring {@link Configuration @Configuration} * beans. They are located using the {@link SpringFactoriesLoader} mechanism (keyed * against this class). Generally auto-configuration beans are * {@link Conditional @Conditional} beans (most often using * {@link ConditionalOnClass @ConditionalOnClass} and * {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations). * * @author Phillip Webb * @author Stephane Nicoll * @since 1.0.0 * @see ConditionalOnBean * @see ConditionalOnMissingBean * @see ConditionalOnClass * @see AutoConfigureAfter * @see SpringBootApplication */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */ Class<?>[] exclude() default {}; /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */ String[] excludeName() default {}; }
开发的组件或bean定义能被自动发现并注入到Spring应用程序上下文。比如我们在控制层添加@Controller注解、服务层添加的@Service注解和@Component注解等,这些注解都可以被@ComponentScan注解扫描到。
配置文件的作用:修改Spring Boot自动配置的默认值。
Spring Boot使用一个全局配置文件,并且配置文件的名称是固定的。
问:你如何理解 Spring Boot 配置加载顺序?
答:在 Spring Boot 里面,可以使用以下几种方式来加载配置。
1)properties文件;
2)YAML文件;
问:什么是 YAML?YAML 配置的优势在哪里 ?
答:YAML 是一种人类可读的数据序列化语言。它通常用于配置文件。与属性文件相比,如果我们想要在配置文件中添加复杂的属性,YAML 文件就更加结构化,而且更少混淆。可以看出 YAML 具有分层配置数据。
问:Spring Boot 是否可以使用 XML 配置 ?
答:Spring Boot 推荐使用 Java 配置而非 XML 配置,但是 Spring Boot 中也可以使用 XML 配置,通过 @ImportResource 注解可以引入一个 XML 配置。
问:spring boot 核心配置文件是什么?bootstrap.properties 和 application.properties 有何区别 ?
答:单纯做 Spring Boot 开发,可能不太容易遇到 bootstrap.properties 配置文件,但是在结合 Spring Cloud 时,这个配置就会经常遇到了,特别是在需要加载一些远程配置文件的时侯。
spring boot 核心的两个配置文件:
持续更新中······
标签:ike 核心 intel span 选项 path mic possible gap
原文地址:https://www.cnblogs.com/AreYouOK857/p/14312965.html