|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 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.context.MessageSource;
|
35 | 36 | import org.springframework.context.annotation2.NamedStubDao2;
|
| 37 | +import org.springframework.context.index.CandidateComponentsTestClassLoader; |
36 | 38 | import org.springframework.context.support.GenericApplicationContext;
|
| 39 | +import org.springframework.core.io.ClassPathResource; |
37 | 40 | import org.springframework.core.type.filter.AnnotationTypeFilter;
|
38 | 41 | import org.springframework.core.type.filter.AssignableTypeFilter;
|
39 | 42 | import org.springframework.stereotype.Component;
|
40 |
| -import org.springframework.tests.sample.beans.TestBean; |
41 | 43 |
|
42 | 44 | import static org.junit.Assert.*;
|
43 | 45 |
|
@@ -103,10 +105,66 @@ public void testSimpleScanWithDefaultFiltersAndPrimaryLazyBean() {
|
103 | 105 | @Test
|
104 | 106 | public void testDoubleScan() {
|
105 | 107 | GenericApplicationContext context = new GenericApplicationContext();
|
| 108 | + |
106 | 109 | ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
|
107 | 110 | int beanCount = scanner.scan(BASE_PACKAGE);
|
108 | 111 | assertEquals(13, beanCount);
|
109 |
| - scanner.scan(BASE_PACKAGE); |
| 112 | + |
| 113 | + ClassPathBeanDefinitionScanner scanner2 = new ClassPathBeanDefinitionScanner(context) { |
| 114 | + @Override |
| 115 | + protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) { |
| 116 | + super.postProcessBeanDefinition(beanDefinition, beanName); |
| 117 | + beanDefinition.setAttribute("someDifference", "someValue"); |
| 118 | + } |
| 119 | + }; |
| 120 | + scanner2.scan(BASE_PACKAGE); |
| 121 | + |
| 122 | + assertTrue(context.containsBean("serviceInvocationCounter")); |
| 123 | + assertTrue(context.containsBean("fooServiceImpl")); |
| 124 | + assertTrue(context.containsBean("stubFooDao")); |
| 125 | + assertTrue(context.containsBean("myNamedComponent")); |
| 126 | + assertTrue(context.containsBean("myNamedDao")); |
| 127 | + assertTrue(context.containsBean("thoreau")); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testWithIndex() { |
| 132 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 133 | + context.setClassLoader(CandidateComponentsTestClassLoader.index( |
| 134 | + ClassPathScanningCandidateComponentProviderTests.class.getClassLoader(), |
| 135 | + new ClassPathResource("spring.components", FooServiceImpl.class))); |
| 136 | + |
| 137 | + ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
| 138 | + int beanCount = scanner.scan(BASE_PACKAGE); |
| 139 | + assertEquals(13, beanCount); |
| 140 | + |
| 141 | + assertTrue(context.containsBean("serviceInvocationCounter")); |
| 142 | + assertTrue(context.containsBean("fooServiceImpl")); |
| 143 | + assertTrue(context.containsBean("stubFooDao")); |
| 144 | + assertTrue(context.containsBean("myNamedComponent")); |
| 145 | + assertTrue(context.containsBean("myNamedDao")); |
| 146 | + assertTrue(context.containsBean("thoreau")); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testDoubleScanWithIndex() { |
| 151 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 152 | + context.setClassLoader(CandidateComponentsTestClassLoader.index( |
| 153 | + ClassPathScanningCandidateComponentProviderTests.class.getClassLoader(), |
| 154 | + new ClassPathResource("spring.components", FooServiceImpl.class))); |
| 155 | + |
| 156 | + ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
| 157 | + int beanCount = scanner.scan(BASE_PACKAGE); |
| 158 | + assertEquals(13, beanCount); |
| 159 | + |
| 160 | + ClassPathBeanDefinitionScanner scanner2 = new ClassPathBeanDefinitionScanner(context) { |
| 161 | + @Override |
| 162 | + protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) { |
| 163 | + super.postProcessBeanDefinition(beanDefinition, beanName); |
| 164 | + beanDefinition.setAttribute("someDifference", "someValue"); |
| 165 | + } |
| 166 | + }; |
| 167 | + scanner2.scan(BASE_PACKAGE); |
110 | 168 |
|
111 | 169 | assertTrue(context.containsBean("serviceInvocationCounter"));
|
112 | 170 | assertTrue(context.containsBean("fooServiceImpl"));
|
|
0 commit comments