SpringBootApplication注解

@SpringBootApplication

Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

指示一个配置类,该类声明一个或多个@Bean方法,并触发自动配置和组件扫描。这是一个方便的注释,相当于声明@Configuration、@EnableAutoConfiguration和@ComponentScan。

1
2
3
4
5
6
7
8
9
10
11
12
@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 {
......
}

@SpringBootConfiguration

Indicates that a class provides Spring Boot application @Configuration. Can be used as an alternative to the Spring’s standard @Configuration annotation so that configuration can be found automatically (for example in tests).
Application should only ever include one @SpringBootConfiguration and most idiomatic Spring Boot applications will inherit it from @SpringBootApplication.

指示类提供了Spring启动应用程序@Configuration。可以作为Spring的标准@Configuration注释的替代,以便自动找到配置(例如在测试中)。
应用程序应该只包含一个@SpringBootConfiguration,大多数惯用的Spring引导应用程序将从@SpringBootApplication继承它。

1
2
3
4
5
6
7
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

@Configuration

Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime, for example:

表示一个类声明了一个或多个@Bean方法,并且可能被Spring容器处理以在运行时为这些bean生成bean定义和服务请求,例如:

1
2
3
4
5
6
7
8
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
// instantiate, configure and return bean ...
}
}

Via AnnotationConfigApplicationContext

@Configuration classes are typically bootstrapped using either AnnotationConfigApplicationContext or its web-capable variant, AnnotationConfigWebApplicationContext. A simple example with the former follows

@Configuration类通常是使用AnnotationConfigApplicationContext或其支持web的变体AnnotationConfigWebApplicationContext引导的。下面是关于前者的一个简单示例

1
2
3
4
5
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
MyBean myBean = ctx.getBean(MyBean.class);
// use myBean ...

Via component scanning

@Configuration is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning (typically using Spring XML’s element) and therefore may also take advantage of @Autowired/@Inject like any regular @Component. In particular, if a single constructor is present autowiring semantics will be applied transparently for that constructor:

@Configuration使用@Component进行元注释,因此@Configuration类是组件扫描的候选类(通常使用Spring XML的元素),因此也可以像使用任何常规的@Component一样利用@Autowired/@Inject。特别是,如果有一个单独的构造函数存在,自动装配语义将被透明地应用于该构造函数:

1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
public class AppConfig {
private final SomeBean someBean;
public AppConfig(SomeBean someBean) {
this.someBean = someBean;
}
// @Bean definition using "SomeBean"
}

@Configuration classes may not only be bootstrapped using component scanning, but may also themselves configure component scanning using the @ComponentScan annotation:

@Configuration类不仅可以使用组件扫描来引导,还可以自己使用@ComponentScan注释来配置组件扫描:

1
2
3
4
5
@Configuration
@ComponentScan("com.acme.app.services")
public class AppConfig {
// various @Bean definitions ...
}

Working with externalized values

Using the Environment API

Externalized values may be looked up by injecting the Spring org.springframework.core.env.Environment into a @Configuration class — for example, using the @Autowired annotation:
外部化的值可以通过注入Spring org.springframe.core.env.Environment来查找。例如,使用@Autowired注解:

1
2
3
4
5
6
7
8
9
10
11
12
@Configuration
public class AppConfig {
@Autowired Environment env;
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
myBean.setName(env.getProperty("bean.name"));
return myBean;
}
}

Properties resolved through the Environment reside in one or more “property source” objects, and @Configuration classes may contribute property sources to the Environment object using the @PropertySource annotation:

通过环境解析的属性驻留在一个或多个“属性源”对象中,而@Configuration类可以使用@PropertySource注释将属性源贡献给环境对象:

1
2
3
4
5
6
7
8
9
10
11
@Configuration
@PropertySource("classpath:/com/acme/app.properties")
public class AppConfig {
@Inject Environment env;
@Bean
public MyBean myBean() {
return new MyBean(env.getProperty("bean.name"));
}
}

Using the @Value annotation
Externalized values may be injected into @Configuration classes using the @Value annotation:

外部化的值可以使用@Value注释注入到@Configuration类中:

1
2
3
4
5
6
7
8
9
10
11
@Configuration
@PropertySource("classpath:/com/acme/app.properties")
public class AppConfig {
@Value("${bean.name}") String beanName;
@Bean
public MyBean myBean() {
return new MyBean(beanName);
}
}

Composing @Configuration classes

With the @Import annotation

@Configuration classes may be composed using the @Import annotation, similar to the way that works in Spring XML. Because @Configuration objects are managed as Spring beans within the container, imported configurations may be injected — for example, via constructor injection:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Configuration
public class DatabaseConfig {
@Bean
public DataSource dataSource() {
// instantiate, configure and return DataSource
}
}
@Configuration
@Import(DatabaseConfig.class)
public class AppConfig {
private final DatabaseConfig dataConfig;
public AppConfig(DatabaseConfig dataConfig) {
this.dataConfig = dataConfig;
}
@Bean
public MyBean myBean() {
// reference the dataSource() bean method
return new MyBean(dataConfig.dataSource());
}
}

Now both AppConfig and the imported DatabaseConfig can be bootstrapped by registering only AppConfig against the Spring context:
new AnnotationConfigApplicationContext(AppConfig.class);

Configuring lazy initialization

By default, @Bean methods will be eagerly instantiated at container bootstrap time. To avoid this, @Configuration may be used in conjunction with the @Lazy annotation to indicate that all @Bean methods declared within the class are by default lazily initialized. Note that @Lazy may be used on individual @Bean methods as well.

Testing support for @Configuration classes

The Spring TestContext framework available in the spring-test module provides the @ContextConfiguration annotation which can accept an array of @Configuration Class objects:

1
2
3
4
5
6
7
8
9
10
11
12
13
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AppConfig.class, DatabaseConfig.class})
public class MyTests {
@Autowired MyBean myBean;
@Autowired DataSource dataSource;
@Test
public void test() {
// assertions against myBean ...
}
}

@EnableAutoConfiguration

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 tomcat-embedded.jar on your classpath you are likely to want a TomcatServletWebServerFactory (unless you have defined your own ServletWebServerFactory bean).

启用Spring应用程序上下文的自动配置,它会尝试猜测和配置您可能需要的bean。自动配置类通常基于你的类路径和定义的bean来应用。例如,如果您的类路径中有tomcat-embedde .jar,那么您可能需要一个TomcatServletWebServerFactory(除非您已经定义了自己的ServletWebServerFactory bean)。

When using SpringBootApplication, the auto-configuration of the context is automatically enabled and adding this annotation has therefore no additional effect.
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 exclude() any configuration that you never want to apply (use excludeName() if you don’t have access to them). You can also exclude them via the spring.autoconfigure.exclude property. Auto-configuration is always applied after user-defined beans have been registered.

在使用SpringBootApplication时,上下文的自动配置是自动启用的,因此添加这个注释不会产生额外的影响。
自动配置试图尽可能地智能化,并且在您定义更多自己的配置时将会回退。您总是可以手动排除()您从不希望应用的任何配置(如果您没有访问它们的权限,则使用exclude())。您还可以通过spring.autoconfigure配置文件排除它们。,自动配置总是会在注册了用户定义的bean之后再应用上。

The package of the class that is annotated with @EnableAutoConfiguration, usually via @SpringBootApplication, has specific significance and is often used as a ‘default’. For example, it will be used when scanning for @Entity classes. It is generally recommended that you place @EnableAutoConfiguration (if you’re not using @SpringBootApplication) in a root package so that all sub-packages and classes can be searched.

被标识为@EnableAutoConfiguration的这个类它所在的包,通常通过@SpringBootApplication具有特殊的签名然后通常用作“默认”。
例如,它将在扫描@Entity类时使用。通常建议将@EnableAutoConfiguration(如果不使用@SpringBootApplication)放在根包中,这样的话所有子包和类都将被扫描到。

Auto-configuration classes are regular Spring Configuration beans. They are located using the SpringFactoriesLoader mechanism (keyed against this class). Generally auto-configuration beans are @Conditional beans (most often using @ConditionalOnClass and @ConditionalOnMissingBean annotations).

自动配置类是常规的Spring配置bean。它们是使用SpringFactoriesLoader机制定位的(针对这个类进行键控)。通常,自动配置bean是@Conditional bean(通常使用@ConditionalOnClass和@ConditionalOnMissingBean注释)。

@AutoConfigurationPackage

Indicates that the package containing the annotated class should be registered with AutoConfigurationPackages.

表示这个包包含了被注解的类应该被注册到AutoConfigurationPackages里面。

@ComponentScan

Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML’s element.
Either basePackageClasses or basePackages (or its alias value) may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

配置组件扫描指令以与@Configuration类一起使用。提供与Spring XML元素并行的支持。

可以指定basepackageclass或basePackages(或其别名值)来定义要扫描的特定包。如果没有定义特定的包,将对声明此注解的类所在的包开始扫描。