|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -24,6 +24,7 @@ import example.scannable.StubFooDao; |
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeanInstantiationException; |
|
|
|
import org.springframework.beans.factory.BeanCreationException; |
|
|
|
import org.springframework.beans.factory.BeanCreationException; |
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
|
|
import org.springframework.beans.factory.config.BeanDefinition; |
|
|
|
@ -69,9 +70,11 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); |
|
|
|
assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)); |
|
|
|
assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); |
|
|
|
assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)); |
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
FooServiceImpl service = context.getBean("fooServiceImpl", FooServiceImpl.class); |
|
|
|
|
|
|
|
|
|
|
|
FooServiceImpl fooService = context.getBean("fooServiceImpl", FooServiceImpl.class); |
|
|
|
assertTrue(context.getDefaultListableBeanFactory().containsSingleton("myNamedComponent")); |
|
|
|
assertTrue(context.getDefaultListableBeanFactory().containsSingleton("myNamedComponent")); |
|
|
|
assertEquals("bar", service.foo(1)); |
|
|
|
assertEquals("bar", fooService.foo(123)); |
|
|
|
|
|
|
|
assertEquals("bar", fooService.lookupFoo(123)); |
|
|
|
assertTrue(context.isPrototype("thoreau")); |
|
|
|
assertTrue(context.isPrototype("thoreau")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -88,11 +91,13 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
assertTrue(context.containsBean("myNamedDao")); |
|
|
|
assertTrue(context.containsBean("myNamedDao")); |
|
|
|
assertTrue(context.containsBean("otherFooDao")); |
|
|
|
assertTrue(context.containsBean("otherFooDao")); |
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
|
|
|
|
|
|
|
|
assertFalse(context.getBeanFactory().containsSingleton("otherFooDao")); |
|
|
|
assertFalse(context.getBeanFactory().containsSingleton("otherFooDao")); |
|
|
|
assertFalse(context.getBeanFactory().containsSingleton("fooServiceImpl")); |
|
|
|
assertFalse(context.getBeanFactory().containsSingleton("fooServiceImpl")); |
|
|
|
FooServiceImpl service = context.getBean("fooServiceImpl", FooServiceImpl.class); |
|
|
|
FooServiceImpl fooService = context.getBean("fooServiceImpl", FooServiceImpl.class); |
|
|
|
assertTrue(context.getBeanFactory().containsSingleton("otherFooDao")); |
|
|
|
assertTrue(context.getBeanFactory().containsSingleton("otherFooDao")); |
|
|
|
assertEquals("other", service.foo(1)); |
|
|
|
assertEquals("other", fooService.foo(123)); |
|
|
|
|
|
|
|
assertEquals("other", fooService.lookupFoo(123)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -102,6 +107,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
assertEquals(12, beanCount); |
|
|
|
assertEquals(12, beanCount); |
|
|
|
scanner.scan(BASE_PACKAGE); |
|
|
|
scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("fooServiceImpl")); |
|
|
|
assertTrue(context.containsBean("fooServiceImpl")); |
|
|
|
assertTrue(context.containsBean("stubFooDao")); |
|
|
|
assertTrue(context.containsBean("stubFooDao")); |
|
|
|
@ -117,6 +123,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
scanner.setIncludeAnnotationConfig(false); |
|
|
|
scanner.setIncludeAnnotationConfig(false); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
assertEquals(6, beanCount); |
|
|
|
assertEquals(6, beanCount); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("fooServiceImpl")); |
|
|
|
assertTrue(context.containsBean("fooServiceImpl")); |
|
|
|
assertTrue(context.containsBean("stubFooDao")); |
|
|
|
assertTrue(context.containsBean("stubFooDao")); |
|
|
|
@ -159,6 +166,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
scanner.setIncludeAnnotationConfig(false); |
|
|
|
scanner.setIncludeAnnotationConfig(false); |
|
|
|
int scannedBeanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int scannedBeanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(5, scannedBeanCount); |
|
|
|
assertEquals(5, scannedBeanCount); |
|
|
|
assertEquals(initialBeanCount + scannedBeanCount, context.getBeanDefinitionCount()); |
|
|
|
assertEquals(initialBeanCount + scannedBeanCount, context.getBeanDefinitionCount()); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
@ -178,6 +186,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
scanner.setIncludeAnnotationConfig(false); |
|
|
|
scanner.setIncludeAnnotationConfig(false); |
|
|
|
int scannedBeanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int scannedBeanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(5, scannedBeanCount); |
|
|
|
assertEquals(5, scannedBeanCount); |
|
|
|
assertEquals(initialBeanCount + scannedBeanCount, context.getBeanDefinitionCount()); |
|
|
|
assertEquals(initialBeanCount + scannedBeanCount, context.getBeanDefinitionCount()); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
@ -220,6 +229,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false); |
|
|
|
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); |
|
|
|
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(7, beanCount); |
|
|
|
assertEquals(7, beanCount); |
|
|
|
assertTrue(context.containsBean("messageBean")); |
|
|
|
assertTrue(context.containsBean("messageBean")); |
|
|
|
assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); |
|
|
|
assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)); |
|
|
|
@ -235,6 +245,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false); |
|
|
|
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); |
|
|
|
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(7, beanCount); |
|
|
|
assertEquals(7, beanCount); |
|
|
|
assertTrue(context.containsBean("messageBean")); |
|
|
|
assertTrue(context.containsBean("messageBean")); |
|
|
|
assertFalse(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertFalse(context.containsBean("serviceInvocationCounter")); |
|
|
|
@ -255,6 +266,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true); |
|
|
|
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); |
|
|
|
scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class)); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(13, beanCount); |
|
|
|
assertEquals(13, beanCount); |
|
|
|
assertTrue(context.containsBean("messageBean")); |
|
|
|
assertTrue(context.containsBean("messageBean")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
@ -275,6 +287,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true); |
|
|
|
scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class)); |
|
|
|
scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class)); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(11, beanCount); |
|
|
|
assertEquals(11, beanCount); |
|
|
|
assertFalse(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertFalse(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("fooServiceImpl")); |
|
|
|
assertTrue(context.containsBean("fooServiceImpl")); |
|
|
|
@ -293,6 +306,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, true); |
|
|
|
scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class)); |
|
|
|
scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class)); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(11, beanCount); |
|
|
|
assertEquals(11, beanCount); |
|
|
|
assertFalse(context.containsBean("fooServiceImpl")); |
|
|
|
assertFalse(context.containsBean("fooServiceImpl")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
@ -313,6 +327,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
scanner.setIncludeAnnotationConfig(false); |
|
|
|
scanner.setIncludeAnnotationConfig(false); |
|
|
|
scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class)); |
|
|
|
scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class)); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(5, beanCount); |
|
|
|
assertEquals(5, beanCount); |
|
|
|
assertFalse(context.containsBean("fooServiceImpl")); |
|
|
|
assertFalse(context.containsBean("fooServiceImpl")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertTrue(context.containsBean("serviceInvocationCounter")); |
|
|
|
@ -331,6 +346,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class)); |
|
|
|
scanner.addExcludeFilter(new AssignableTypeFilter(FooService.class)); |
|
|
|
scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class)); |
|
|
|
scanner.addExcludeFilter(new AnnotationTypeFilter(Aspect.class)); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(10, beanCount); |
|
|
|
assertEquals(10, beanCount); |
|
|
|
assertFalse(context.containsBean("fooServiceImpl")); |
|
|
|
assertFalse(context.containsBean("fooServiceImpl")); |
|
|
|
assertFalse(context.containsBean("serviceInvocationCounter")); |
|
|
|
assertFalse(context.containsBean("serviceInvocationCounter")); |
|
|
|
@ -350,6 +366,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
scanner.setBeanNameGenerator(new TestBeanNameGenerator()); |
|
|
|
scanner.setBeanNameGenerator(new TestBeanNameGenerator()); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(12, beanCount); |
|
|
|
assertEquals(12, beanCount); |
|
|
|
assertFalse(context.containsBean("fooServiceImpl")); |
|
|
|
assertFalse(context.containsBean("fooServiceImpl")); |
|
|
|
assertTrue(context.containsBean("fooService")); |
|
|
|
assertTrue(context.containsBean("fooService")); |
|
|
|
@ -403,6 +420,7 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
MessageSource ms = (MessageSource) context.getBean("messageSource"); |
|
|
|
MessageSource ms = (MessageSource) context.getBean("messageSource"); |
|
|
|
assertTrue(fooService.isInitCalled()); |
|
|
|
assertTrue(fooService.isInitCalled()); |
|
|
|
assertEquals("bar", fooService.foo(123)); |
|
|
|
assertEquals("bar", fooService.foo(123)); |
|
|
|
|
|
|
|
assertEquals("bar", fooService.lookupFoo(123)); |
|
|
|
assertSame(context.getDefaultListableBeanFactory(), fooService.beanFactory); |
|
|
|
assertSame(context.getDefaultListableBeanFactory(), fooService.beanFactory); |
|
|
|
assertEquals(2, fooService.listableBeanFactory.size()); |
|
|
|
assertEquals(2, fooService.listableBeanFactory.size()); |
|
|
|
assertSame(context.getDefaultListableBeanFactory(), fooService.listableBeanFactory.get(0)); |
|
|
|
assertSame(context.getDefaultListableBeanFactory(), fooService.listableBeanFactory.get(0)); |
|
|
|
@ -426,13 +444,13 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
int beanCount = scanner.scan(BASE_PACKAGE); |
|
|
|
assertEquals(6, beanCount); |
|
|
|
assertEquals(6, beanCount); |
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
FooService fooService = (FooService) context.getBean("fooService"); |
|
|
|
|
|
|
|
assertFalse(fooService.isInitCalled()); |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
fooService.foo(123); |
|
|
|
context.getBean("fooService"); |
|
|
|
fail("NullPointerException expected; fooDao must not have been set"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
catch (NullPointerException expected) { |
|
|
|
catch (BeanCreationException expected) { |
|
|
|
|
|
|
|
assertTrue(expected.contains(BeanInstantiationException.class)); |
|
|
|
|
|
|
|
// @Lookup method not substituted
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -442,11 +460,13 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
scanner.setIncludeAnnotationConfig(true); |
|
|
|
scanner.setIncludeAnnotationConfig(true); |
|
|
|
scanner.setBeanNameGenerator(new TestBeanNameGenerator()); |
|
|
|
scanner.setBeanNameGenerator(new TestBeanNameGenerator()); |
|
|
|
scanner.setAutowireCandidatePatterns(new String[] { "*FooDao" }); |
|
|
|
scanner.setAutowireCandidatePatterns("*FooDao"); |
|
|
|
scanner.scan(BASE_PACKAGE); |
|
|
|
scanner.scan(BASE_PACKAGE); |
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
FooService fooService = (FooService) context.getBean("fooService"); |
|
|
|
|
|
|
|
|
|
|
|
FooServiceImpl fooService = (FooServiceImpl) context.getBean("fooService"); |
|
|
|
assertEquals("bar", fooService.foo(123)); |
|
|
|
assertEquals("bar", fooService.foo(123)); |
|
|
|
|
|
|
|
assertEquals("bar", fooService.lookupFoo(123)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -455,8 +475,9 @@ public class ClassPathBeanDefinitionScannerTests { |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); |
|
|
|
scanner.setIncludeAnnotationConfig(true); |
|
|
|
scanner.setIncludeAnnotationConfig(true); |
|
|
|
scanner.setBeanNameGenerator(new TestBeanNameGenerator()); |
|
|
|
scanner.setBeanNameGenerator(new TestBeanNameGenerator()); |
|
|
|
scanner.setAutowireCandidatePatterns(new String[] { "*NoSuchDao" }); |
|
|
|
scanner.setAutowireCandidatePatterns("*NoSuchDao"); |
|
|
|
scanner.scan(BASE_PACKAGE); |
|
|
|
scanner.scan(BASE_PACKAGE); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
context.refresh(); |
|
|
|
context.refresh(); |
|
|
|
context.getBean("fooService"); |
|
|
|
context.getBean("fooService"); |
|
|
|
|