@ComponentScan具有多个配置类:基于注释的配置 [英] @ComponentScan with multiple configuration class : Annotation Based Configuration
问题描述
根据Spring Doc-
配置与@Configuration类一起使用的组件扫描指令.提供与Spring XML的
<context:component-scan>
元素并行的支持.
在我的spring Web应用程序中,有多个标记为@Configuration
的文件,以便在Spring容器中注册@component
bean-
问题1-我们可以在 问题2 我也在春季看到了文档 为什么在这里扫描配置类本身. 基本上,我对 问题1- 是的,您可以使用 让我们说-您有要扫描组件的 要在容器中注册 任何一个 或 您的问题2- spring不仅在注册 As per Spring Doc- Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's In my spring web application there are multiple files those are marked Question1- Can we use Question2- Also I seen in spring doc Why here scanning for configuration class itself. edit: Basically my understanding with For your Question 1 - yes, you can register a bean using Let say- you have To register Either Or For your Question 2 - Here spring is not only registering 这篇关于@ComponentScan具有多个配置类:基于注释的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!@Configuration
类的任何或所有 @Configuration
类中使用@ComponentScan
吗? >
@Configuration
@ComponentScan(basePackageClasses = { MyConfiguration.class })
public class MyConfiguration extends WebMvcConfigurerAdapter {
...
}
@ComponentScan
的理解是扫描和注册立体声类型bean(例如-@componant
,@Controller
,@Services
等.),为什么我们要注册@Configuration
Bean.>
@ComponentScan
在任何配置在弹簧容器中的配置豆中注册.您可以通过任何方式将Bean注册到容器中以下方式之一-
@Configuration
在rootcontext
中注册Bean或
dispatchersevletcontext
.@Configuration
bean(已在容器中注册).MvcConfig
类-@ComponentScan(basePackages = {"xxxx","yyyy","zzzz"})
@Configuration
public class MvcConfig {
....
}
MvcConfig
,您必须做new AnnotationConfigWebApplicationContext().register(MvcConfig.class);
new AnnotationConfigWebApplicationContext().register(AnotherConfig.class);
@Configuration
@Import({MvcConfig.class})
public class AnotherConfig {
....
}
MyConfiguration.class
,而且还在注册MyConfiguration
的程序包中存在的所有组件类.
<context:component-scan>
element.@Configuration
,in order to register @component
bean in spring container-@ComponentScan
in any of the @Configuration
class or in all @Configuration
classes?@Configuration
@ComponentScan(basePackageClasses = { MyConfiguration.class })
public class MyConfiguration extends WebMvcConfigurerAdapter {
...
}
@ComponentScan
is to scan and register stereo type bean(ex- @componant
,@Controller
,@Services
etc..),why we are registering @Configuration
Bean.@ComponentScan
in any of the configuration bean which is registered in spring container.you can register a bean into container by any of the following way-
@Configuration
to register bean in rootcontext
or
dispatchersevletcontext
.@Configuration
bean (which is already registered in container).MvcConfig
class in which you are scanning component-@ComponentScan(basePackages = {"xxxx","yyyy","zzzz"})
@Configuration
public class MvcConfig {
....
}
MvcConfig
in container you must do-new AnnotationConfigWebApplicationContext().register(MvcConfig.class);
new AnnotationConfigWebApplicationContext().register(AnotherConfig.class);
@Configuration
@Import({MvcConfig.class})
public class AnotherConfig {
....
}
MyConfiguration.class
but also all the component classes those are present in the package in which MyConfiguration
defined.