Example usage for org.springframework.beans.factory.config ConfigurableBeanFactory SCOPE_PROTOTYPE

List of usage examples for org.springframework.beans.factory.config ConfigurableBeanFactory SCOPE_PROTOTYPE

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableBeanFactory SCOPE_PROTOTYPE.

Prototype

String SCOPE_PROTOTYPE

To view the source code for org.springframework.beans.factory.config ConfigurableBeanFactory SCOPE_PROTOTYPE.

Click Source Link

Document

Scope identifier for the standard prototype scope: "prototype".

Usage

From source file:me.springframework.di.spring.SpringConfigurationLoader.java

/**
 * Loads a {@link MutableInstance} from one of the {@link BeanDefinition}s
 * provided by the {@link BeanDefinitionRegistry} passed in.
 * //from  www  .  j  ava 2 s .  c o  m
 * @param instance
 *            A {@link MutableInstance} to be populated.
 * @param definition
 *            A {@link BeanDefinition}, providing the meta data.
 */
private static void load(MutableInstance instance, BeanDefinition definition, MutableContext context) {
    instance.setReferencedType(definition.getBeanClassName());
    instance.setPrimitive(false);
    instance.setLazyInit(definition.isLazyInit());
    instance.setId("source" + counter++);
    instance.setFactoryMethod(definition.getFactoryMethodName());
    instance.setFactoryInstance(definition.getFactoryBeanName());
    if (ConfigurableBeanFactory.SCOPE_SINGLETON.equals(definition.getScope())) {
        instance.setScope(Scope.SINGLETON);
    }
    if (ConfigurableBeanFactory.SCOPE_PROTOTYPE.equals(definition.getScope())) {
        instance.setScope(Scope.PROTOTYPE);
    }
    if (definition instanceof AbstractBeanDefinition) {
        instance.setInitMethod(((AbstractBeanDefinition) definition).getInitMethodName());
        instance.setDestroyMethod(((AbstractBeanDefinition) definition).getDestroyMethodName());
    }
    if (!definition.getConstructorArgumentValues().isEmpty()) {
        List<MutableConstructorArgument> arguments = new ArrayList<MutableConstructorArgument>();
        for (Object object : definition.getConstructorArgumentValues().getGenericArgumentValues()) {
            MutableConstructorArgument argument = new MutableConstructorArgument(instance);
            argument.setInstance(instance);
            ValueHolder holder = (ValueHolder) object;
            argument.setSource(loadSource(context, argument, holder.getValue()));
            argument.setType(holder.getType());
            arguments.add(argument);
        }
        instance.setConstructorArguments(arguments);
    }
    Set<MutablePropertySetter> setters = new HashSet<MutablePropertySetter>();
    for (Object object : definition.getPropertyValues().getPropertyValueList()) {
        MutablePropertySetter setter = new MutablePropertySetter(instance);
        setter.setInstance(instance);
        PropertyValue value = (PropertyValue) object;
        setter.setName(value.getName());
        setter.setSource(loadSource(context, setter, value.getValue()));
        setters.add(setter);
    }
    instance.setSetters(setters);

    // added by woj
    instance.setAutowireCandidate(definition.isAutowireCandidate());
    if (definition instanceof AbstractBeanDefinition) {
        instance.setAutowireMode(((AbstractBeanDefinition) definition).getResolvedAutowireMode());
    } else {
        instance.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_NO);
    }
}

From source file:com.logsniffer.app.ElasticSearchAppConfig.java

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Autowired//from www  . ja  va2s .c  om
public ElasticClientTemplate clientTemplate(final EsSettingsHolder settingsHolder) {
    return new ElasticClientTemplate() {
        @Override
        public <T> T executeWithClient(final ClientCallback<T> callback) {
            final Client c = getClientConnection(settingsHolder.getSettings()).getClient();
            return callback.execute(c);
        }
    };
}

From source file:org.springframework.beans.factory.DefaultListableBeanFactoryTests.java

@SuppressWarnings("unchecked")
@Test//from ww  w  . j  a va  2  s .  c o m
public void testInitSecurityAwarePrototypeBean() {
    final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class);
    bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
    bd.setInitMethodName("init");
    lbf.registerBeanDefinition("test", bd);
    final Subject subject = new Subject();
    subject.getPrincipals().add(new TestPrincipal("user1"));

    TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject, new PrivilegedAction() {
        @Override
        public Object run() {
            return lbf.getBean("test");
        }
    }, null);
    assertNotNull(bean);
    assertEquals("user1", bean.getUserName());
}