1. Overview
1.概述
Spring Boot made configuring Spring easier with its auto-configuration feature.
Spring Boot通过其自动配置功能使配置Spring更加容易。
In this quick tutorial, we’ll explore the annotations from the org.springframework.boot.autoconfigure and org.springframework.boot.autoconfigure.condition packages.
在这个快速教程中,我们将探讨org.springframework.boot.autoconfigure和org.springframework.boot.autoconfigure.condition包的注解。
2. @SpringBootApplication
2.@SpringBootApplication
We use this annotation to mark the main class of a Spring Boot application:
我们使用这个注解来标记Spring Boot应用程序的主类。
@SpringBootApplication
class VehicleFactoryApplication {
public static void main(String[] args) {
SpringApplication.run(VehicleFactoryApplication.class, args);
}
}
@SpringBootApplication encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.
@SpringBootApplication封装了@Configuration、@EnableAutoConfiguration和@ComponentScan注释及其默认属性。
3. @EnableAutoConfiguration
3.@EnableAutoConfiguration
@EnableAutoConfiguration, as its name says, enables auto-configuration. It means that Spring Boot looks for auto-configuration beans on its classpath and automatically applies them.
@EnableAutoConfiguration,正如它的名字一样,启用自动配置。这意味着Spring Boot在其classpath上寻找自动配置Bean并自动应用它们。
Note, that we have to use this annotation with @Configuration:
注意,我们必须将这个注解与@Configuration一起使用。
@Configuration
@EnableAutoConfiguration
class VehicleFactoryConfig {}
4. Auto-Configuration Conditions
4.自动配置条件
Usually, when we write our custom auto-configurations, we want Spring to use them conditionally. We can achieve this with the annotations in this section.
通常,当我们编写自定义自动配置时,我们希望Spring能够有条件地使用它们。我们可以通过本节中的注解来实现这一点。
We can place the annotations in this section on @Configuration classes or @Bean methods.
我们可以将这部分的注解放在@Configuration类或@Bean方法上。
In the next sections, we’ll only introduce the basic concept behind each condition. For further information, please visit this article.
在接下来的章节中,我们将只介绍每个条件背后的基本概念。欲了解更多信息,请访问这篇文章。
4.1. @ConditionalOnClass and @ConditionalOnMissingClass
4.1.@ConditionalOnClass 和 @ConditionalOnMissingClass
Using these conditions, Spring will only use the marked auto-configuration bean if the class in the annotation’s argument is present/absent:
使用这些条件,Spring将只在注解的参数中的类存在/不存在的情况下使用标记的自动配置Bean。
@Configuration
@ConditionalOnClass(DataSource.class)
class MySQLAutoconfiguration {
//...
}
4.2. @ConditionalOnBean and @ConditionalOnMissingBean
4.2.@ConditionalOnBean 和 @ConditionalOnMissingBean
We can use these annotations when we want to define conditions based on the presence or absence of a specific bean:
当我们想根据特定Bean的存在与否来定义条件时,我们可以使用这些注解。
@Bean
@ConditionalOnBean(name = "dataSource")
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// ...
}
4.3. @ConditionalOnProperty
4.3.@ConditionalOnProperty
With this annotation, we can make conditions on the values of properties:
有了这个注解,我们可以对属性的值提出条件。
@Bean
@ConditionalOnProperty(
name = "usemysql",
havingValue = "local"
)
DataSource dataSource() {
// ...
}
4.4. @ConditionalOnResource
4.4.@ConditionalOnResource
We can make Spring to use a definition only when a specific resource is present:
我们可以让Spring只在特定的资源存在时才使用一个定义。
@ConditionalOnResource(resources = "classpath:mysql.properties")
Properties additionalProperties() {
// ...
}
4.5. @ConditionalOnWebApplication and @ConditionalOnNotWebApplication
4.5.@ConditionalOnWebApplication 和 @ConditionalOnNotWebApplication
With these annotations, we can create conditions based on if the current application is or isn’t a web application:
通过这些注解,我们可以根据当前应用程序是否为Web应用程序来创建条件。
@ConditionalOnWebApplication
HealthCheckController healthCheckController() {
// ...
}
4.6. @ConditionalExpression
4.6.@ConditionalExpression
We can use this annotation in more complex situations. Spring will use the marked definition when the SpEL expression is evaluated to true:
我们可以在更复杂的情况下使用这个注解。当SpEL表达式被评估为真时,Spring将使用该标记的定义。
@Bean
@ConditionalOnExpression("${usemysql} && ${mysqlserver == 'local'}")
DataSource dataSource() {
// ...
}
4.7. @Conditional
4.7.@Conditional(条件)
For even more complex conditions, we can create a class evaluating the custom condition. We tell Spring to use this custom condition with @Conditional:
对于更复杂的条件,我们可以创建一个评估custom condition的类。我们用@Conditional告诉Spring要使用这个自定义条件。
@Conditional(HibernateCondition.class)
Properties additionalProperties() {
//...
}
5. Conclusion
5.总结
In this article, we saw an overview of how can we fine-tune the auto-configuration process and provide conditions for custom auto-configuration beans.
在这篇文章中,我们看到了一个关于如何微调自动配置过程和为自定义自动配置Bean提供条件的概述。
As usual, the examples are available over on GitHub.
像往常一样,这些例子可以在GitHub上找到over。