Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#addBeanPostProcessor()

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#addBeanPostProcessor() . 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: InjectAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testResourceInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(ResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);

	ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());

	bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
}
 
Example 2
Source File: RequiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithAllRequiredPropertiesSpecified() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	BeanDefinition beanDef = BeanDefinitionBuilder
		.genericBeanDefinition(RequiredTestBean.class)
		.addPropertyValue("age", "24")
		.addPropertyValue("favouriteColour", "Blue")
		.addPropertyValue("jobTitle", "Grand Poobah")
		.getBeanDefinition();
	factory.registerBeanDefinition("testBean", beanDef);
	factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
	factory.preInstantiateSingletons();
	RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
	assertEquals(24, bean.getAge());
	assertEquals("Blue", bean.getFavouriteColour());
}
 
Example 3
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testLazyOptionalResourceInjectionWithNonExistingTarget() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(OptionalFieldResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	OptionalFieldResourceInjectionBean bean = (OptionalFieldResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotNull(bean.getTestBean());
	assertNotNull(bean.getTestBeans());
	assertTrue(bean.getTestBeans().isEmpty());
	try {
		bean.getTestBean().getName();
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example 4
Source File: RequiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	BeanDefinition beanDef = BeanDefinitionBuilder
			.genericBeanDefinition(RequiredTestBean.class)
			.setFactoryMethod("create")
			.addPropertyValue("age", "24")
			.addPropertyValue("favouriteColour", "Blue")
			.addPropertyValue("jobTitle", "Grand Poobah")
			.getBeanDefinition();
	factory.registerBeanDefinition("testBean", beanDef);
	factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
	factory.preInstantiateSingletons();
	RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
	assertEquals(24, bean.getAge());
	assertEquals("Blue", bean.getFavouriteColour());
}
 
Example 5
Source File: InjectAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testObjectFactoryInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class));
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
	bf.registerBeanDefinition("testBean", bd);
	bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));

	ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
	assertSame(bf.getBean("testBean"), bean.getTestBean());
	bf.destroySingletons();
}
 
Example 6
Source File: RequiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	BeanDefinition beanDef = BeanDefinitionBuilder
			.genericBeanDefinition(RequiredTestBean.class)
			.setFactoryMethod("create")
			.addPropertyValue("age", "24")
			.addPropertyValue("favouriteColour", "Blue")
			.addPropertyValue("jobTitle", "Grand Poobah")
			.getBeanDefinition();
	factory.registerBeanDefinition("testBean", beanDef);
	factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
	factory.preInstantiateSingletons();
	RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
	assertEquals(24, bean.getAge());
	assertEquals("Blue", bean.getFavouriteColour());
}
 
Example 7
Source File: RequiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithThreeRequiredPropertiesOmitted() {
	try {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		BeanDefinition beanDef = BeanDefinitionBuilder
			.genericBeanDefinition(RequiredTestBean.class)
			.addPropertyValue("name", "Rob Harrop")
			.getBeanDefinition();
		factory.registerBeanDefinition("testBean", beanDef);
		factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
		factory.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		String message = ex.getCause().getMessage();
		assertTrue(message.contains("Properties"));
		assertTrue(message.contains("age"));
		assertTrue(message.contains("favouriteColour"));
		assertTrue(message.contains("jobTitle"));
		assertTrue(message.contains("testBean"));
	}
}
 
Example 8
Source File: InjectAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testProviderOfOptionalMethodInjectionWithBeanNotAvailable() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ProviderOfOptionalMethodInjectionBean.class));

	ProviderOfOptionalMethodInjectionBean bean = (ProviderOfOptionalMethodInjectionBean) bf.getBean("annotatedBean");
	assertFalse(bean.getTestBean().isPresent());
	bf.destroySingletons();
}
 
Example 9
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPostConstructAndPreDestroy() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.addBeanPostProcessor(new CommonAnnotationBeanPostProcessor());
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedInitDestroyBean.class));

	AnnotatedInitDestroyBean bean = (AnnotatedInitDestroyBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
}
 
Example 10
Source File: SpringHandlerInstantiatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("capitalizer", new RootBeanDefinition(Capitalizer.class));
	instantiator = new SpringHandlerInstantiator(bf);
	objectMapper = Jackson2ObjectMapperBuilder.json().handlerInstantiator(instantiator).build();
}
 
Example 11
Source File: InjectAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {
	bf = new DefaultListableBeanFactory();
	bf.registerResolvableDependency(BeanFactory.class, bf);
	bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
}
 
Example 12
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericsBasedFieldInjectionWithSimpleMatch() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);

	bf.registerSingleton("repo", new StringRepository());

	RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean");
	Repository repo = bf.getBean("repo", Repository.class);
	assertSame(repo, bean.repository);
	assertSame(repo, bean.stringRepository);
	assertSame(1, bean.repositoryArray.length);
	assertSame(1, bean.stringRepositoryArray.length);
	assertSame(repo, bean.repositoryArray[0]);
	assertSame(repo, bean.stringRepositoryArray[0]);
	assertSame(1, bean.repositoryList.size());
	assertSame(1, bean.stringRepositoryList.size());
	assertSame(repo, bean.repositoryList.get(0));
	assertSame(repo, bean.stringRepositoryList.get(0));
	assertSame(1, bean.repositoryMap.size());
	assertSame(1, bean.stringRepositoryMap.size());
	assertSame(repo, bean.repositoryMap.get("repo"));
	assertSame(repo, bean.stringRepositoryMap.get("repo"));
}
 
Example 13
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testSinglePersistenceContextFromJndi() {
	EntityManager mockEm = mock(EntityManager.class);
	EntityManager mockEm2 = mock(EntityManager.class);

	Map<String, String> persistenceContexts = new HashMap<>();
	persistenceContexts.put("System", "pc1");
	Map<String, String> extendedPersistenceContexts = new HashMap<>();
	extendedPersistenceContexts .put("System", "pc2");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pc1", mockEm);
	jt.addObject("java:comp/env/pc2", mockEm2);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceContexts(persistenceContexts);
	bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPublicPersistenceContextSetter bean2 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEm, bean1.em);
	assertSame(mockEm2, bean2.em);
}
 
Example 14
Source File: LookupAnnotationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	beanFactory = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor aabpp = new AutowiredAnnotationBeanPostProcessor();
	aabpp.setBeanFactory(beanFactory);
	beanFactory.addBeanPostProcessor(aabpp);
	beanFactory.registerBeanDefinition("abstractBean", new RootBeanDefinition(AbstractBean.class));
	RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
	tbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	beanFactory.registerBeanDefinition("testBean", tbd);
}
 
Example 15
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testResourceInjectionWithPrototypes() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setResourceFactory(bf);
	bf.addBeanPostProcessor(bpp);
	RootBeanDefinition abd = new RootBeanDefinition(ResourceInjectionBean.class);
	abd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd1 = new RootBeanDefinition(TestBean.class);
	tbd1.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean", tbd1);
	RootBeanDefinition tbd2 = new RootBeanDefinition(TestBean.class);
	tbd2.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("testBean2", tbd2);

	ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertTrue(bean.init3Called);

	TestBean tb = bean.getTestBean();
	TestBean tb2 = bean.getTestBean2();
	assertNotNull(tb);
	assertNotNull(tb2);

	ResourceInjectionBean anotherBean = (ResourceInjectionBean) bf.getBean("annotatedBean");
	assertNotSame(anotherBean, bean);
	assertNotSame(anotherBean.getTestBean(), tb);
	assertNotSame(anotherBean.getTestBean2(), tb2);

	bf.destroyBean("annotatedBean", bean);
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
	assertTrue(bean.destroy3Called);
}
 
Example 16
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtendedEjbInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerResolvableDependency(BeanFactory.class, bf);

	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ExtendedEjbInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	TestBean tb2 = new TestBean();
	bf.registerSingleton("testBean2", tb2);
	TestBean tb3 = new TestBean();
	bf.registerSingleton("testBean3", tb3);
	TestBean tb4 = new TestBean();
	bf.registerSingleton("testBean4", tb4);
	NestedTestBean tb6 = new NestedTestBean();
	bf.registerSingleton("xy", tb6);
	bf.registerAlias("xy", "testBean9");

	ExtendedEjbInjectionBean bean = (ExtendedEjbInjectionBean) bf.getBean("annotatedBean");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	assertSame(tb4, bean.getTestBean3());
	assertSame(tb3, bean.getTestBean4());
	assertSame(tb6, bean.testBean5);
	assertSame(tb6, bean.testBean6);
	assertSame(bf, bean.beanFactory);

	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 17
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncompleteBeanDefinition() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("testBean", new GenericBeanDefinition());
	try {
		bf.getBean("testBean");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getRootCause() instanceof IllegalStateException);
	}
}
 
Example 18
Source File: RequiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testWithFactoryBean() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	RootBeanDefinition beanDef = new RootBeanDefinition(RequiredTestBean.class);
	beanDef.setFactoryBeanName("testBeanFactory");
	beanDef.setFactoryMethodName("create");
	factory.registerBeanDefinition("testBean", beanDef);
	factory.registerBeanDefinition("testBeanFactory", new RootBeanDefinition(RequiredTestBeanFactory.class));
	RequiredAnnotationBeanPostProcessor bpp = new RequiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(factory);
	factory.addBeanPostProcessor(bpp);
	factory.preInstantiateSingletons();
}
 
Example 19
Source File: InjectAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructorResourceInjectionWithMultipleCandidatesAndFallback() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);

	ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean3());
	assertNull(bean.getTestBean4());
	bf.destroySingletons();
}
 
Example 20
Source File: AbstractSpringManagedBeanFactory.java    From dolphin-platform with Apache License 2.0 4 votes vote down vote up
protected void init() {
    ApplicationContext context = getContext();
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getAutowireCapableBeanFactory();
    beanFactory.addBeanPostProcessor(SpringPreInjector.getInstance());
}