“Spring Boot @Configuration 详解”的版本间的差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		使用 
		
	
| Jihongchang(讨论 | 贡献) | Jihongchang(讨论 | 贡献)  | ||
| 第8行: | 第8行: | ||
| 告诉 Spring Boot 添加了 @Configuration 注解的类,是一个配置类,作用等同于以前的 XML 配置文件。 | 告诉 Spring Boot 添加了 @Configuration 注解的类,是一个配置类,作用等同于以前的 XML 配置文件。 | ||
| − | + | ==== 使用 <code>@Bean</code> 往容器中注册组件 ==== | |
| + | <syntaxhighlight lang="java"> | ||
| @Configuration //告诉 SpringBoot 这是一个配置类 == 配置文件 | @Configuration //告诉 SpringBoot 这是一个配置类 == 配置文件 | ||
| public class MyConfig { | public class MyConfig { | ||
| 第32行: | 第33行: | ||
| } | } | ||
| − | </syntaxhighlight> | + | </syntaxhighlight>运行 MainApplication:<syntaxhighlight lang="java"> | 
| − | |||
| − | 运行 MainApplication:<syntaxhighlight lang="java"> | ||
| /** | /** | ||
|   * 主程序类 |   * 主程序类 | ||
| 第78行: | 第77行: | ||
| worldController | worldController | ||
| …… | …… | ||
| − | </syntaxhighlight>可以观察到容器中多了  | + | </syntaxhighlight>可以观察到容器中多了 tomcatPet 和 user01 两个 bean,默认使用方法名作为组件名,也可以通过 <code>@Bean("tom")</code> 自定义组件名,再运行 MainApplication:<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>可以发现组件名就变成自定义的组件名了。 | ||
2023年2月12日 (日) 07:59的版本
https://www.bilibili.com/video/BV19K4y1L7MT/?p=8
回顾旧版本通过 XML 配置文件进行 bean 配置
@Configuration 的作用
告诉 Spring Boot 添加了 @Configuration 注解的类,是一个配置类,作用等同于以前的 XML 配置文件。
使用 @Bean 往容器中注册组件
@Configuration //告诉 SpringBoot 这是一个配置类 == 配置文件
public class MyConfig {
    /**
     * 给容器中添加组件。
     * 以方法名作为组件的id。
     * 返回类型就是组件类型。
     * 返回的值,就是组件在容器中的实例
     *
     * 外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
     * @return
     */
    @Bean
    public User user01() {
        return new User("zhangsan", 18);
    }
    @Bean
    public Pet tomcatPet() {
        return new Pet("tomcat");
    }
}
运行 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
tomcatPet
tomcatServletWebServerFactory
tomcatServletWebServerFactoryCustomizer
tomcatWebServerFactoryCustomizer
user01
viewControllerHandlerMapping
viewResolver
webServerFactoryCustomizerBeanPostProcessor
websocketServletWebServerCustomizer
welcomePageHandlerMapping
worldController
……
可以观察到容器中多了 tomcatPet 和 user01 两个 bean,默认使用方法名作为组件名,也可以通过 @Bean("tom") 自定义组件名,再运行 MainApplication:
……
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
……
可以发现组件名就变成自定义的组件名了。