Skip to content

Allow recursive use of @ComponentScan [SPR-8307] #12956

Closed

Description

@spring-projects-issues

Chris Beams opened SPR-8307 and commented

Currently (in Spring 3.1 M1), @ComponentScan annotations are not processed recursively. For example:

package one;

@Configuration
@ComponentScan("two")
public class ConfigOne { }
package two;

@Configuration
@ComponentScan("three")
public class ConfigTwo { }
package three;

@Component
public class MyComponent { }

Under Spring 3.1 M1, the following behavior will occur:

package bootstrap;

public class Bootstrap {
    public static void main(String... args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(ConfigOne.class);
        ctx.refresh();

        ctx.getBean(ConfigOne.class); // success
        ctx.getBean(ConfigTwo.class); // success - 1st level @ComponentScan processed
        ctx.getBean(MyComponent.class); // fails - 2nd level @ComponentScan not processed
    }
}

Once the recursion logic is complete, it will also be important to detect and prevent cycles in scanned packages, e.g. @ComponentScan("one")=>@ComponentScan("two")=>@ComponentScan("one"). This is not unlike the cycle detection we already do with the @Import annotation.


Affects: 3.1 M1

Attachments:

Issue Links:

Referenced from: commits 6991cd9, d0c31ad

Activity

  1. spring-projects-issues commented on May 8, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Author: Chris Beams <[email protected]>
    Date:   Sun May 8 16:49:42 2011 +0800
    
        Allow recursive use of @ComponentScan
        
        Prior to this change, @ComponentScan annotations were only processed at
        the first level of depth.  Now, the set of bean definitions resulting
        from each declaration of @ComponentScan is checked for configuration
        classes that declare @ComponentScan, and recursion is performed as
        necessary.
        
        Cycles between @ComponentScan declarations are detected as well. See
        CircularComponentScanException.
        
        Issue: SPR-8307
    
  2. added this to the 3.1 M2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions