View Javadoc
1   /*
2    * Copyright 2002-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.context.annotation;
18  
19  import org.junit.Test;
20  
21  import org.springframework.context.annotation.componentscan.cycle.left.LeftConfig;
22  import org.springframework.context.annotation.componentscan.level1.Level1Config;
23  import org.springframework.context.annotation.componentscan.level2.Level2Config;
24  import org.springframework.context.annotation.componentscan.level3.Level3Component;
25  
26  import static org.hamcrest.CoreMatchers.*;
27  import static org.junit.Assert.*;
28  
29  /**
30   * Tests ensuring that configuration classes marked with @ComponentScan
31   * may be processed recursively
32   *
33   * @author Chris Beams
34   * @since 3.1
35   */
36  public class ComponentScanAnnotationRecursionTests {
37  
38  	@Test
39  	public void recursion() {
40  		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
41  		ctx.register(Level1Config.class);
42  		ctx.refresh();
43  
44  		// assert that all levels have been detected
45  		ctx.getBean(Level1Config.class);
46  		ctx.getBean(Level2Config.class);
47  		ctx.getBean(Level3Component.class);
48  
49  		// assert that enhancement is working
50  		assertThat(ctx.getBean("level1Bean"), sameInstance(ctx.getBean("level1Bean")));
51  		assertThat(ctx.getBean("level2Bean"), sameInstance(ctx.getBean("level2Bean")));
52  	}
53  
54  	public void evenCircularScansAreSupported() {
55  		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
56  		ctx.register(LeftConfig.class); // left scans right, and right scans left
57  		ctx.refresh();
58  		ctx.getBean("leftConfig");      // but this is handled gracefully
59  		ctx.getBean("rightConfig");     // and beans from both packages are available
60  	}
61  
62  }