|
1 | 1 | /* |
2 | | - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
28 | 28 | import org.springframework.beans.factory.BeanCreationException; |
29 | 29 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
30 | 30 | import org.springframework.beans.factory.config.BeanDefinition; |
| 31 | +import org.springframework.beans.factory.support.AbstractBeanDefinition; |
31 | 32 | import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
32 | 33 | import org.springframework.beans.factory.support.RootBeanDefinition; |
33 | 34 | import org.springframework.beans.factory.support.StaticListableBeanFactory; |
34 | 35 | import org.springframework.beans.testfixture.beans.TestBean; |
35 | 36 | import org.springframework.context.MessageSource; |
36 | 37 | import org.springframework.context.annotation2.NamedStubDao2; |
37 | 38 | import org.springframework.context.support.GenericApplicationContext; |
| 39 | +import org.springframework.context.testfixture.index.CandidateComponentsTestClassLoader; |
| 40 | +import org.springframework.core.io.ClassPathResource; |
38 | 41 | import org.springframework.core.type.filter.AnnotationTypeFilter; |
39 | 42 | import org.springframework.core.type.filter.AssignableTypeFilter; |
40 | 43 | import org.springframework.stereotype.Component; |
@@ -104,10 +107,66 @@ public void testSimpleScanWithDefaultFiltersAndPrimaryLazyBean() { |
104 | 107 | @Test |
105 | 108 | public void testDoubleScan() { |
106 | 109 | GenericApplicationContext context = new GenericApplicationContext(); |
| 110 | + |
107 | 111 | ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
108 | 112 | int beanCount = scanner.scan(BASE_PACKAGE); |
109 | 113 | assertThat(beanCount).isEqualTo(12); |
110 | | - scanner.scan(BASE_PACKAGE); |
| 114 | + |
| 115 | + ClassPathBeanDefinitionScanner scanner2 = new ClassPathBeanDefinitionScanner(context) { |
| 116 | + @Override |
| 117 | + protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) { |
| 118 | + super.postProcessBeanDefinition(beanDefinition, beanName); |
| 119 | + beanDefinition.setAttribute("someDifference", "someValue"); |
| 120 | + } |
| 121 | + }; |
| 122 | + scanner2.scan(BASE_PACKAGE); |
| 123 | + |
| 124 | + assertThat(context.containsBean("serviceInvocationCounter")).isTrue(); |
| 125 | + assertThat(context.containsBean("fooServiceImpl")).isTrue(); |
| 126 | + assertThat(context.containsBean("stubFooDao")).isTrue(); |
| 127 | + assertThat(context.containsBean("myNamedComponent")).isTrue(); |
| 128 | + assertThat(context.containsBean("myNamedDao")).isTrue(); |
| 129 | + assertThat(context.containsBean("thoreau")).isTrue(); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testWithIndex() { |
| 134 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 135 | + context.setClassLoader(CandidateComponentsTestClassLoader.index( |
| 136 | + ClassPathScanningCandidateComponentProviderTests.class.getClassLoader(), |
| 137 | + new ClassPathResource("spring.components", FooServiceImpl.class))); |
| 138 | + |
| 139 | + ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
| 140 | + int beanCount = scanner.scan(BASE_PACKAGE); |
| 141 | + assertThat(beanCount).isEqualTo(12); |
| 142 | + |
| 143 | + assertThat(context.containsBean("serviceInvocationCounter")).isTrue(); |
| 144 | + assertThat(context.containsBean("fooServiceImpl")).isTrue(); |
| 145 | + assertThat(context.containsBean("stubFooDao")).isTrue(); |
| 146 | + assertThat(context.containsBean("myNamedComponent")).isTrue(); |
| 147 | + assertThat(context.containsBean("myNamedDao")).isTrue(); |
| 148 | + assertThat(context.containsBean("thoreau")).isTrue(); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testDoubleScanWithIndex() { |
| 153 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 154 | + context.setClassLoader(CandidateComponentsTestClassLoader.index( |
| 155 | + ClassPathScanningCandidateComponentProviderTests.class.getClassLoader(), |
| 156 | + new ClassPathResource("spring.components", FooServiceImpl.class))); |
| 157 | + |
| 158 | + ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
| 159 | + int beanCount = scanner.scan(BASE_PACKAGE); |
| 160 | + assertThat(beanCount).isEqualTo(12); |
| 161 | + |
| 162 | + ClassPathBeanDefinitionScanner scanner2 = new ClassPathBeanDefinitionScanner(context) { |
| 163 | + @Override |
| 164 | + protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) { |
| 165 | + super.postProcessBeanDefinition(beanDefinition, beanName); |
| 166 | + beanDefinition.setAttribute("someDifference", "someValue"); |
| 167 | + } |
| 168 | + }; |
| 169 | + scanner2.scan(BASE_PACKAGE); |
111 | 170 |
|
112 | 171 | assertThat(context.containsBean("serviceInvocationCounter")).isTrue(); |
113 | 172 | assertThat(context.containsBean("fooServiceImpl")).isTrue(); |
|
0 commit comments