Example usage for org.hibernate.resource.beans.container.spi BeanContainer getBean

List of usage examples for org.hibernate.resource.beans.container.spi BeanContainer getBean

Introduction

In this page you can find the example usage for org.hibernate.resource.beans.container.spi BeanContainer getBean.

Prototype

<B> ContainedBean<B> getBean(Class<B> beanType, LifecycleOptions lifecycleOptions,
            BeanInstanceProducer fallbackProducer);

Source Link

Usage

From source file:org.springframework.orm.jpa.hibernate.HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTests.java

License:Apache License

@Test
public void testCanRetrieveBeanByTypeWithJpaCompliantOptions() {
    BeanContainer beanContainer = getBeanContainer();
    assertNotNull(beanContainer);/*from  www.  ja v a 2 s.c om*/

    ContainedBean<SinglePrototypeInSpringContextTestBean> bean = beanContainer.getBean(
            SinglePrototypeInSpringContextTestBean.class, JpaLifecycleOptions.INSTANCE,
            IneffectiveBeanInstanceProducer.INSTANCE);

    assertNotNull(bean);
    SinglePrototypeInSpringContextTestBean instance = bean.getBeanInstance();
    assertNotNull(instance);
    assertSame(applicationContext, instance.getApplicationContext());
}

From source file:org.springframework.orm.jpa.hibernate.HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTests.java

License:Apache License

@Test
public void testCanRetrieveBeanByTypeWithNativeOptions() {
    BeanContainer beanContainer = getBeanContainer();
    assertNotNull(beanContainer);/*from   w  ww  .  j  a  va2s.  c o  m*/

    ContainedBean<SinglePrototypeInSpringContextTestBean> bean = beanContainer.getBean(
            SinglePrototypeInSpringContextTestBean.class, NativeLifecycleOptions.INSTANCE,
            IneffectiveBeanInstanceProducer.INSTANCE);

    assertNotNull(bean);
    SinglePrototypeInSpringContextTestBean instance = bean.getBeanInstance();
    assertNotNull(instance);
    assertEquals("single", instance.getName());
    assertSame(applicationContext, instance.getApplicationContext());

    ContainedBean<SinglePrototypeInSpringContextTestBean> bean2 = beanContainer.getBean(
            SinglePrototypeInSpringContextTestBean.class, NativeLifecycleOptions.INSTANCE,
            IneffectiveBeanInstanceProducer.INSTANCE);

    assertNotNull(bean2);
    SinglePrototypeInSpringContextTestBean instance2 = bean2.getBeanInstance();
    assertNotNull(instance2);
    // Due to the lifecycle options, and because the bean has the "prototype" scope, we should not return the same instance
    assertNotSame(instance, instance2);
}

From source file:org.springframework.orm.jpa.hibernate.HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTests.java

License:Apache License

@Test
public void testCanRetrieveFallbackBeanByTypeWithJpaCompliantOptions() {
    BeanContainer beanContainer = getBeanContainer();
    assertNotNull(beanContainer);//  w w w .  j  a  v  a 2  s . c  om
    NoDefinitionInSpringContextTestBeanInstanceProducer fallbackProducer = new NoDefinitionInSpringContextTestBeanInstanceProducer();

    ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer
            .getBean(NoDefinitionInSpringContextTestBean.class, JpaLifecycleOptions.INSTANCE, fallbackProducer);

    assertEquals(1, fallbackProducer.currentUnnamedInstantiationCount());
    assertEquals(0, fallbackProducer.currentNamedInstantiationCount());

    assertNotNull(bean);
    NoDefinitionInSpringContextTestBean instance = bean.getBeanInstance();
    assertNotNull(instance);
    assertEquals(BeanSource.FALLBACK, instance.getSource());
    assertNull(instance.getApplicationContext());
}

From source file:org.springframework.orm.jpa.hibernate.HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTests.java

License:Apache License

@Test
public void testCanRetrieveFallbackBeanByTypeWithNativeOptions() {
    BeanContainer beanContainer = getBeanContainer();
    assertNotNull(beanContainer);//from   www. ja v  a 2  s .c o  m
    NoDefinitionInSpringContextTestBeanInstanceProducer fallbackProducer = new NoDefinitionInSpringContextTestBeanInstanceProducer();

    ContainedBean<NoDefinitionInSpringContextTestBean> bean = beanContainer.getBean(
            NoDefinitionInSpringContextTestBean.class, NativeLifecycleOptions.INSTANCE, fallbackProducer);

    assertEquals(1, fallbackProducer.currentUnnamedInstantiationCount());
    assertEquals(0, fallbackProducer.currentNamedInstantiationCount());

    assertNotNull(bean);
    NoDefinitionInSpringContextTestBean instance = bean.getBeanInstance();
    assertNotNull(instance);
    assertEquals(BeanSource.FALLBACK, instance.getSource());
    assertNull(instance.getApplicationContext());
}