1. [@SpringBootApplication](#) 注解式标注一个SpringBoot应用的标志,比如下面的代码就可以正常的启动一个非常简单的SpringBoot应用
  1. @SpringBootApplication
  2. public class SpringbootApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SpringbootApplication.class, args);
  5. // 或者下面的这种方法
  6. SpringApplication application = new SpringApplication(SpringbootApplication.class);
  7. application.run(args);
  8. }
  9. }

SpringBootApplication

@SpringBootApplicaiton源码如下:

  • 标识出一个配置类,声明一个或者多个Bean的方法,同时触发启用自动配置以及组件扫描
  1. /**标识出一个配置类,声明一个或者多个Bean的方法,同时触发启用自动配置以及组件扫描 */
  2. @Target(ElementType.TYPE)
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @Documented
  5. @Inherited
  6. @SpringBootConfiguration
  7. @EnableAutoConfiguration
  8. @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  9. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  10. public @interface SpringBootApplication {//...}
  11. /*** 标识提供了SpringBoot的配置功能*/
  12. @Target(ElementType.TYPE)
  13. @Retention(RetentionPolicy.RUNTIME)
  14. @Documented
  15. // 标识一个类具有多个Bean方法,这些方法可以被Spring容器定义,使用
  16. @Configuration
  17. public @interface SpringBootConfiguration {//...}

Configuration

注解Configuration 是一个非常重要的注解,他标识了的类的Bean对象会被Spring容器管理,具有非常多的应用场景,下面展示常用的示例

  1. /* Indicates that a class declares one or more @Bean methods and may be processed by the Spring container
  2. to generate bean definitions and service requests for those beans at runtime, for example:
  3. 标识一个类具有多个Bean的方法,这些Bean 可以被Spring容器来定义BeanDefined以及使用
  4. */
  5. @Target(ElementType.TYPE)
  6. @Retention(RetentionPolicy.RUNTIME)
  7. @Documented
  8. @Component
  9. public @interface Configuration {//...}
  10. // 比如下面的形式
  11. @Configuration
  12. public class AppConfig {
  13. @Bean
  14. public MyBean myBean() {
  15. // instantiate, configure and return bean ...
  16. }
  17. }
  18. /*
  19. Bootstrapping @Configuration classes Via AnnotationConfigApplicationContext
  20. @Configuration classes are typically bootstrapped using either AnnotationConfigApplicationContext or its web-capable variant,
  21. AnnotationConfigWebApplicationContext. A simple example with the former follows:
  22. */
  23. //下面的示例展示了通过 AnnotationConfigApplicationContext 从@Configuration 标记的注解的类中获取Bean对象
  24. AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  25. ctx.register(AppConfig.class);
  26. ctx.refresh();
  27. MyBean myBean = ctx.getBean(MyBean.class);
  28. // use myBean ...
  29. // 也可以充分的应用@Autwired或者@Injec, 构造方法等方式来注入其他的bean.
  30. @Configuration
  31. @ComponentScan("com.zhoutao123.app.services") // 配置到哪里扫描需要Bean
  32. public class AppConfig {
  33. private final SomeBean someBean;
  34. public AppConfig(SomeBean someBean) {
  35. this.someBean = someBean;
  36. }
  37. // @Bean definition using "SomeBean"
  38. }
  39. // 也可以从 Environment 中获取环境变量值
  40. @Configuration
  41. public class AppConfig {
  42. @Autowired Environment env;
  43. @Bean
  44. public MyBean myBean() {
  45. MyBean myBean = new MyBean();
  46. myBean.setName(env.getProperty("bean.name"));
  47. return myBean;
  48. }
  49. }
  50. // 甚至可以指定Properties元数据 然后从中获取环境变量值
  51. @Configuration
  52. @PropertySource("classpath:/com/zhoutao123/app.properties")
  53. public class AppConfig {
  54. @Inject Environment env;
  55. @Bean
  56. public MyBean myBean() {
  57. return new MyBean(env.getProperty("bean.name"));
  58. }
  59. }
  60. // 当然也可以使用@Value注解,没有PropertySource,会从SpringBoot的默认配置文件注入
  61. @Configuration
  62. @PropertySource("classpath:/com/zhoutao123/app.properties")
  63. public class AppConfig {
  64. @Value("${bean.name}")
  65. String beanName;
  66. @Bean
  67. public MyBean myBean() {
  68. return new MyBean(beanName);
  69. }
  70. }
  71. // 使用 @Import 注入其他Config,由于其他@Configuration 对象是被Spring 容器管理的,因此其可以注入到AppConfig对象中
  72. @Configuration
  73. public class DatabaseConfig {
  74. @Bean
  75. public DataSource dataSource() {
  76. // instantiate, configure and return DataSource
  77. }
  78. }
  79. @Configuration
  80. @Import(DatabaseConfig.class)
  81. public class AppConfig {
  82. private final DatabaseConfig dataConfig;
  83. public AppConfig(DatabaseConfig dataConfig) {
  84. this.dataConfig = dataConfig;
  85. }
  86. @Bean
  87. public MyBean myBean() {
  88. // reference the dataSource() bean method
  89. return new MyBean(dataConfig.dataSource());
  90. }
  91. }
  92. //还有其他的使用场景,更多情况这里不再过多的赘述,可以参考文件 org.springframework.context.annotation.Configuration
  • Configuration classes must be provided as classes (i.e. not as instances returned from factory methods), allowing for runtime enhancements through a generated subclass. 必须是一个类
  • Configuration classes must be non-final (allowing for subclasses at runtime), unless the proxyBeanMethods flag is set to false in which case no runtime-generated subclass is necessary. 此类必须不是final修饰的
  • Configuration classes must be non-local (i.e. may not be declared within a method). 配置类必须不是
  • Any nested configuration classes must be declared as static. 嵌套的内部类必须是静态的
  • @Bean methods may not in turn create further configuration classes (any such instances will be treated as regular beans, with their configuration annotations remaining undetected). Bean方法修饰的并不是further类型的配置类