org.springframework.context.support.GenericApplicationContext Java Examples

The following examples show how to use org.springframework.context.support.GenericApplicationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QualifierAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldResolvesWithBaseQualifierAndNonDefaultValue() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue("the real juergen");
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue("juergen imposter");
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "not really juergen"));
	context.registerBeanDefinition("juergen1", person1);
	context.registerBeanDefinition("juergen2", person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean bean =
			(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean) context.getBean("autowired");
	assertEquals("the real juergen", bean.getPerson().getName());
}
 
Example #2
Source File: SpringVerticleFactory.java    From spring-vertx-ext with Apache License 2.0 6 votes vote down vote up
private Verticle createSpringVerticle(final Class<?> currentVerticleClass, ClassLoader classLoader) {
    final SpringVerticle annotation = currentVerticleClass.getAnnotation(SpringVerticle.class);
    final Class<?> springConfigClass = annotation.springConfig();
    
    // Create the parent context  
    final GenericApplicationContext genericApplicationContext = getGenericApplicationContext(
        classLoader);

    // 1. Create a new context for each verticle and use the specified spring configuration class if possible
    AnnotationConfigApplicationContext annotationConfigApplicationContext = getAnnotationConfigApplicationContext(
        springConfigClass, genericApplicationContext);

    // 2. Register the Vertx instance as a singleton in spring context
    annotationConfigApplicationContext.getBeanFactory().registerSingleton(vertx.getClass().getSimpleName(), vertx);

    // 3. Register a bean definition for this verticle
    annotationConfigApplicationContext.registerBeanDefinition(currentVerticleClass.getSimpleName(), new VerticleBeanDefinition(currentVerticleClass));
    
    // 4. Add a bean factory post processor to avoid configuration issues
    addPostprocessorAndUpdateContext(currentVerticleClass, annotationConfigApplicationContext);
    
    // 5. Return the verticle by fetching the bean from the context
    return (Verticle) annotationConfigApplicationContext.getBeanFactory().getBean(currentVerticleClass.getSimpleName());
}
 
Example #3
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldResolvesMetaQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(MetaQualifiedFieldTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	MetaQualifiedFieldTestBean bean = (MetaQualifiedFieldTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example #4
Source File: HeaderMethodArgumentResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {
	@SuppressWarnings("resource")
	GenericApplicationContext cxt = new GenericApplicationContext();
	cxt.refresh();
	this.resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());

	Method method = getClass().getDeclaredMethod("handleMessage",
			String.class, String.class, String.class, String.class, String.class);
	this.paramRequired = new SynthesizingMethodParameter(method, 0);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramNotAnnotated = new SynthesizingMethodParameter(method, 3);
	this.paramNativeHeader = new SynthesizingMethodParameter(method, 4);

	this.paramRequired.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
	GenericTypeResolver.resolveParameterType(this.paramRequired, HeaderMethodArgumentResolver.class);
}
 
Example #5
Source File: ClassPathBeanDefinitionScannerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCustomIncludeFilterWithoutDefaultsAndNoPostProcessors() {
	GenericApplicationContext context = new GenericApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false);
	scanner.addIncludeFilter(new AnnotationTypeFilter(CustomComponent.class));
	int beanCount = scanner.scan(BASE_PACKAGE);

	assertEquals(6, beanCount);
	assertTrue(context.containsBean("messageBean"));
	assertFalse(context.containsBean("serviceInvocationCounter"));
	assertFalse(context.containsBean("fooServiceImpl"));
	assertFalse(context.containsBean("stubFooDao"));
	assertFalse(context.containsBean("myNamedComponent"));
	assertFalse(context.containsBean("myNamedDao"));
	assertTrue(context.containsBean(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_PROCESSOR_BEAN_NAME));
	assertTrue(context.containsBean(AnnotationConfigUtils.EVENT_LISTENER_FACTORY_BEAN_NAME));
}
 
Example #6
Source File: AsyncExecutionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void asyncMethodsWithQualifier() throws Exception {
	originalThreadName = Thread.currentThread().getName();
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("asyncTest", new RootBeanDefinition(AsyncMethodWithQualifierBean.class));
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	context.registerBeanDefinition("e0", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.registerBeanDefinition("e1", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.registerBeanDefinition("e2", new RootBeanDefinition(ThreadPoolTaskExecutor.class));
	context.refresh();

	AsyncMethodWithQualifierBean asyncTest = context.getBean("asyncTest", AsyncMethodWithQualifierBean.class);
	asyncTest.doNothing(5);
	asyncTest.doSomething(10);
	Future<String> future = asyncTest.returnSomething(20);
	assertEquals("20", future.get());
	Future<String> future2 = asyncTest.returnSomething2(30);
	assertEquals("30", future2.get());
}
 
Example #7
Source File: SchemaPluginConfiguration.java    From liiklus with MIT License 6 votes vote down vote up
@Override
public void initialize(GenericApplicationContext applicationContext) {
    var environment = applicationContext.getEnvironment();
    if (!environment.acceptsProfiles(Profiles.of("gateway"))) {
        return;
    }

    var schemaProperties = PropertiesUtil.bind(environment, new SchemaProperties());

    if (!schemaProperties.isEnabled()) {
        return;
    }

    applicationContext.registerBean(RecordPreProcessor.class, () -> {
        return new JsonSchemaPreProcessor(
                schemaProperties.getSchemaURL(),
                JsonPointer.compile(schemaProperties.getEventTypeJsonPointer()),
                schemaProperties.isAllowDeprecatedProperties()
        );
    });
}
 
Example #8
Source File: InjectAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue("the real juergen");
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue("juergen imposter");
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	context.registerBeanDefinition("juergen1", person1);
	context.registerBeanDefinition("juergen2", person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #9
Source File: InjectAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #10
Source File: LoggerBeanNamesResolverTest.java    From eclair with Apache License 2.0 6 votes vote down vote up
@Test
public void resolveManyLoggersWithPrimary() {
    // given
    String beanName = "simpleLogger";
    String emptyLoggerName = "";
    String alias = "logger";
    String beanName2 = "simpleLogger2";
    String alias2 = "logger2";
    AbstractBeanDefinition primaryDefinition = givenLoggerBeanDefinition();
    primaryDefinition.setPrimary(true);
    GenericApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerBeanDefinition(beanName, primaryDefinition);
    applicationContext.getBeanFactory().registerAlias(beanName, alias);
    applicationContext.registerBeanDefinition(beanName2, givenLoggerBeanDefinition());
    applicationContext.getBeanFactory().registerAlias(beanName2, alias2);
    // when
    Set<String> namesByBeanName = loggerBeanNamesResolver.resolve(applicationContext, beanName);
    // then
    assertThat(namesByBeanName, hasSize(3));
    assertThat(namesByBeanName, containsInAnyOrder(beanName, emptyLoggerName, alias));
}
 
Example #11
Source File: QualifierAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldResolvesMetaQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(MetaQualifiedFieldTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	MetaQualifiedFieldTestBean bean = (MetaQualifiedFieldTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example #12
Source File: QualifierAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredMethodParameterWithStaticallyQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(QualifiedPerson.class, cavs, null);
	context.registerBeanDefinition(JUERGEN,
			ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(person, JUERGEN), context, true).getBeanDefinition());
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedMethodParameterTestBean bean =
			(QualifiedMethodParameterTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example #13
Source File: QualifierAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue("the real juergen");
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue("juergen imposter");
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
	context.registerBeanDefinition("juergen1", person1);
	context.registerBeanDefinition("juergen2", person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e instanceof UnsatisfiedDependencyException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #14
Source File: AspectJAutoProxyCreatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAspectsAndAdvisorNotAppliedToManySingletonsIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);

	GenericApplicationContext ac = new GenericApplicationContext();

	new XmlBeanDefinitionReader(ac).loadBeanDefinitions(new ClassPathResource(qName("aspectsPlusAdvisor.xml"),
			getClass()));
	for (int i = 0; i < 10000; i++) {
		ac.registerBeanDefinition("singleton" + i, new RootBeanDefinition(NestedTestBean.class));
	}
	StopWatch sw = new StopWatch();
	sw.start("Singleton Creation");
	ac.refresh();
	sw.stop();

	// What's a reasonable expectation for _any_ server or developer machine load?
	// 8 seconds?
	assertStopWatchTimeLimit(sw, 8000);
}
 
Example #15
Source File: QualifierAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #16
Source File: PostgresqlDatabaseClientInitializer.java    From spring-fu with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {

	PostgresqlConnectionConfiguration configuration = PostgresqlConnectionConfiguration
			.builder()
			.host(this.properties.getHost())
			.port(this.properties.getPort())
			.database(this.properties.getDatabase())
			.username(this.properties.getUsername())
			.password(this.properties.getPassword())
			.build();

	context.registerBean(DatabaseClient.class, () -> DatabaseClient.builder().connectionFactory(new PostgresqlConnectionFactory(configuration)).build());
}
 
Example #17
Source File: InjectAnnotationAutowireContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutowiredMethodParameterWithSingleNonQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null);
	context.registerBeanDefinition(JUERGEN, person);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedMethodParameterTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #18
Source File: BeanValidationPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSizeConstraint() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	RootBeanDefinition bd = new RootBeanDefinition(NotNullConstrainedBean.class);
	bd.getPropertyValues().add("testBean", new TestBean());
	bd.getPropertyValues().add("stringValue", "s");
	ac.registerBeanDefinition("bean", bd);
	try {
		ac.refresh();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause().getMessage().contains("stringValue"));
		assertTrue(ex.getRootCause().getMessage().contains("invalid"));
	}
	ac.close();
}
 
Example #19
Source File: JsfCdiToSpringApplicationBeanFactoryPostProcessorIT.java    From joinfaces with Apache License 2.0 6 votes vote down vote up
@Test
public void testViewScopedClass() {
	GenericApplicationContext acx = new GenericApplicationContext();
	AnnotationConfigUtils.registerAnnotationConfigProcessors(acx);

	acx.registerBeanDefinition("viewScopedClass", new AnnotatedGenericBeanDefinition(
		new StandardAnnotationMetadata(ViewScopedClass.class)));
	acx.registerBeanDefinition("scopedBeansConfiguration", new RootBeanDefinition(
		ScopedBeansConfiguration.class));
	acx.addBeanFactoryPostProcessor(JsfScopeAnnotationsAutoConfiguration.jsfScopeAnnotationsConfigurer(acx.getEnvironment()));
	acx.addBeanFactoryPostProcessor(CdiScopeAnnotationsAutoConfiguration.cdiScopeAnnotationsConfigurer(acx.getEnvironment()));
	acx.refresh();

	assertThat(acx.getBeanDefinition("viewScopedClass").getScope())
		.isEqualTo(ViewScope.SCOPE_VIEW);
	assertThat(acx.getBeanDefinition("viewScopedBean").getScope())
		.isEqualTo(ViewScope.SCOPE_VIEW);
}
 
Example #20
Source File: InjectAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowiredFieldDoesNotResolveCandidateWithDefaultValueAndConflictingValueOnBeanDefinition() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	// qualifier added, and non-default value specified
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class, "not the default"));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	try {
		context.refresh();
		fail("expected BeanCreationException");
	}
	catch (BeanCreationException e) {
		assertTrue(e.getRootCause() instanceof NoSuchBeanDefinitionException);
		assertEquals("autowired", e.getBeanName());
	}
}
 
Example #21
Source File: AsyncExecutionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void asyncPrototypeClassListener() throws Exception {
	// Arrange
	originalThreadName = Thread.currentThread().getName();
	listenerCalled = 0;
	listenerConstructed = 0;
	GenericApplicationContext context = new GenericApplicationContext();
	RootBeanDefinition listenerDef = new RootBeanDefinition(AsyncClassListener.class);
	listenerDef.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	context.registerBeanDefinition("asyncTest", listenerDef);
	context.registerBeanDefinition("autoProxyCreator", new RootBeanDefinition(DefaultAdvisorAutoProxyCreator.class));
	context.registerBeanDefinition("asyncAdvisor", new RootBeanDefinition(AsyncAnnotationAdvisor.class));
	// Act
	context.refresh();
	context.close();
	// Assert
	Awaitility.await()
				.atMost(1, TimeUnit.SECONDS)
				.pollInterval(10, TimeUnit.MILLISECONDS)
				.until(() -> listenerCalled == 2);
	assertEquals(2, listenerConstructed);
}
 
Example #22
Source File: QualifierAnnotationAutowireContextTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void autowiredFieldResolvesWithBaseQualifierAndDefaultValue() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class));
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldWithBaseQualifierDefaultValueTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedFieldWithBaseQualifierDefaultValueTestBean bean =
			(QualifiedFieldWithBaseQualifierDefaultValueTestBean) context.getBean("autowired");
	assertEquals(MARK, bean.getPerson().getName());
}
 
Example #23
Source File: InjectAnnotationAutowireContextTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowiredFieldResolvesQualifiedCandidate() {
	GenericApplicationContext context = new GenericApplicationContext();
	ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
	cavs1.addGenericArgumentValue(JUERGEN);
	RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
	person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
	ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
	cavs2.addGenericArgumentValue(MARK);
	RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
	context.registerBeanDefinition(JUERGEN, person1);
	context.registerBeanDefinition(MARK, person2);
	context.registerBeanDefinition("autowired",
			new RootBeanDefinition(QualifiedFieldTestBean.class));
	AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
	context.refresh();
	QualifiedFieldTestBean bean = (QualifiedFieldTestBean) context.getBean("autowired");
	assertEquals(JUERGEN, bean.getPerson().getName());
}
 
Example #24
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPublicExtendedPersistenceContextSetter() throws Exception {
	EntityManager mockEm = mock(EntityManager.class);
	given(mockEmf.createEntityManager()).willReturn(mockEm);

	GenericApplicationContext gac = new GenericApplicationContext();
	gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
	gac.registerBeanDefinition("annotationProcessor",
			new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
	gac.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));
	gac.refresh();

	DefaultPublicPersistenceContextSetter bean = (DefaultPublicPersistenceContextSetter) gac.getBean(
			DefaultPublicPersistenceContextSetter.class.getName());
	assertNotNull(bean.em);
}
 
Example #25
Source File: AbstractMessageChannelBinder.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
private void destroyBean(String beanName) {
	if (getApplicationContext().containsBeanDefinition(beanName)) {
		((DefaultSingletonBeanRegistry) getApplicationContext().getBeanFactory())
				.destroySingleton(beanName);
		((GenericApplicationContext) getApplicationContext())
				.removeBeanDefinition(beanName);
	}
}
 
Example #26
Source File: BeanValidationPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotNullConstraint() {
	GenericApplicationContext ac = new GenericApplicationContext();
	ac.registerBeanDefinition("bvpp", new RootBeanDefinition(BeanValidationPostProcessor.class));
	ac.registerBeanDefinition("capp", new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class));
	ac.registerBeanDefinition("bean", new RootBeanDefinition(NotNullConstrainedBean.class));
	try {
		ac.refresh();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause().getMessage().contains("testBean"));
		assertTrue(ex.getRootCause().getMessage().contains("invalid"));
	}
}
 
Example #27
Source File: ComponentScanParserBeanDefinitionDefaultsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAutowireConstructor() {
	GenericApplicationContext context = new GenericApplicationContext();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
	reader.loadBeanDefinitions(LOCATION_PREFIX + "defaultAutowireConstructorTests.xml");
	context.refresh();
	DefaultsTestBean bean = (DefaultsTestBean) context.getBean(TEST_BEAN_NAME);
	assertNotNull("constructor dependency should have been autowired", bean.getConstructorDependency());
	assertEquals("cd", bean.getConstructorDependency().getName());
	assertNull("property dependencies should not have been autowired", bean.getPropertyDependency1());
	assertNull("property dependencies should not have been autowired", bean.getPropertyDependency2());
}
 
Example #28
Source File: AutowiredConfigurationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testAutowiredSingleConstructorSupported() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
			new ClassPathResource("annotation-config.xml", AutowiredConstructorConfig.class));
	GenericApplicationContext ctx = new GenericApplicationContext(factory);
	ctx.registerBeanDefinition("config1", new RootBeanDefinition(AutowiredConstructorConfig.class));
	ctx.registerBeanDefinition("config2", new RootBeanDefinition(ColorConfig.class));
	ctx.refresh();
	assertSame(ctx.getBean(AutowiredConstructorConfig.class).colour, ctx.getBean(Colour.class));
}
 
Example #29
Source File: CustomNamespaceHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	NamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(CLASS.getClassLoader(), NS_PROPS);
	this.beanFactory = new GenericApplicationContext();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
	reader.setNamespaceHandlerResolver(resolver);
	reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
	reader.setEntityResolver(new DummySchemaResolver());
	reader.loadBeanDefinitions(getResource());
	this.beanFactory.refresh();
}
 
Example #30
Source File: ConfigurationClassProcessingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test  // SPR-11830
public void configWithBeanWithProviderImplementation() {
	GenericApplicationContext ac = new GenericApplicationContext();
	AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
	ac.registerBeanDefinition("config", new RootBeanDefinition(ConfigWithBeanWithProviderImplementation.class));
	ac.refresh();
	assertSame(ac.getBean("customName"), ConfigWithBeanWithProviderImplementation.testBean);
}