View Javadoc
1   /*
2    * Copyright 2002-2014 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.spr12334;
18  
19  import org.junit.Test;
20  
21  import org.springframework.beans.factory.support.BeanDefinitionRegistry;
22  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
23  import org.springframework.context.annotation.Configuration;
24  import org.springframework.context.annotation.Import;
25  import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
26  import org.springframework.core.type.AnnotationMetadata;
27  
28  /**
29   * @author Juergen Hoeller
30   * @author Alex Pogrebnyak
31   */
32  public class Spr12334Tests {
33  
34  	@Test
35  	public void shouldNotScanTwice() {
36  		TestImport.scanned = false;
37  
38  		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
39  		context.scan(TestImport.class.getPackage().getName());
40  		context.refresh();
41  		context.getBean(TestConfiguration.class);
42  	}
43  
44  
45  	@Import(TestImport.class)
46  	public @interface AnotherImport {
47  	}
48  
49  
50  	@Configuration
51  	@AnotherImport
52  	public static class TestConfiguration {
53  	}
54  
55  
56  	public static class TestImport implements ImportBeanDefinitionRegistrar {
57  
58  		private static boolean scanned = false;
59  
60  		@Override
61  		public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry)  {
62  			if (scanned) {
63  				throw new IllegalStateException("Already scanned");
64  			}
65  			scanned = true;
66  		}
67  	}
68  
69  }