Search in sources :

Example 1 with FooServiceImpl

use of example.scannable.FooServiceImpl in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerTests method testSimpleScanWithDefaultFiltersAndPrimaryLazyBean.

@Test
public void testSimpleScanWithDefaultFiltersAndPrimaryLazyBean() {
    GenericApplicationContext context = new GenericApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.scan(BASE_PACKAGE);
    scanner.scan("org.springframework.context.annotation5");
    assertThat(context.containsBean("serviceInvocationCounter")).isTrue();
    assertThat(context.containsBean("fooServiceImpl")).isTrue();
    assertThat(context.containsBean("stubFooDao")).isTrue();
    assertThat(context.containsBean("myNamedComponent")).isTrue();
    assertThat(context.containsBean("myNamedDao")).isTrue();
    assertThat(context.containsBean("otherFooDao")).isTrue();
    context.refresh();
    assertThat(context.getBeanFactory().containsSingleton("otherFooDao")).isFalse();
    assertThat(context.getBeanFactory().containsSingleton("fooServiceImpl")).isFalse();
    FooServiceImpl fooService = context.getBean("fooServiceImpl", FooServiceImpl.class);
    assertThat(context.getBeanFactory().containsSingleton("otherFooDao")).isTrue();
    assertThat(fooService.foo(123)).isEqualTo("other");
    assertThat(fooService.lookupFoo(123)).isEqualTo("other");
}
Also used : FooServiceImpl(example.scannable.FooServiceImpl) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Test(org.junit.jupiter.api.Test)

Example 2 with FooServiceImpl

use of example.scannable.FooServiceImpl in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerTests method testSimpleScanWithDefaultFiltersAndPostProcessors.

@Test
public void testSimpleScanWithDefaultFiltersAndPostProcessors() {
    GenericApplicationContext context = new GenericApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    int beanCount = scanner.scan(BASE_PACKAGE);
    assertThat(beanCount).isGreaterThanOrEqualTo(12);
    assertThat(context.containsBean("serviceInvocationCounter")).isTrue();
    assertThat(context.containsBean("fooServiceImpl")).isTrue();
    assertThat(context.containsBean("stubFooDao")).isTrue();
    assertThat(context.containsBean("myNamedComponent")).isTrue();
    assertThat(context.containsBean("myNamedDao")).isTrue();
    assertThat(context.containsBean("thoreau")).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME)).isTrue();
    assertThat(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME)).isTrue();
    context.refresh();
    FooServiceImpl fooService = context.getBean("fooServiceImpl", FooServiceImpl.class);
    assertThat(context.getDefaultListableBeanFactory().containsSingleton("myNamedComponent")).isTrue();
    assertThat(fooService.foo(123)).isEqualTo("bar");
    assertThat(fooService.lookupFoo(123)).isEqualTo("bar");
    assertThat(context.isPrototype("thoreau")).isTrue();
}
Also used : FooServiceImpl(example.scannable.FooServiceImpl) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Test(org.junit.jupiter.api.Test)

Example 3 with FooServiceImpl

use of example.scannable.FooServiceImpl in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerTests method testBeanAutowiredWithAnnotationConfigEnabled.

@Test
public void testBeanAutowiredWithAnnotationConfigEnabled() {
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition("myBf", new RootBeanDefinition(StaticListableBeanFactory.class));
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.setBeanNameGenerator(new TestBeanNameGenerator());
    int beanCount = scanner.scan(BASE_PACKAGE);
    assertThat(beanCount).isGreaterThanOrEqualTo(12);
    context.refresh();
    FooServiceImpl fooService = context.getBean("fooService", FooServiceImpl.class);
    StaticListableBeanFactory myBf = (StaticListableBeanFactory) context.getBean("myBf");
    MessageSource ms = (MessageSource) context.getBean("messageSource");
    assertThat(fooService.isInitCalled()).isTrue();
    assertThat(fooService.foo(123)).isEqualTo("bar");
    assertThat(fooService.lookupFoo(123)).isEqualTo("bar");
    assertThat(fooService.beanFactory).isSameAs(context.getDefaultListableBeanFactory());
    assertThat(fooService.listableBeanFactory.size()).isEqualTo(2);
    assertThat(fooService.listableBeanFactory.get(0)).isSameAs(context.getDefaultListableBeanFactory());
    assertThat(fooService.listableBeanFactory.get(1)).isSameAs(myBf);
    assertThat(fooService.resourceLoader).isSameAs(context);
    assertThat(fooService.resourcePatternResolver).isSameAs(context);
    assertThat(fooService.eventPublisher).isSameAs(context);
    assertThat(fooService.messageSource).isSameAs(ms);
    assertThat(fooService.context).isSameAs(context);
    assertThat(fooService.configurableContext.length).isEqualTo(1);
    assertThat(fooService.configurableContext[0]).isSameAs(context);
    assertThat(fooService.genericContext).isSameAs(context);
}
Also used : FooServiceImpl(example.scannable.FooServiceImpl) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MessageSource(org.springframework.context.MessageSource) Test(org.junit.jupiter.api.Test)

Example 4 with FooServiceImpl

use of example.scannable.FooServiceImpl in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerTests method testAutowireCandidatePatternMatches.

@Test
public void testAutowireCandidatePatternMatches() {
    GenericApplicationContext context = new GenericApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.setIncludeAnnotationConfig(true);
    scanner.setBeanNameGenerator(new TestBeanNameGenerator());
    scanner.setAutowireCandidatePatterns("*FooDao");
    scanner.scan(BASE_PACKAGE);
    context.refresh();
    FooServiceImpl fooService = (FooServiceImpl) context.getBean("fooService");
    assertThat(fooService.foo(123)).isEqualTo("bar");
    assertThat(fooService.lookupFoo(123)).isEqualTo("bar");
}
Also used : FooServiceImpl(example.scannable.FooServiceImpl) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

FooServiceImpl (example.scannable.FooServiceImpl)4 Test (org.junit.jupiter.api.Test)4 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)4 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 StaticListableBeanFactory (org.springframework.beans.factory.support.StaticListableBeanFactory)1 MessageSource (org.springframework.context.MessageSource)1