Search in sources :

Example 71 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project geronimo-xbean by apache.

the class JndiTest method testConfigureJndiInsideSpringXml.

public void testConfigureJndiInsideSpringXml() throws Exception {
    // lets load a spring context
    BeanFactory factory = new ClassPathXmlApplicationContext("org/apache/xbean/spring/jndi/spring.xml");
    Object test = factory.getBean("restaurant");
    assertNotNull("Should have found the test object", test);
    Object jndi = factory.getBean("jndi");
    assertNotNull("Should have found the jndi object", jndi);
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, SpringInitialContextFactory.class.getName());
    InitialContext context = new InitialContext(env);
    assertEntryExists(context, "test");
    assertEntryExists(context, "test/restaurant");
    assertSame(test, context.lookup("test/restaurant"));
}
Also used : ClassPathXmlApplicationContext(org.apache.xbean.spring.context.ClassPathXmlApplicationContext) Hashtable(java.util.Hashtable) BeanFactory(org.springframework.beans.factory.BeanFactory) InitialContext(javax.naming.InitialContext)

Example 72 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project pentaho-platform by pentaho.

the class DefaultPluginManager method getNativeBeanFactory.

/**
 * The native bean factory is the bean factory that has had all of its bean definitions loaded natively. In other
 * words, the plugin manager will not add any further bean definitions (i.e. from a plugin.xml file) into this
 * factory. This factory represents the one responsible for holding bean definitions for plugin.spring.xml or, if in a
 * unit test environment, the unit test pre-loaded bean factory.
 *
 * @return a bean factory will preconfigured bean definitions or <code>null</code> if no bean definition source is
 * available
 */
protected BeanFactory getNativeBeanFactory(final IPlatformPlugin plugin, final ClassLoader loader) {
    BeanFactory nativeFactory = null;
    if (plugin.getBeanFactory() != null) {
        // then we are probably in a unit test so just use the preconfigured one
        BeanFactory testFactory = plugin.getBeanFactory();
        if (testFactory instanceof ConfigurableBeanFactory) {
            ((ConfigurableBeanFactory) testFactory).setBeanClassLoader(loader);
        } else {
            // $NON-NLS-1$
            logger.warn(Messages.getInstance().getString("PluginManager.WARN_WRONG_BEAN_FACTORY_TYPE"));
        }
        nativeFactory = testFactory;
    } else {
        // $NON-NLS-1$
        File f = new File(((PluginClassLoader) loader).getPluginDir(), "plugin.spring.xml");
        if (f.exists()) {
            // $NON-NLS-1$
            logger.debug("Found plugin spring file @ " + f.getAbsolutePath());
            FileSystemResource fsr = new FileSystemResource(f);
            GenericApplicationContext appCtx = new GenericApplicationContext() {

                @Override
                protected void prepareBeanFactory(ConfigurableListableBeanFactory clBeanFactory) {
                    super.prepareBeanFactory(clBeanFactory);
                    clBeanFactory.setBeanClassLoader(loader);
                }

                @Override
                public ClassLoader getClassLoader() {
                    return loader;
                }
            };
            XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appCtx);
            xmlReader.setBeanClassLoader(loader);
            xmlReader.loadBeanDefinitions(fsr);
            nativeFactory = appCtx;
        }
    }
    return nativeFactory;
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Example 73 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project pentaho-platform by pentaho.

the class PentahoSystemPluginManager method createBeanFactory.

private GenericApplicationContext createBeanFactory(IPlatformPlugin plugin, ClassLoader classloader) throws PlatformPluginRegistrationException {
    if (!(classloader instanceof PluginClassLoader)) {
        throw new PlatformPluginRegistrationException("Can't determine plugin dir to load spring file because classloader is not of type PluginClassLoader.  " + "This is since we are probably in a unit test");
    }
    // 
    // Get the native factory (the factory that comes preconfigured via either Spring bean files or via JUnit test
    // 
    BeanFactory nativeBeanFactory = getNativeBeanFactory(plugin, classloader);
    // 
    // Now create the definable factory for accepting old style bean definitions from IPluginProvider
    // 
    GenericApplicationContext beanFactory = null;
    if (nativeBeanFactory != null && nativeBeanFactory instanceof GenericApplicationContext) {
        beanFactory = (GenericApplicationContext) nativeBeanFactory;
    } else {
        beanFactory = new GenericApplicationContext();
        beanFactory.setClassLoader(classloader);
        beanFactory.getBeanFactory().setBeanClassLoader(classloader);
        if (nativeBeanFactory != null) {
            beanFactory.getBeanFactory().setParentBeanFactory(nativeBeanFactory);
        }
    }
    beanFactory.addBeanFactoryPostProcessor(new PentahoBeanScopeValidatorPostProcessor());
    // been made available to the plugin manager
    for (PluginBeanDefinition def : plugin.getBeans()) {
        // register by classname if id is null
        def.setBeanId((def.getBeanId() == null) ? def.getClassname() : def.getBeanId());
        try {
            assertUnique(beanFactory, plugin.getId(), def.getBeanId());
        } catch (PlatformPluginRegistrationException e) {
            logger.error(MessageFormat.format("Unable to register plugin bean, a bean by the id {0} is already defined in plugin: {1}", def.getBeanId(), plugin.getId()));
            continue;
        }
        // defining plugin beans the old way through the plugin provider ifc supports only prototype scope
        BeanDefinition beanDef = BeanDefinitionBuilder.rootBeanDefinition(def.getClassname()).setScope(BeanDefinition.SCOPE_PROTOTYPE).getBeanDefinition();
        beanFactory.registerBeanDefinition(def.getBeanId(), beanDef);
    }
    return beanFactory;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) PluginBeanDefinition(org.pentaho.platform.api.engine.PluginBeanDefinition) PentahoBeanScopeValidatorPostProcessor(org.pentaho.platform.engine.core.system.objfac.spring.PentahoBeanScopeValidatorPostProcessor) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ListableBeanFactory(org.springframework.beans.factory.ListableBeanFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) PluginBeanDefinition(org.pentaho.platform.api.engine.PluginBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) PlatformPluginRegistrationException(org.pentaho.platform.api.engine.PlatformPluginRegistrationException)

Example 74 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project spring-cloud-sleuth by spring-cloud.

the class TraceableExecutorServiceTests method should_propagate_trace_info_when_compleable_future_is_used.

@Test
public void should_propagate_trace_info_when_compleable_future_is_used() throws Exception {
    ExecutorService executorService = this.executorService;
    BeanFactory beanFactory = beanFactory();
    // tag::completablefuture[]
    CompletableFuture<Long> completableFuture = CompletableFuture.supplyAsync(() -> {
        // perform some logic
        return 1_000_000L;
    }, new TraceableExecutorService(beanFactory, executorService, // 'calculateTax' explicitly names the span - this param is optional
    "calculateTax"));
    // end::completablefuture[]
    then(completableFuture.get()).isEqualTo(1_000_000L);
    then(this.tracer.currentSpan()).isNull();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 75 with BeanFactory

use of org.springframework.beans.factory.BeanFactory in project com.revolsys.open by revolsys.

the class BeanReferenceListFactoryBean method createInstance.

@Override
protected List<T> createInstance() throws Exception {
    final BeanFactory beanFactory = getBeanFactory();
    final List<T> beans = new ArrayList<>();
    for (int i = 0; i < this.beanNames.size(); i++) {
        final String beanName = this.beanNames.get(i);
        final T bean = (T) beanFactory.getBean(beanName);
        beans.add(bean);
    }
    return beans;
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) ArrayList(java.util.ArrayList)

Aggregations

BeanFactory (org.springframework.beans.factory.BeanFactory)121 Test (org.junit.jupiter.api.Test)30 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)25 Test (org.junit.Test)20 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)16 ListableBeanFactory (org.springframework.beans.factory.ListableBeanFactory)15 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)12 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)11 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 CountDownLatch (java.util.concurrent.CountDownLatch)9 ExecutorService (java.util.concurrent.ExecutorService)8 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)8 AutowireCapableBeanFactory (org.springframework.beans.factory.config.AutowireCapableBeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 JmsTemplate (org.springframework.jms.core.JmsTemplate)6 Bucket4JAutoConfigurationServletFilter (com.giffing.bucket4j.spring.boot.starter.config.servlet.Bucket4JAutoConfigurationServletFilter)4 Bucket4JAutoConfigurationZuul (com.giffing.bucket4j.spring.boot.starter.config.zuul.Bucket4JAutoConfigurationZuul)4