private void bootstrap(Class<?> configurationClass) { context = new AnnotationConfigApplicationContext(configurationClass); context.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); }
@Test public void testInjectionIntoExistingObjects() { 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.refresh(); DefaultPrivatePersistenceContextField existingBean1 = new DefaultPrivatePersistenceContextField(); gac.getAutowireCapableBeanFactory().autowireBean(existingBean1); assertNotNull(existingBean1.em); DefaultPublicPersistenceContextSetter existingBean2 = new DefaultPublicPersistenceContextSetter(); gac.getAutowireCapableBeanFactory().autowireBean(existingBean2); assertNotNull(existingBean2.em); }
@Test public void accessAfterClosing() { GenericApplicationContext ac = new GenericApplicationContext(); ac.registerBeanDefinition("testBean", new RootBeanDefinition(String.class)); ac.refresh(); assertSame(ac.getBean("testBean"), ac.getBean(String.class)); assertSame(ac.getAutowireCapableBeanFactory().getBean("testBean"), ac.getAutowireCapableBeanFactory().getBean(String.class)); ac.close(); try { assertSame(ac.getBean("testBean"), ac.getBean(String.class)); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } try { assertSame(ac.getAutowireCapableBeanFactory().getBean("testBean"), ac.getAutowireCapableBeanFactory().getBean(String.class)); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
private Object handler(GenericApplicationContext generic, Object handler, Class<?> type) { if (handler == null) { handler = generic.getAutowireCapableBeanFactory().createBean(type); } return handler; }
private Object handler(GenericApplicationContext generic, Object handler, Class<?> type) { if (handler == null) { handler = generic.getAutowireCapableBeanFactory().createBean(type); } return handler; }
public AutowireCapableBeanFactory getAutowirer() { return getContext().getAutowireCapableBeanFactory(); }
public AutowireCapableBeanFactory getAutowirer() { return getContext().getAutowireCapableBeanFactory(); }
@Override public void registerSingleton(String beanName, Object bean, Class<?> type) { log.debug("registering bean '{}' of type '{}'", beanName, type.getName()); context.getAutowireCapableBeanFactory().autowireBean(bean); context.getBeanFactory().initializeBean(bean, beanName); context.getBeanFactory().registerSingleton(beanName, bean); // beans that get pushed straight into the context need to be attached to destructive bean post processors context.getDefaultListableBeanFactory().registerDisposableBean( beanName, new DisposableBeanAdapter(bean, beanName, context)); }
@Override public void prepareTest(Object testInstance) { try { context.getAutowireCapableBeanFactory().autowireBean(testInstance); } catch (BeansException e) { Throwable cause = e.getMostSpecificCause(); if (cause instanceof NoSuchBeanDefinitionException) { NoSuchBeanDefinitionException n = (NoSuchBeanDefinitionException) cause; throw new MissingBeanException(n, testInstance, n.getBeanType()); } if (cause instanceof PermanentException) throw (PermanentException) cause; throw new TestInjectionFailure(e, testInstance.getClass()); } }
context.registerBean(FunctionRegistrationPostProcessor.class, () -> new FunctionRegistrationPostProcessor( context.getAutowireCapableBeanFactory() .getBeanProvider(FunctionRegistration.class)));
context.registerBean(FunctionRegistrationPostProcessor.class, () -> new FunctionRegistrationPostProcessor( context.getAutowireCapableBeanFactory() .getBeanProvider(FunctionRegistration.class)));