spring-boot-reference

如果不想阅读英文原文,你可以直接读本文的【TS】标注参考翻译内容。由于本人水平有限,如有理解错误,烦请指正,互相交流。
Lire les documents officiels et apprendre les techniques originales.
【TS】读官方文档,学原味技术。


读官方文档,学原味技术!

3.1.5 Starters (启动器)

  • 简介

Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.
【TS】启动器是一组方便的依赖关系描述符,您可以将其包含在应用程序中。您可以获得所需的所有Spring和相关技术的一站式服务,而不必遍历样例代码和依赖描述符的复制-粘贴负载。例如,如果您希望开始使用Spring和JPA进行数据库访问,请在项目中包含Spring -boot-starter-data- JPA依赖项。

The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.
【TS】启动程序包含许多依赖项,您需要这些依赖项来快速启动和运行项目,并使用一组一致的、受支持的托管传递依赖项。

  • 命名

What’s in a name
【TS】名字有什么关系
All official starters follow a similar naming pattern; spring-boot-starter-, where * is a particular type of application. This naming structure is intended to help when you need to find a starter. The Maven integration in many IDEs lets you search dependencies by name. For example, with the appropriate Eclipse or STS plugin installed, you can press ctrl-space in the POM editor and type “spring-boot-starter” for a complete list.
【TS】所有正式启动者都遵循类似的命名模式;spring-boot-starter-
,其中*是特定类型的应用程序。这种命名结构是为了在您需要查找初学者时提供帮助。许多ide中的Maven集成允许您根据名称搜索依赖项。例如,安装了适当的Eclipse或STS插件后,您可以在POM编辑器中按ctrl-space并输入“spring-boot-starter”以获得完整的列表。

As explained in the “Creating Your Own Starter” section, third party starters should not start with spring-boot, as it is reserved for official Spring Boot artifacts. Rather, a third-party starter typically starts with the name of the project. For example, a third-party starter project called thirdpartyproject would typically be named thirdpartyproject-spring-boot-starter.
【TS】正如“创建您自己的启动程序”一节中所解释的,第三方启动程序不应该使用Spring - Boot启动,因为它是为官方的Spring启动工件保留的。相反,第三方启动者通常以项目的名称开始。例如,第三方启动项目thirdpartyproject通常被命名为thirdpartyproject-spring-boot-starter。

The following application starters are provided by Spring Boot under the org.springframework.boot group:
【TS】以下应用程序启动器是由org.springframework下的Spring Boot提供的。引导组:

Table 1. Spring Boot application starters(应用程序启动器)

  • spring-boot-starter
    - Core starter, including auto-configuration support, logging and YAML
    - 核心启动器,包括自动配置支持,日志和YAML
  • spring-boot-starter-aop
    - Starter for aspect-oriented programming with Spring AOP and AspectJ
    - 使用Spring AOP和AspectJ进行面向方面编程的启动器
  • spring-boot-starter-data-jdbc
    - Starter for using Spring Data JDBC
    - 用于使用Spring Data JDBC的启动器
  • spring-boot-starter-data-jpa
    - Starter for using Spring Data JPA with Hibernate
    - 用于在Hibernate中使用Spring Data JPA的启动器
  • spring-boot-starter-web
    - Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container
    - 用于使用Spring MVC构建web(包括RESTful)应用程序的启动器。使用Tomcat作为默认的嵌入式容器
  • … 略

In addition to the application starters, the following starters can be used to add production ready features:
【TS】除了应用程序启动器之外,还可以使用以下启动器添加产品就绪特性:

Table 2. Spring Boot production starters(产品特性启动器)

  • spring-boot-starter-actuator
    - Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application
    - 启动器用于使用Spring Boot的执行器,它提供了产品就绪功能,帮助您监视和管理应用程序

Finally, Spring Boot also includes the following starters that can be used if you want to exclude or swap specific technical facets:
【TS】最后,Spring Boot还包括以下启动程序,如果您想排除或交换特定的技术方面,可以使用这些启动程序:

Table 3. Spring Boot technical starters(特定技术启动器)

  • spring-boot-starter-jetty
    - Starter for using Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcat
    - 使用Jetty作为嵌入式servlet容器的启动器。spring-boot-starter-tomcat的替代方案
  • spring-boot-starter-log4j2
    - Starter for using Log4j2 for logging. An alternative to spring-boot-starter-logging
    - 使用Log4j2进行日志记录的启动程序。spring-boot-starter-logging的替代方法
  • … 略

For a list of additional community contributed starters, see the README file in the spring-boot-starters module on GitHub.
【TS】有关其他社区贡献的启动器列表,请参阅GitHub上spring-boot-starters模块中的README文件。

4.28. Creating Your Own Auto-configuration(创建自己的自动配置)

If you work in a company that develops shared libraries, or if you work on an open-source or commercial library, you might want to develop your own auto-configuration. Auto-configuration classes can be bundled in external jars and still be picked-up by Spring Boot.
【TS】如果您在开发共享库的公司工作,或者在开源或商业库工作,您可能希望开发自己的自动配置。自动配置类可以绑定到外部jar中,并且仍然可以通过Spring Boot获取。

Auto-configuration can be associated to a “starter” that provides the auto-configuration code as well as the typical libraries that you would use with it. We first cover what you need to know to build your own auto-configuration and then we move on to the typical steps required to create a custom starter.
【TS】自动配置可以关联到提供自动配置代码以及与之一起使用的典型库的“starter”。我们首先介绍构建自己的自动配置所需要知道的内容,然后继续介绍创建自定义启动程序所需的典型步骤。

A demo project is available to showcase how you can create a starter step-by-step.
【TS】演示项目可用来展示如何一步一步地创建启动程序。

4.28.1. Understanding Auto-configured Beans(理解自动配置的bean)

Under the hood, auto-configuration is implemented with standard @Configuration classes. Additional @Conditional annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use @ConditionalOnClass and @ConditionalOnMissingBean annotations. This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own @Configuration.
在幕后,使用标准@Configuration类实现自动配置。附加的@Conditional注解用于约束应该应用自动配置的时间。通常,自动配置类使用@ConditionalOnClass和@ConditionalOnMissingBean注解。这确保了自动配置仅在找到相关类并且没有声明自己的@Configuration时才适用。

You can browse the source code of spring-boot-autoconfigure to see the @Configuration classes that Spring provides (see the META-INF/spring.factories file).
【TS】您可以浏览spring-boot-autoconfigure的源代码,以查看Spring提供的@Configuration类(参见META-INF/spring.factories)。

4.28.2. Locating Auto-configuration Candidates(自动配置候选位置)

Spring Boot checks for the presence of a META-INF/spring.factories file within your published jar. The file should list your configuration classes under the EnableAutoConfiguration key, as shown in the following example:
【TS】Spring引导检查是否存在 META-INF/spring.factories 文件在您发布的jar中。文件应该在EnableAutoConfiguration键下列出您的配置类,如下面的示例所示:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.mycorp.libx.autoconfigure.LibXAutoConfiguration,\
com.mycorp.libx.autoconfigure.LibXWebAutoConfiguration

Auto-configurations must be loaded that way only. Make sure that they are defined in a specific package space and that they are never the target of component scanning. Furthermore, auto-configuration classes should not enable component scanning to find additional components. Specific @Imports should be used instead.
【TS】自动配置只能以这种方式加载。确保它们是在特定的包空间中定义的,并且它们从来不是组件扫描的目标。此外,自动配置类不应启用组件扫描来查找其他组件。应该使用特定的@import。

You can use the @AutoConfigureAfter or @AutoConfigureBefore annotations if your configuration needs to be applied in a specific order. For example, if you provide web-specific configuration, your class may need to be applied after WebMvcAutoConfiguration.
【TS】如果需要按特定顺序应用配置,可以使用@AutoConfigureAfter或@AutoConfigureBefore注解。例如,如果您提供特定于web的配置,您的类可能需要在WebMvcAutoConfiguration之后应用。

If you want to order certain auto-configurations that should not have any direct knowledge of each other, you can also use @AutoConfigureOrder. That annotation has the same semantic as the regular @Order annotation but provides a dedicated order for auto-configuration classes.
【TS】如果您想订购某些自动配置,而这些自动配置之间不应该有任何直接的了解,您还可以使用@AutoConfigureOrder。该注解具有与常规@Order注解相同的语义,但是为自动配置类提供了专用的顺序。

4.28.3. Condition Annotations(条件注解)

You almost always want to include one or more @Conditional annotations on your auto-configuration class. The @ConditionalOnMissingBean annotation is one common example that is used to allow developers to override auto-configuration if they are not happy with your defaults.
【TS】您几乎总是希望在自动配置类中包含一个或多个@Conditional注解。@ConditionalOnMissingBean注解是一个常见的示例,如果开发人员对您的默认值不满意,它可以允许他们重写自动配置。

Spring Boot includes a number of @Conditional annotations that you can reuse in your own code by annotating @Configuration classes or individual @Bean methods. These annotations include:
【TS】Spring Boot包含许多@Conditional注解,您可以通过注解@Configuration类或单个@Bean方法在自己的代码中重用这些注解。这些注解包括:

  • Class Conditions
  • Bean Conditions
  • Property Conditions
  • Resource Conditions
  • Web Application Conditions
  • SpEL Expression Conditions

Class Conditions(特定类型条件)

The @ConditionalOnClass and @ConditionalOnMissingClass annotations let @Configuration classes be included based on the presence or absence of specific classes. Due to the fact that annotation metadata is parsed by using ASM, you can use the value attribute to refer to the real class, even though that class might not actually appear on the running application classpath. You can also use the name attribute if you prefer to specify the class name by using a String value.
【TS】@ConditionalOnClass和@ConditionalOnMissingClass注解允许根据特定类的存在与否包含@Configuration类。由于使用ASM解析注解元数据的事实,您可以使用value属性引用实际的类,即使该类实际上可能不会出现在运行的应用程序类路径上。如果希望通过使用字符串值来指定类名,还可以使用name属性。

This mechanism does not apply the same way to @Bean methods where typically the return type is the target of the condition: before the condition on the method applies, the JVM will have loaded the class and potentially processed method references which will fail if the class is not present.
【TS】这种机制不会以同样的方式应用于@Bean方法,在这些方法中,返回类型通常是条件的目标:在应用方法上的条件之前,jvm将加载类和可能处理的方法引用,如果类不存在,这些引用将失败。

To handle this scenario, a separate @Configuration class can be used to isolate the condition, as shown in the following example:
【TS】为了处理这个场景,可以使用一个单独的@Configuration类来隔离条件,如下面的示例所示:

@Configuration(proxyBeanMethods = false)
// Some conditions
public class MyAutoConfiguration {// Auto-configured beans@Configuration(proxyBeanMethods = false)@ConditionalOnClass(EmbeddedAcmeService.class)static class EmbeddedConfiguration {@Bean@ConditionalOnMissingBeanpublic EmbeddedAcmeService embeddedAcmeService() { ... }}}

If you use @ConditionalOnClass or @ConditionalOnMissingClass as a part of a meta-annotation to compose your own composed annotations, you must use name as referring to the class in such a case is not handled.
【TS】如果您使用@ConditionalOnClass或@ConditionalOnMissingClass作为元注解的一部分来组成您自己的组合注解,那么在这种情况下,您必须使用name作为对类的引用。

Bean Conditions(特定对象条件)

The @ConditionalOnBean and @ConditionalOnMissingBean annotations let a bean be included based on the presence or absence of specific beans. You can use the value attribute to specify beans by type or name to specify beans by name. The search attribute lets you limit the ApplicationContext hierarchy that should be considered when searching for beans.
【TS】@ConditionalOnBean和@ConditionalOnMissingBean注解允许根据特定bean的存在或不存在来包含一个bean。可以使用value属性按类型指定bean,也可以使用name指定bean。search属性允许您限制在搜索bean时应该考虑的ApplicationContext层次结构。

When placed on a @Bean method, the target type defaults to the return type of the method, as shown in the following example:
【TS】当放置在@Bean方法上时,目标类型默认为该方法的返回类型,如下面的示例所示:

@Configuration(proxyBeanMethods = false)
public class MyAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic MyService myService() { ... }}

In the preceding example, the myService bean is going to be created if no bean of type MyService is already contained in the ApplicationContext.
【TS】在前面的示例中,如果ApplicationContext中还没有包含myService类型的bean,则将创建myService的bean。

You need to be very careful about the order in which bean definitions are added, as these conditions are evaluated based on what has been processed so far. For this reason, we recommend using only @ConditionalOnBean and @ConditionalOnMissingBean annotations on auto-configuration classes (since these are guaranteed to load after any user-defined bean definitions have been added).
【TS】您需要非常小心添加bean定义的顺序,因为这些条件是根据到目前为止(version2.2.2)所处理的内容进行评估的。因此,我们建议只在自动配置类上使用@ConditionalOnBean和@ConditionalOnMissingBean注解(因为这些注解保证在添加任何用户定义的bean定义之后加载)。

@ConditionalOnBean and @ConditionalOnMissingBean do not prevent @Configuration classes from being created. The only difference between using these conditions at the class level and marking each contained @Bean method with the annotation is that the former prevents registration of the @Configuration class as a bean if the condition does not match.
【TS】@ConditionalOnBean和@ConditionalOnMissingBean不会阻止创建@Configuration类。在类级别上使用这些条件与用注解标记每个包含的@Bean方法之间的唯一区别是,前者防止在条件不匹配时将@Configuration类注册为bean。

Property Conditions(特定属性条件)

The @ConditionalOnProperty annotation lets configuration be included based on a Spring Environment property. Use the prefix and name attributes to specify the property that should be checked. By default, any property that exists and is not equal to false is matched. You can also create more advanced checks by using the havingValue and matchIfMissing attributes.
【TS】@ConditionalOnProperty注释允许根据Spring环境属性包含配置。使用前缀和名称属性来指定应该检查的属性。默认情况下,匹配任何存在且不等于false的属性。您还可以使用havingValue和matchIfMissing属性创建更高级的检查。

Resource Conditions(特定资源条件)

The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present. Resources can be specified by using the usual Spring conventions, as shown in the following example: file:/home/user/test.dat.
【TS】@ConditionalOnResource注释只允许在出现特定资源时包含配置。可以使用通常的Spring约定来指定资源,如下面的示例所示:file:/home/user/test.dat。

Web Application Conditions(Web应用程序条件)

The @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations let configuration be included depending on whether the application is a “web application”. A servlet-based web application is any application that uses a Spring WebApplicationContext, defines a session scope, or has a ConfigurableWebEnvironment. A reactive web application is any application that uses a ReactiveWebApplicationContext, or has a ConfigurableReactiveWebEnvironment.
【TS】@ConditionalOnWebApplication和@ConditionalOnNotWebApplication注释根据应用程序是否是“web应用程序”来包含配置。基于servlet的web应用程序是任何使用Spring WebApplicationContext、定义会话范围或具有ConfigurableWebEnvironment。一个响应式web应用程序是使用ReactiveWebApplicationContext或具有ConfigurableReactiveWebEnvironment的任何应用程序。

SpEL Expression Conditions(特定SpEL表达式条件)

The @ConditionalOnExpression annotation lets configuration be included based on the result of a SpEL expression.
【TS】@ConditionalOnExpression注释允许根据SpEL表达式的结果包含配置。

4.28.4. Testing your Auto-configuration (测试自动配置)

An auto-configuration can be affected by many factors: user configuration (@Bean definition and Environment customization), condition evaluation (presence of a particular library), and others. Concretely, each test should create a well defined ApplicationContext that represents a combination of those customizations. ApplicationContextRunner provides a great way to achieve that.
【TS】自动配置可以受到许多因素的影响:用户配置(@bean定义和环境定制)、条件评估(特定库的存在)等等。具体来说,每个测试都应该创建一个定义良好的applicationcontext,它表示这些定制的组合。ApplicationContextRunner提供了实现这一点的好方法。

ApplicationContextRunner is usually defined as a field of the test class to gather the base, common configuration. The following example makes sure that UserServiceAutoConfiguration is always invoked:
ApplicationContextRunner通常被定义为测试类的一个字段,用于收集基本的公共配置。下面的示例确保总是调用UserServiceAutoConfiguration :

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(UserServiceAutoConfiguration.class));

If multiple auto-configurations have to be defined, there is no need to order their declarations as they are invoked in the exact same order as when running the application.
【TS】如果必须定义多个自动配置,则不需要按照与运行应用程序时完全相同的顺序调用它们的声明。

Each test can use the runner to represent a particular use case. For instance, the sample below invokes a user configuration (UserConfiguration) and checks that the auto-configuration backs off properly. Invoking run provides a callback context that can be used with Assert4J.
【TS】每个测试都可以使用runner来表示特定的用例。例如,下面的示例将调用用户配置(UserConfiguration)并检查自动配置是否正确备份。调用run提供了一个可以与Assert4J一起使用的回调上下文。

@Test
void defaultServiceBacksOff() {this.contextRunner.withUserConfiguration(UserConfiguration.class).run((context) -> {assertThat(context).hasSingleBean(UserService.class);assertThat(context).getBean("myUserService").isSameAs(context.getBean(UserService.class));});
}@Configuration(proxyBeanMethods = false)
static class UserConfiguration {@BeanUserService myUserService() {return new UserService("mine");}}

It is also possible to easily customize the Environment, as shown in the following example:
也可以很容易地定制环境,如下面的示例所示:

@Test
void serviceNameCanBeConfigured() {this.contextRunner.withPropertyValues("user.name=test123").run((context) -> {assertThat(context).hasSingleBean(UserService.class);assertThat(context.getBean(UserService.class).getName()).isEqualTo("test123");});
}

The runner can also be used to display the ConditionEvaluationReport. The report can be printed at INFO or DEBUG level. The following example shows how to use the ConditionEvaluationReportLoggingListener to print the report in auto-configuration tests.
【TS】runner还可以用来显示条件评估报告。报告可以在INFO或DEBUG级别打印。下面的示例展示了如何在自动配置测试中使用ConditionEvaluationReportLoggingListener打印报告。

@Test
public void autoConfigTest {ConditionEvaluationReportLoggingListener initializer = new ConditionEvaluationReportLoggingListener(LogLevel.INFO);ApplicationContextRunner contextRunner = new ApplicationContextRunner().withInitializer(initializer).run((context) -> {// Do something...});
}

Simulating a Web Context(模拟Web上下文)

If you need to test an auto-configuration that only operates in a Servlet or Reactive web application context, use the WebApplicationContextRunner or ReactiveWebApplicationContextRunner respectively.
【TS】如果您需要测试仅在Servlet或Reactive web(响应式)应用程序上下文中运行的自动配置,请分别使用WebApplicationContextRunner或ReactiveWebApplicationContextRunner 。

Overriding the Classpath(覆盖类路径)

It is also possible to test what happens when a particular class and/or package is not present at runtime. Spring Boot ships with a FilteredClassLoader that can easily be used by the runner. In the following example, we assert that if UserService is not present, the auto-configuration is properly disabled:
【TS】当特定的类和/或包在运行时不存在时,也可以测试发生了什么。Spring Boot附带一个FilteredClassLoader ,它可以很容易地被runner使用。在下面的例子中,我们断言如果UserService不存在,自动配置将被适当地禁用:

@Test
void serviceIsIgnoredIfLibraryIsNotPresent() {this.contextRunner.withClassLoader(new FilteredClassLoader(UserService.class)).run((context) -> assertThat(context).doesNotHaveBean("userService"));
}

4.28.5. Creating Your Own Starter(自定义Starter)

  • 简介

A full Spring Boot starter for a library may contain the following components:
【TS】一个完整的Spring启动程序库可能包含以下组件:
The autoconfigure module that contains the auto-configuration code.
【TS】包含自动配置代码的autoconfigure模块。
The starter module that provides a dependency to the autoconfigure module as well as the library and any additional dependencies that are typically useful. In a nutshell, adding the starter should provide everything needed to start using that library.
【TS】为autoconfigure模块提供依赖项的starter模块,以及通常有用的库和任何附加依赖项。简单地说,添加starter应该可以提供开始使用该库所需的一切。
You may combine the auto-configuration code and the dependency management in a single module if you do not need to separate those two concerns.
【TS】如果不需要将自动配置代码和依赖项管理分离开来,则可以将它们合并到一个模块中。

  • 命名

You should make sure to provide a proper namespace for your starter. Do not start your module names with spring-boot, even if you use a different Maven groupId. We may offer official support for the thing you auto-configure in the future.
【TS】您应该确保为启动程序提供适当的名称空间。即使使用不同的Maven groupId,也不要用spring-boot启动模块名。我们可能会提供官方支持的东西,你自动配置在未来。
As a rule of thumb, you should name a combined module after the starter. For example, assume that you are creating a starter for “acme” and that you name the auto-configure module acme-spring-boot-autoconfigure and the starter acme-spring-boot-starter. If you only have one module that combines the two, name it acme-spring-boot-starter.
【TS】根据经验,您应该以starter命名组合模块。例如,假设您正在为“acme”创建一个启动程序,并将自动配置模块命名为acme-spring-boot-autoconfigure,将启动程序命名为acme-spring-boot-starter。如果只有一个模块组合了这两个模块,那么将它命名为acme-spring-boot-starter。

  • 配置键

If your starter provides configuration keys, use a unique namespace for them. In particular, do not include your keys in the namespaces that Spring Boot uses (such as server, management, spring, and so on). If you use the same namespace, we may modify these namespaces in the future in ways that break your modules. As a rule of thumb, prefix all your keys with a namespace that you own (e.g. acme).
【TS】如果启动器提供了配置键,请为它们使用惟一的名称空间。特别是,不要在Spring Boot使用的名称空间(如服务器、管理、Spring等)中包含键。如果您使用相同的名称空间,我们将来可能会以破坏模块的方式修改这些名称空间。根据经验,在所有键前面加上您自己的名称空间(例如acme)。
Make sure that configuration keys are documented by adding field javadoc for each property, as shown in the following example:
【TS】确保通过为每个属性添加字段javadoc来记录配置键,如下面的示例所示:

@ConfigurationProperties("acme")
public class AcmeProperties {/*** Whether to check the location of acme resources.*/private boolean checkLocation = true;/*** Timeout for establishing a connection to the acme server.*/private Duration loginTimeout = Duration.ofSeconds(3);// getters & setters}

You should only use simple text with @ConfigurationProperties field Javadoc, since they are not processed before being added to the JSON.
【TS】您应该只使用带有@ConfigurationProperties字段Javadoc的简单文本,因为在将它们添加到JSON之前不会对它们进行处理。

Here are some rules we follow internally to make sure descriptions are consistent:
【TS】下面是一些我们内部遵循的规则,以确保描述是一致的:

  • Do not start the description by “The” or “A”.
    【TS】不要以“the”或“A”开头。
  • For boolean types, start the description with “Whether” or “Enable”.
    【TS】对于布尔类型,以“Whether”或“Enable”开始描述。
  • For collection-based types, start the description with “Comma-separated list”
    【TS】对于基于集合的类型,以“逗号分隔的列表”开始描述
  • Use java.time.Duration rather than long and describe the default unit if it differs from milliseconds, e.g. “If a duration suffix is not specified, seconds will be used”.
    【TS】使用java.time。持续时间,而不是很长,并描述默认单位,如果它不同于毫秒,例如。“如果没有指定持续时间后缀,将使用秒”。
  • Do not provide the default value in the description unless it has to be determined at runtime.
    【TS】除非必须在运行时确定,否则不要在描述中提供默认值。

Make sure to trigger meta-data generation so that IDE assistance is available for your keys as well. You may want to review the generated metadata (META-INF/spring-configuration-metadata.json) to make sure your keys are properly documented. Using your own starter in a compatible IDE is also a good idea to validate that quality of the metadata.
【TS】除非必须在运行时确定,否则不要在描述中提供默认值。
确保触发元数据生成,以便您的密钥也可以使用IDE帮助。您可能需要查看生成的元数据(META-INF/spring-configuration-metadata.json),以确保您的键得到了适当的文档记录。在一个兼容的IDE中使用您自己的starter也是验证元数据质量的一个好主意。

  • autoconfigure 模块

The autoconfigure module contains everything that is necessary to get started with the library. It may also contain configuration key definitions (such as @ConfigurationProperties) and any callback interface that can be used to further customize how the components are initialized.
【TS】autoconfigure模块包含启动库所需的所有内容。它还可能包含配置键定义(比如@ConfigurationProperties)和任何回调接口,这些回调接口可用于进一步定制如何初始化组件。

You should mark the dependencies to the library as optional so that you can include the autoconfigure module in your projects more easily. If you do it that way, the library is not provided and, by default, Spring Boot backs off.
【TS】您应该将库的依赖项标记为可选,以便能够更轻松地在项目中包含autoconfigure模块。如果这样做,就不提供这个库,并且默认情况下,Spring引导会回退。

Spring Boot uses an annotation processor to collect the conditions on auto-configurations in a metadata file (META-INF/spring-autoconfigure-metadata.properties). If that file is present, it is used to eagerly filter auto-configurations that do not match, which will improve startup time. It is recommended to add the following dependency in a module that contains auto-configurations:
【TS】Spring Boot使用一个注解处理器来收集元数据文件(META-INF/ Spring -autoconfigure-metada .properties)中的自动配置条件。如果存在该文件,则使用它来急切地筛选不匹配的自动配置,这将改进启动时间。建议在包含自动配置的模块中添加以下依赖项:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure-processor</artifactId><optional>true</optional>
</dependency>
  • Starter模块

The starter is really an empty jar. Its only purpose is to provide the necessary dependencies to work with the library. You can think of it as an opinionated view of what is required to get started.
【TS】启动器实际上是一个空罐子。它的唯一目的是提供与库一起工作所需的依赖项。你可以把它想象成一个关于开始需要什么的观点。

Do not make assumptions about the project in which your starter is added. If the library you are auto-configuring typically requires other starters, mention them as well. Providing a proper set of default dependencies may be hard if the number of optional dependencies is high, as you should avoid including dependencies that are unnecessary for a typical usage of the library. In other words, you should not include optional dependencies.
【TS】不要对添加启动程序的项目做出假设。如果您正在自动配置的库通常需要其他启动程序,那么也要提到它们。如果可选依赖项的数量很高,那么提供一组适当的缺省依赖项可能会很困难,因为您应该避免包含对于库的典型使用来说不必要的依赖项。换句话说,您不应该包含可选的依赖项。

Either way, your starter must reference the core Spring Boot starter (spring-boot-starter) directly or indirectly (i.e. no need to add it if your starter relies on another starter). If a project is created with only your custom starter, Spring Boot’s core features will be honoured by the presence of the core starter.
【TS】无论哪种方式,启动程序都必须直接或间接地引用核心的Spring Boot starter(spring-boot-starter),也就是说,如果启动程序依赖于另一个启动程序,则无需添加它。如果只使用自定义启动器创建项目,那么Spring boot的核心特性将通过核心启动器的存在而得到尊重。


读官方文档,学原味技术!

手把手图文完成自定义日志Starter的制作和使用,带自定义properties参数

由于本章内容过多,案例重新上贴,点击阅读

【读官方文档,学原味技术】SpringBoot-Staters和自定义Starter相关推荐

  1. 跟着官方文档学DGL框架第一天——DGL概览

    主要参考:https://docs.dgl.ai/tutorials/basics/1_first.html DGL是什么 DGL是一种用于简化图神经网络实现的包,感觉官方文档写得很亲民,打算好好拜读 ...

  2. 跟着官方文档学DGL框架第七天——下载和处理数据集

    参考链接 https://docs.dgl.ai/guide/data.html#guide-data-pipeline https://docs.dgl.ai/en/0.5.x/_modules/d ...

  3. Qt官方文档阅读笔记-对官方Star Delegate Example实例的解析

    对应的博文为: 目录 Star Delegate Example StarDelegate Class Definition StarDelegate Class Implementation Sta ...

  4. Detectron2 官方文档详细解读 (上)

    Detectron2解读全部文章链接: Facebook计算机视觉开源框架Detectron2学习笔记 - 从demo到训练自己的模型 Detectron2 "快速开始" Dete ...

  5. 一起学微软Power BI系列-官方文档-入门指南(2)获取源数据

    阅读目录 1.系列文章说明 2.入门指南(2)获取数据源 3.资源 我们在文章: 一起学微软Power BI系列-官方文档-入门指南(1)Power BI初步介绍中,我们介绍了官方入门文档的第一章.今 ...

  6. Spring 4 官方文档学习 Spring与Java EE技术的集成

    本部分覆盖了以下内容: Chapter 28, Remoting and web services using Spring -- 使用Spring进行远程和web服务 Chapter 29, Ent ...

  7. android mvvm官方文档,MVVM: 这是一个android MVVM 框架,基于谷歌dataBinding技术实现

    MVVM 这是一个android MVVM 框架,基于谷歌dataBinding技术实现.dataBinding 实现的 V 和 VM的关联:使用IOC架构实现了 M 和 V的关联. 框架具有以下功能 ...

  8. Poco 库开发-教你如何读Poco的官方文档

    引言 在使用任何一种框架开发的时候,都不免要阅读官方的文档. 下面就讲述如何来读Poco库的官方文档 Poco库 在我们下载好Poco 后,会有两个文件夹,一个是放文档的doc,另外一个就是代码了,这 ...

  9. oracle12c官方文档中文版_三分钟让你真正读懂oracle12c 中cdb pdb概念及原理

    Oracle 12C引入了CDB与PDB的新特性,在ORACLE 12C数据库引入的多租用户环境(Multitenant Environment)中,允许一个数据库容器(CDB)承载多个可插拔数据库( ...

最新文章

  1. python3 tkinter电子书_python3 tkinter实现添加图片和文本
  2. Visual Studio LightSwitch初体验和定位看法
  3. 【英语学习】【WOTD】bardolater 释义/词源/示例
  4. Sklearn fit , transform ,fit_transform
  5. MySQL获取汉字拼音首字母
  6. 遥感图像预处理—步骤
  7. html5 mediarecorder,用浏览器轻松录制音频、视频—— MediaRecorder API
  8. php如何实现mac端重签ipa,越狱插件:实现自动重签,让你不再掉签的越狱插件ReProvision...
  9. Strut2简单使用
  10. 关于Binder (AIDL)的 oneway 机制
  11. conventional-changelog 参数含义
  12. Android手机界面组成
  13. 分享电脑日常使用的小技巧
  14. LC-3学习记录(一)
  15. PLC学习笔记(一):概述
  16. 用css实现垂直水平居中的几种方法
  17. 风投掘金可穿戴设备:大数据才是背后真金
  18. 打印系统开发(15)——WinForm开发(43)——winform 使用打印机
  19. Unix 哲学的一些原则——《Unix 编程艺术》
  20. Trustdata:互联网企业应该承担怎样的社会责任?

热门文章

  1. java英语单词测试_Java 英语单词自测
  2. 丰泽区python培训机构
  3. ant-design-vue 中标签页tab上额外的元素(tabBarExtraContent) , tab选项卡头增加文字
  4. 年轻人的第一个桌面应用
  5. 【前端Vue+后端Node.js+MySql】部署到服务器
  6. 转载,汉语世界上最先进的语言(来自几年前的转发,如今重新转发)
  7. 【1163】阿克曼(Ackmann)函数
  8. 简单来说,我们常听到的 22nm、14nm、10nm 究竟是什么意思?
  9. 在线支付功能-含银联在线和支付宝
  10. android 小视频 应用,短视频应用专题 - 只给你好看的短视频合集 - Android 应用 - 【最美应用】...