private SpringContainedBean<?> createBean( Class<?> beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { try { if (lifecycleOptions.useJpaCompliantCreation()) { return new SpringContainedBean<>( this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false), this.beanFactory::destroyBean); } else { return new SpringContainedBean<>(this.beanFactory.getBean(beanType)); } } catch (BeansException ex) { if (logger.isDebugEnabled()) { logger.debug("Falling back to Hibernate's default producer after bean creation failure for " + beanType + ": " + ex); } return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(beanType)); } }
/** * Initialize the LifecycleProcessor. * Uses HeritrixLifecycleProcessor, which prevents an automatic lifecycle * start(), if none defined in the context. * @see org.springframework.context.support.DefaultLifecycleProcessor */ protected void initLifecycleProcessor() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (!beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) { HeritrixLifecycleProcessor obj = (HeritrixLifecycleProcessor)beanFactory.createBean(HeritrixLifecycleProcessor.class); beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME,obj); } super.initLifecycleProcessor(); }
private SpringContainedBean<?> createBean( Class<?> beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { try { if (lifecycleOptions.useJpaCompliantCreation()) { return new SpringContainedBean<>( this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false), this.beanFactory::destroyBean); } else { return new SpringContainedBean<>(this.beanFactory.getBean(beanType)); } } catch (BeansException ex) { if (logger.isDebugEnabled()) { logger.debug("Falling back to Hibernate's default producer after bean creation failure for " + beanType + ": " + ex); } return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(beanType)); } }
public <T> T newInstance(Class<T> type) { Object value = applicationContext.getBeanFactory().createBean(type, autowireMode, dependencyCheck); return type.cast(value); }
private <T> T defineBean(Class<T> type, String beanName) { T bean = this.factory.createBean(type); this.factory.registerSingleton(beanName, bean); return bean; }
/** * Register bean into application context. * * @param <T> the type parameter * @param applicationContext the application context * @param beanClazz the bean clazz * @param beanId the bean id * @return the type registered */ public static <T> T registerBeanIntoApplicationContext(final ConfigurableApplicationContext applicationContext, final Class<T> beanClazz, final String beanId) { val beanFactory = applicationContext.getBeanFactory(); val provider = beanFactory.createBean(beanClazz); beanFactory.initializeBean(provider, beanId); beanFactory.autowireBean(provider); beanFactory.registerSingleton(beanId, provider); return provider; }
@Override public Object getInstance() { return springContext.getBeanFactory().createBean(c, a.mode().getSpringCode(), a.dependencyCheck()); }
@Override public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) { try { String beanName = getBeanName(key); if (!beanFactory.isPrototype(beanName)) { String msg = "Bean [%s] must be of prototype scope."; throw new IllegalArgumentException(String.format(msg, beanName)); } return beanFactory.getBean(beanName, key); } catch (NoSuchElementException e) { // The factory does not know about the bean it creates it. return beanFactory.createBean(key); } }
@Override protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { if (baseContextConfiguration == null) { throw new IllegalStateException("Context configuration is not set. See ServerConfigurationAwareContextLoaderListener."); } super.prepareBeanFactory(beanFactory); // add auto-mock factory post processor addBeanFactoryPostProcessor(new BeanAutoMockPostProcessor(baseContextConfiguration)); // add the registry bean beanFactory.createBean(ServerExecutionRegistry.class); // add the snooper ((DefaultListableBeanFactory) beanFactory).registerBeanDefinition("TheSnooper", new RootBeanDefinition(SpringEventSnooper.class)); log.info("LOG00040: Added snooper bean definition"); }
/** * Initialize the LifecycleProcessor. * Uses HeritrixLifecycleProcessor, which prevents an automatic lifecycle * start(), if none defined in the context. * @see org.springframework.context.support.DefaultLifecycleProcessor */ protected void initLifecycleProcessor() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (!beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) { HeritrixLifecycleProcessor obj = (HeritrixLifecycleProcessor)beanFactory.createBean(HeritrixLifecycleProcessor.class); beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME,obj); } super.initLifecycleProcessor(); }
final Object result = springContext.getBeanFactory().createBean(clazz, autowire.mode().getSpringCode(), autowire.dependencyCheck()); return clazz.cast(result);
@Deprecated public AcrossModuleSettings getSettings() { try { LOG.warn( "The use of @AcrossCondition('settings') is deprecated - functionality will be removed in the future." ); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) getBeanFactory(); ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) getBeanFactory(); String[] beanNames = beanFactory.getBeanNamesForType( AcrossModuleSettings.class ); if ( beanNames.length >= 1 ) { AcrossModuleSettings settings = (AcrossModuleSettings) beanFactory.createBean( beanFactory.getType( beanNames[0] ) ); settings.setEnvironment( environment ); return settings; } } catch ( NoSuchBeanDefinitionException ignore ) { } return null; }
@Override protected Object createTest() throws Exception { return this.springTest.getContext().getBeanFactory().createBean(this.getTestClass().getJavaClass()); }
@Override protected Object createTest() throws Exception { return this.springTest.getContext().getBeanFactory().createBean(this.getTestClass().getJavaClass()); }
@Test @Ignore("To slow and doesn't have 100% confirmation") public void concurrency() throws InterruptedException, ExecutionException { final int concurrencyLevel = 8; final ExecutorService executorService = Executors.newFixedThreadPool(concurrencyLevel); try { for (int i = 0; i < 1000; ++i) { final ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class); try { final Callable<?> task = () -> context.getBeanFactory().createBean(BeanForConcurrencyTesting.class); final List<? extends Future<?>> futures = executorService .invokeAll(Collections.nCopies(concurrencyLevel, task)); for (Future<?> future : futures) { future.get(); } } finally { context.close(); } } } finally { executorService.shutdown(); } }
private Object registerResBean(Class clazz, String beanName, Plugin plugin, ClassLoader classLoader, CamelContext camelContext) throws Exception { //NOSONAR ClassLoader contextLoader = currentThread().getContextClassLoader(); currentThread().setContextClassLoader(classLoader); try { if (!(beanFactory instanceof ConfigurableListableBeanFactory) || !(beanFactory instanceof BeanDefinitionRegistry)) { throw new PluginsSystemException("Failed to initialize the resources beans: beanFactory must be " + "instance of BeanDefinitionRegistry and ConfigurableListableBeanFactory"); } final ConfigurableListableBeanFactory defaultBeanFactory = (ConfigurableListableBeanFactory) beanFactory; final BeanDefinitionRegistry beanReg = (BeanDefinitionRegistry) beanFactory; // replace old definitions with the new ones if (defaultBeanFactory.containsBeanDefinition(beanName)) { ((BeanDefinitionRegistry) defaultBeanFactory).removeBeanDefinition(beanName); } beanReg.registerBeanDefinition(beanName, new ClassLoaderBeanDefinition(clazz, classLoader)); final Object res = defaultBeanFactory.createBean(clazz, AUTOWIRE_BY_NAME, true); defaultBeanFactory.autowireBean(res); DefaultCamelBeanPostProcessor processor = new DefaultCamelBeanPostProcessor(camelContext); processor.postProcessBeforeInitialization(res, null); plugin.getContext().getInjector().inject(res, plugin.getContext(), null); defaultBeanFactory.registerSingleton(beanName, res); return res; } finally { currentThread().setContextClassLoader(contextLoader); } }
private SpringContainedBean<?> createBean( Class<?> beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { try { if (lifecycleOptions.useJpaCompliantCreation()) { return new SpringContainedBean<>( this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false), this.beanFactory::destroyBean); } else { return new SpringContainedBean<>(this.beanFactory.getBean(beanType)); } } catch (BeansException ex) { if (logger.isDebugEnabled()) { logger.debug("Falling back to Hibernate's default producer after bean creation failure for " + beanType + ": " + ex); } return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(beanType)); } }
@Test public void testGetInstanceNotInFactory() { String[] names = {}; when(beanFactory.getBeanNamesForType(validatorType)).thenReturn(names); when(beanFactory.createBean(validatorType)).thenReturn(validator); TestConstraintValidator aValidator = factory.getInstance(validatorType); assertEquals(aValidator, validator); }
public MiniumCucumber(Class<?> clazz) throws InitializationError, IOException { super(clazz); Assertions.assertNoCucumberAnnotatedMethods(clazz); for (String[] profiles : getProfilesMatrix().getMatrix()) { MiniumActiveProfilesResolver.setActiveProfiles(profiles); try { MiniumRhinoTestContextManager testContextManager = new MiniumRhinoTestContextManager(MiniumCucumberTest.class); MiniumProfileRunner profileRunner = testContextManager.getBeanFactory().createBean(MiniumProfileRunner.class); profileRunner.setTestContextManager(testContextManager); children.add(profileRunner); } finally { MiniumActiveProfilesResolver.setActiveProfiles(); } } }
log.debug("Creating element " + elementClass); Element element = (Element) beanFactory.createBean(elementClass, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);