Search in sources :

Example 1 with SimpleJndiBeanFactory

use of org.springframework.jndi.support.SimpleJndiBeanFactory in project spring-framework by spring-projects.

the class CommonAnnotationBeanPostProcessorTests method testResourceInjectionFromJndi.

@Test
public void testResourceInjectionFromJndi() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
    SimpleJndiBeanFactory resourceFactory = new SimpleJndiBeanFactory();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
    TestBean tb = new TestBean();
    jndiTemplate.addObject("java:comp/env/testBean", tb);
    TestBean tb2 = new TestBean();
    jndiTemplate.addObject("java:comp/env/testBean2", tb2);
    resourceFactory.setJndiTemplate(jndiTemplate);
    bpp.setResourceFactory(resourceFactory);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class));
    ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.initCalled).isTrue();
    assertThat(bean.init2Called).isTrue();
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb2);
    bf.destroySingletons();
    assertThat(bean.destroyCalled).isTrue();
    assertThat(bean.destroy2Called).isTrue();
}
Also used : NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) INestedTestBean(org.springframework.beans.testfixture.beans.INestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExpectedLookupTemplate(org.springframework.context.testfixture.jndi.ExpectedLookupTemplate) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) SimpleJndiBeanFactory(org.springframework.jndi.support.SimpleJndiBeanFactory) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 INestedTestBean (org.springframework.beans.testfixture.beans.INestedTestBean)1 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)1 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)1 SimpleJndiBeanFactory (org.springframework.jndi.support.SimpleJndiBeanFactory)1