SpringBoot官方文档翻译(十八):使用@SpringBootApplication注解

18. Using the @SpringBootApplication Annotation(使用@SpringBootApplication注解)

1
2
3
4
Many Spring Boot developers like their apps to use auto-configuration,     
component scan and be able to define extra configuration on their
"application class". A single@SpringBootApplication annotation
can be used to enable those tree features, that is:

很多Spring Boot的开发者想要他们的应用能够使用自动配置,组件扫描和定义额外的配置在他们的“主类”里。一个单独的@SpringBootApplication注解即可将这3个功能都实现,也就是:

1
2
3
* @EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism
* @ComponentScan: enable @Component scan on the package where the application is located (see the best practices)
* @Configuration: allow to register extra beans in the context or import additional configuration classes
  • @EnableAutoConfiguration: 开启Spring Boot的自动配置机制
  • @ComponentSca:开启主类路径包下的组件自动扫描
  • @Configuration :允许注册额外的beans到context容器中,或者导入更多的配置类
    1
    2
    3
    4
    The @SpringBootApplication annotation is equivalent     
    to using @Configuration, @EnableAutoConfiguration,
    and @ComponentScan with their default attributes,
    as shown in the following example:

@SpringBootApplication注解等同于使用@Configuration, @EnableAutoConfiguration,
和 @ComponentScan
这3个注解,并且使用了他们默认的属性配置,如下例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

1
2
@SpringBootApplication also provides aliases to customize the     
attributes of @EnableAutoConfiguration and @ComponentScan.

@SpringBootApplication注解同样提供别名去定制化@EnableAutoConfiguration@EnableAutoConfiguration属性。

1
2
3
4
None of these features are mandatory and you may chose to     
replace this single annotation by any of the features that it
enables. For instance, you may not want to use component scan
in your application:

这些功能都不是强制性的,您可以选择通过任何其他的开启的功能去替换这个单一的注解。
例如,您可能不想使用组件扫描在你的应用中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

1
2
3
4
In this example, Application is just like any other Spring Boot     
application except that @Component-annotated classes are not
detected automatically and the user-defined beans are imported
explicitly (see @Import).

在上面这个例子中,应用如同其他Spring Boot应用一样,
除了组件注解的类不是被自动导入以及用户自定义beans被明确导入(见@Import)。

分享到