Example usage for org.springframework.beans.factory ObjectFactory getObject

List of usage examples for org.springframework.beans.factory ObjectFactory getObject

Introduction

In this page you can find the example usage for org.springframework.beans.factory ObjectFactory getObject.

Prototype

T getObject() throws BeansException;

Source Link

Document

Return an instance (possibly shared or independent) of the object managed by this factory.

Usage

From source file:org.eclipse.gemini.blueprint.context.support.internal.scope.OsgiBundleScope.java

public Object get(String name, ObjectFactory<?> objectFactory) {
    // outside bundle calling (no need to cache things)
    if (isExternalBundleCalling()) {
        Object bean = objectFactory.getObject();

        return bean;
    }/*from   w w  w.java  2  s.c o m*/
    // in-appCtx call
    else {
        // use local bean repository
        // cannot use a concurrent map since we want to postpone the call to
        // getObject
        synchronized (localBeans) {
            Object bean = localBeans.get(name);
            if (bean == null) {
                bean = objectFactory.getObject();
                localBeans.put(name, bean);
            }
            return bean;
        }
    }

}

From source file:org.springframework.batch.core.partition.gemfire.RemoteScope.java

public Object get(String name, ObjectFactory<?> objectFactory) {
    if (cache.containsKey(name)) {
        return cache.get(name);
    }/*from   www .  j a va 2s  . c o  m*/
    Object value = objectFactory.getObject();
    cache.put(name, value);
    return value;
}

From source file:org.springframework.batch.core.scope.JobScope.java

/**
 * @see Scope#get(String, ObjectFactory)
 *//*from  w ww  . j a  v a  2 s . co m*/
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
    JobContext context = getContext();
    Object scopedObject = context.getAttribute(name);

    if (scopedObject == null) {

        synchronized (mutex) {
            scopedObject = context.getAttribute(name);
            if (scopedObject == null) {

                if (logger.isDebugEnabled()) {
                    logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name));
                }

                scopedObject = objectFactory.getObject();
                context.setAttribute(name, scopedObject);

            }

        }

    }
    return scopedObject;
}

From source file:org.springframework.batch.core.scope.StepScope.java

/**
 * @see Scope#get(String, ObjectFactory)
 *//*from  ww  w .ja  v  a  2  s.  c  o  m*/
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
    StepContext context = getContext();
    Object scopedObject = context.getAttribute(name);

    if (scopedObject == null) {

        synchronized (mutex) {
            scopedObject = context.getAttribute(name);
            if (scopedObject == null) {

                if (logger.isDebugEnabled()) {
                    logger.debug(String.format("Creating object in scope=%s, name=%s", this.getName(), name));
                }

                scopedObject = objectFactory.getObject();
                context.setAttribute(name, scopedObject);

            }

        }

    }
    return scopedObject;
}

From source file:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.java

/**
 * Return the (raw) singleton object registered under the given name.
 * <p>Checks already instantiated singletons and also allows for an early
 * reference to a currently created singleton (resolving a circular reference).
 * @param beanName the name of the bean to look for
 * @param allowEarlyReference whether early references should be created or not
 * @return the registered singleton object, or {@code null} if none found
 *//*from www. jav a2s.  com*/
@Nullable
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
    Object singletonObject = this.singletonObjects.get(beanName);
    if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
        synchronized (this.singletonObjects) {
            singletonObject = this.earlySingletonObjects.get(beanName);
            if (singletonObject == null && allowEarlyReference) {
                ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                if (singletonFactory != null) {
                    singletonObject = singletonFactory.getObject();
                    this.earlySingletonObjects.put(beanName, singletonObject);
                    this.singletonFactories.remove(beanName);
                }
            }
        }
    }
    return singletonObject;
}

From source file:org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.java

/**
 * Return the (raw) singleton object registered under the given name,
 * creating and registering a new one if none registered yet.
 * @param beanName the name of the bean/*from w w  w  . j av a  2  s  .c om*/
 * @param singletonFactory the ObjectFactory to lazily create the singleton
 * with, if necessary
 * @return the registered singleton object
 */
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
    Assert.notNull(beanName, "Bean name must not be null");
    synchronized (this.singletonObjects) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null) {
            if (this.singletonsCurrentlyInDestruction) {
                throw new BeanCreationNotAllowedException(beanName,
                        "Singleton bean creation not allowed while singletons of this factory are in destruction "
                                + "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
            }
            beforeSingletonCreation(beanName);
            boolean newSingleton = false;
            boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
            if (recordSuppressedExceptions) {
                this.suppressedExceptions = new LinkedHashSet<>();
            }
            try {
                singletonObject = singletonFactory.getObject();
                newSingleton = true;
            } catch (IllegalStateException ex) {
                // Has the singleton object implicitly appeared in the meantime ->
                // if yes, proceed with it since the exception indicates that state.
                singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {
                    throw ex;
                }
            } catch (BeanCreationException ex) {
                if (recordSuppressedExceptions) {
                    for (Exception suppressedException : this.suppressedExceptions) {
                        ex.addRelatedCause(suppressedException);
                    }
                }
                throw ex;
            } finally {
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = null;
                }
                afterSingletonCreation(beanName);
            }
            if (newSingleton) {
                addSingleton(beanName, singletonObject);
            }
        }
        return singletonObject;
    }
}

From source file:org.springframework.boot.devtools.restart.Restarter.java

public Object getOrAddAttribute(final String name, final ObjectFactory<?> objectFactory) {
    synchronized (this.attributes) {
        if (!this.attributes.containsKey(name)) {
            this.attributes.put(name, objectFactory.getObject());
        }/*from ww  w . java 2s  .co m*/
        return this.attributes.get(name);
    }
}

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void genericApplicationContext() throws Exception {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);

    ac.getBeanFactory().registerScope("myScope", new Scope() {
        @Override//  w  w w.  j av  a 2s  . c  o  m
        public Object get(String name, ObjectFactory<?> objectFactory) {
            return objectFactory.getObject();
        }

        @Override
        public Object remove(String name) {
            return null;
        }

        @Override
        public void registerDestructionCallback(String name, Runnable callback) {
        }

        @Override
        public Object resolveContextualObject(String key) {
            if (key.equals("mySpecialAttr")) {
                return "42";
            } else {
                return null;
            }
        }

        @Override
        public String getConversationId() {
            return null;
        }
    });

    PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
    Properties placeholders = new Properties();
    placeholders.setProperty("code", "123");
    ppc.setProperties(placeholders);
    ac.addBeanFactoryPostProcessor(ppc);

    GenericBeanDefinition bd0 = new GenericBeanDefinition();
    bd0.setBeanClass(TestBean.class);
    bd0.getPropertyValues().add("name", "myName");
    bd0.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "original"));
    ac.registerBeanDefinition("tb0", bd0);

    GenericBeanDefinition bd1 = new GenericBeanDefinition();
    bd1.setBeanClassName("#{tb0.class}");
    bd1.setScope("myScope");
    bd1.getConstructorArgumentValues().addGenericArgumentValue("XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ");
    bd1.getConstructorArgumentValues().addGenericArgumentValue("#{mySpecialAttr}");
    ac.registerBeanDefinition("tb1", bd1);

    GenericBeanDefinition bd2 = new GenericBeanDefinition();
    bd2.setBeanClassName("#{tb1.class.name}");
    bd2.setScope("myScope");
    bd2.getPropertyValues().add("name", "{ XXX#{tb0.name}YYY#{mySpecialAttr}ZZZ }");
    bd2.getPropertyValues().add("age", "#{mySpecialAttr}");
    bd2.getPropertyValues().add("country", "${code} #{systemProperties.country}");
    ac.registerBeanDefinition("tb2", bd2);

    GenericBeanDefinition bd3 = new GenericBeanDefinition();
    bd3.setBeanClass(ValueTestBean.class);
    bd3.setScope("myScope");
    ac.registerBeanDefinition("tb3", bd3);

    GenericBeanDefinition bd4 = new GenericBeanDefinition();
    bd4.setBeanClass(ConstructorValueTestBean.class);
    bd4.setScope("myScope");
    ac.registerBeanDefinition("tb4", bd4);

    GenericBeanDefinition bd5 = new GenericBeanDefinition();
    bd5.setBeanClass(MethodValueTestBean.class);
    bd5.setScope("myScope");
    ac.registerBeanDefinition("tb5", bd5);

    GenericBeanDefinition bd6 = new GenericBeanDefinition();
    bd6.setBeanClass(PropertyValueTestBean.class);
    bd6.setScope("myScope");
    ac.registerBeanDefinition("tb6", bd6);

    System.getProperties().put("country", "UK");
    try {
        ac.refresh();

        TestBean tb0 = ac.getBean("tb0", TestBean.class);

        TestBean tb1 = ac.getBean("tb1", TestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb1.getName());
        assertEquals(42, tb1.getAge());

        TestBean tb2 = ac.getBean("tb2", TestBean.class);
        assertEquals("{ XXXmyNameYYY42ZZZ }", tb2.getName());
        assertEquals(42, tb2.getAge());
        assertEquals("123 UK", tb2.getCountry());

        ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
        assertEquals(42, tb3.age);
        assertEquals(42, tb3.ageFactory.getObject().intValue());
        assertEquals("123 UK", tb3.country);
        assertEquals("123 UK", tb3.countryFactory.getObject());
        System.getProperties().put("country", "US");
        assertEquals("123 UK", tb3.country);
        assertEquals("123 US", tb3.countryFactory.getObject());
        System.getProperties().put("country", "UK");
        assertEquals("123 UK", tb3.country);
        assertEquals("123 UK", tb3.countryFactory.getObject());
        assertSame(tb0, tb3.tb);

        tb3 = (ValueTestBean) SerializationTestUtils.serializeAndDeserialize(tb3);
        assertEquals("123 UK", tb3.countryFactory.getObject());

        ConstructorValueTestBean tb4 = ac.getBean("tb4", ConstructorValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb4.name);
        assertEquals(42, tb4.age);
        assertEquals("123 UK", tb4.country);
        assertSame(tb0, tb4.tb);

        MethodValueTestBean tb5 = ac.getBean("tb5", MethodValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb5.name);
        assertEquals(42, tb5.age);
        assertEquals("123 UK", tb5.country);
        assertSame(tb0, tb5.tb);

        PropertyValueTestBean tb6 = ac.getBean("tb6", PropertyValueTestBean.class);
        assertEquals("XXXmyNameYYY42ZZZ", tb6.name);
        assertEquals(42, tb6.age);
        assertEquals("123 UK", tb6.country);
        assertSame(tb0, tb6.tb);
    } finally {
        System.getProperties().remove("country");
    }
}

From source file:org.springframework.context.support.SimpleThreadScope.java

@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
    Map<String, Object> scope = this.threadScope.get();
    Object scopedObject = scope.get(name);
    if (scopedObject == null) {
        scopedObject = objectFactory.getObject();
        scope.put(name, scopedObject);//from  w  ww.j a v a 2 s  .  c o m
    }
    return scopedObject;
}

From source file:org.springframework.faces.mvc.scope.AbstractFacesScope.java

public Object get(String name, ObjectFactory objectFactory) {
    MutableAttributeMap scope = getScope();
    Object scopedObject = scope.get(name);
    if (scopedObject == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("No scoped instance '" + name + "' found; creating new instance");
        }/*  w ww  .java2 s . co  m*/
        scopedObject = objectFactory.getObject();
        scope.put(name, scopedObject);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Returning scoped instance '" + name + "'");
        }
    }
    return scopedObject;
}