Using @ComponentScan or with only one class

后端 未结 4 587
鱼传尺愫
鱼传尺愫 2020-12-06 03:56

I\'m maintaining a project with two set of main packages, the project is using Spring and Spring MVC, one of these packages contains several controllers and is scanned using

相关标签:
4条回答
  • 2020-12-06 04:44

    What @Bart said for XML.

    If you need to pull in that one class using annotations, add the following to one of your @Configuration classes

    @ComponentScan(
        basePackageClasses = YourClass.class, 
        useDefaultFilters = false,
        includeFilters = {
            @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = YourClass.class)
        })
    
    0 讨论(0)
  • 2020-12-06 04:49

    Simply add is as a bean to your context e.g.

    <bean class="my.package.MyClass" />
    
    0 讨论(0)
  • 2020-12-06 04:56

    You need to use filters to filter out other classes and just include your class which you want to be scanned

    <context:component-scan base-package="com.abc" >
    
        <context:include-filter type="regex" 
                       expression="com.abc.customer.dao.*DAO.*" /> 
    
    </context:component-scan>
    
    0 讨论(0)
  • 2020-12-06 05:00

    In addition to the method described by Emerson Farrugia there is a less verbose solution which has been supported since Spring Framework 4.2 as mentioned in the documentation here.

    As of Spring Framework 4.2, @Import also supports references regular component classes, analogous to the AnnotationConfigApplicationContext.register method. This is particularly useful if you want to avoid component scanning, by using a few configuration classes as entry points to explicitly define all your components.

    So your example would simply become:

    @Import(YourClass.class)
    
    0 讨论(0)
提交回复
热议问题