1. Overview
1.概述
In this tutorial, we’ll briefly discuss the @SpringBootConfiguration annotation. We’ll also look at its usage in a Spring Boot application.
在本教程中,我们将简要讨论@SpringBootConfiguration注解。我们还将看看它在Spring Boot应用程序中的用法。
2. Spring Boot Application Configuration
2.Spring Boot应用配置
@SpringBootConfiguration is a class-level annotation that is part of the Spring Boot framework. It indicates that a class provides application configuration.
@SpringBootConfiguration是一个类级注解,是Spring Boot框架的一部分。它表示一个类提供应用配置。
Spring Boot favors Java-based configuration. As a result, the @SpringBootConfiguration annotation is the primary source for configuration in applications. Generally, the class that defines the main() method is a good candidate for this annotation.
Spring Boot倾向于基于Java的配置。因此,@SpringBootConfiguration注解是应用程序中配置的主要来源。一般来说,定义main()方法的类是该注解的良好候选者。
2.1. @SpringBootConfiguration
2.1.@SpringBootConfiguration
Most Spring Boot use @SpringBootConfiguration via @SpringBootApplication, an annotation that inherits from it. If an application uses @SpringBootApplication, it is already using @SpringBootConfiguration.
大多数Spring Boot通过@SpringBootApplication使用@SpringBootConfiguration,这是一个继承自它的注释。如果一个应用程序使用@SpringBootApplication,它已经在使用@SpringBootConfiguration。
Let’s look at @SpringBootConfiguration’s usage in an application.
让我们看看@SpringBootConfiguration的在应用程序中的用法。
First, we create an application class that contains our configuration:
首先,我们创建一个包含我们配置的应用类。
@SpringBootConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public PersonService personService() {
return new PersonServiceImpl();
}
}
The @SpringBootConfiguration annotation annotates the Application class. This indicates to the Spring container that the class has @Bean definition methods. In other words, it contains methods that instantiate and configure our dependencies.
@SpringBootConfiguration 注解注释了Application类。这向Spring容器表明,该类有@Bean定义方法。换句话说,它包含了实例化和配置我们的依赖关系的方法。
For example, the Application class contains the bean definition method for the PersonService bean.
例如,Application类包含PersonServicebean的bean定义方法。
Furthermore, the container processes the configuration class. This, in turn, generates beans for the application. As a result, we can now use Dependency Injection annotations like @Autowired or @Inject.
此外,容器还处理配置类。这反过来又为应用程序生成了Bean。因此,我们现在可以使用依赖注入注解,如@Autowired或@Inject。
2.2. @SpringBootConfiguration vs @Configuration
2.2.@SpringBootConfiguration vs @Configuration
@SpringBootConfiguration is an alternative to the @Configuration annotation. The main difference is that @SpringBootConfiguration allows configuration to be automatically located. This can be especially useful for unit or integration tests.
@SpringBootConfiguration是@Configuration注释的替代品。主要区别在于,@SpringBootConfiguration允许配置被自动定位。这对单元或集成测试特别有用。
The recommendation is to only have one @SpringBootConfiguration or @SpringBootApplication for your application. Most applications will simply use @SpringBootApplication.
我们的建议是,你的应用程序只需要一个@SpringBootConfiguration或@SpringBootApplication。大多数应用程序将简单地使用@SpringBootApplication.。
3. Conclusion
3.总结
In this article, we got a quick look at the @SpringBootConfiguration annotation. Further, we looked at @SpringBootConfiguration‘s usage in a Spring Boot Application. We also reviewed Spring’s @Bean annotation.
在这篇文章中,我们快速了解了@SpringBootConfiguration注解。此外,我们还研究了@SpringBootConfiguration在Spring Boot应用中的用法。我们还回顾了Spring的@Bean注解。
The full source code of our examples here is, as always, over on GitHub.
我们这里的例子的完整源代码一如既往地在GitHub上。