@Bean配置时一段经典的源码

如下代码:

1
2
3
4
5
6
7
8

@Bean
@ConditionalOnBean(MultipartResolver.class) 
@ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
public MultipartResolver multipartResolver(MultipartResolver resolver) {
    return resolver;
}

这段配置的含义是这样的,如果容器中有dispatcherServlet的bean,容器中有MultipartResolver.class的bean,那么我们将创造一个名为multipartResolver的Bean。具体的操作是从容器中注入的MultipartResolver.class的Bean,返回一个名为multipartResolver的Bean。