“Spring Boot @Configuration 详解”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
| 第33行: | 第33行: | ||
} | } | ||
</syntaxhighlight>使用 @Bean 注解往容器中添加组件。 | </syntaxhighlight>使用 @Bean 注解往容器中添加组件。 | ||
| + | |||
| + | 运行 MainApplication:<syntaxhighlight lang="java"> | ||
| + | /** | ||
| + | * 主程序类 | ||
| + | * @SpringBootApplication:这是一个 Spring Boot 应用 | ||
| + | */ | ||
| + | @SpringBootApplication(scanBasePackages = "io.github.jihch") | ||
| + | public class MainApplication { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | //1、返回 IOC 容器 | ||
| + | ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args); | ||
| + | |||
| + | //2、查看容器里面的组件 | ||
| + | String[] beanDefinitionNames = context.getBeanDefinitionNames(); | ||
| + | Arrays.sort(beanDefinitionNames); | ||
| + | for (String name : beanDefinitionNames) { | ||
| + | System.out.println(name); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight><syntaxhighlight lang="console"> | ||
| + | …… | ||
| + | spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties | ||
| + | spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties | ||
| + | spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties | ||
| + | spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties | ||
| + | standardJacksonObjectMapperBuilderCustomizer | ||
| + | stringHttpMessageConverter | ||
| + | taskExecutorBuilder | ||
| + | taskSchedulerBuilder | ||
| + | tom | ||
| + | tomcatServletWebServerFactory | ||
| + | tomcatServletWebServerFactoryCustomizer | ||
| + | tomcatWebServerFactoryCustomizer | ||
| + | user01 | ||
| + | viewControllerHandlerMapping | ||
| + | viewResolver | ||
| + | webServerFactoryCustomizerBeanPostProcessor | ||
| + | websocketServletWebServerCustomizer | ||
| + | welcomePageHandlerMapping | ||
| + | worldController | ||
| + | …… | ||
| + | </syntaxhighlight>可以观察到容器中多了 tom 和 user01 两个 bean | ||
2023年2月12日 (日) 07:51的版本
https://www.bilibili.com/video/BV19K4y1L7MT/?p=8
回顾旧版本通过 XML 配置文件进行 bean 配置
@Configuration 的作用
告诉 Spring Boot 添加了 @Configuration 注解的类,是一个配置类,作用等同于以前的 XML 配置文件。
比如为容器添加组件:
@Configuration //告诉 SpringBoot 这是一个配置类 == 配置文件
public class MyConfig {
/**
* 给容器中添加组件。
* 以方法名作为组件的id。
* 返回类型就是组件类型。
* 返回的值,就是组件在容器中的实例
*
* 外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
* @return
*/
@Bean
public User user01() {
return new User("zhangsan", 18);
}
@Bean("tom")
public Pet tomcatPet() {
return new Pet("tomcat");
}
}
使用 @Bean 注解往容器中添加组件。 运行 MainApplication:
/**
* 主程序类
* @SpringBootApplication:这是一个 Spring Boot 应用
*/
@SpringBootApplication(scanBasePackages = "io.github.jihch")
public class MainApplication {
public static void main(String[] args) {
//1、返回 IOC 容器
ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的组件
String[] beanDefinitionNames = context.getBeanDefinitionNames();
Arrays.sort(beanDefinitionNames);
for (String name : beanDefinitionNames) {
System.out.println(name);
}
}
}
……
spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties
spring.servlet.multipart-org.springframework.boot.autoconfigure.web.servlet.MultipartProperties
spring.task.execution-org.springframework.boot.autoconfigure.task.TaskExecutionProperties
spring.task.scheduling-org.springframework.boot.autoconfigure.task.TaskSchedulingProperties
standardJacksonObjectMapperBuilderCustomizer
stringHttpMessageConverter
taskExecutorBuilder
taskSchedulerBuilder
tom
tomcatServletWebServerFactory
tomcatServletWebServerFactoryCustomizer
tomcatWebServerFactoryCustomizer
user01
viewControllerHandlerMapping
viewResolver
webServerFactoryCustomizerBeanPostProcessor
websocketServletWebServerCustomizer
welcomePageHandlerMapping
worldController
……
可以观察到容器中多了 tom 和 user01 两个 bean