Example usage for org.springframework.beans BeanUtils instantiate

List of usage examples for org.springframework.beans BeanUtils instantiate

Introduction

In this page you can find the example usage for org.springframework.beans BeanUtils instantiate.

Prototype

@Deprecated
public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException 

Source Link

Document

Convenience method to instantiate a class using its no-arg constructor.

Usage

From source file:org.shept.org.springframework.web.servlet.mvc.support.ModelUtils.java

/**
 * //  w  w  w.java  2 s  .  co m
 * @return the newModelTemplate if any
 */
public static ModelCreation getNewModelTemplate(Class<?> clazz, Object model, String initString) {
    if (clazz == null)
        return null;
    Object entity = BeanUtils.instantiate(clazz);
    if (!(entity instanceof ModelCreation)) {
        logger.warn("The entity model '" + entity.getClass()
                + "' does not implement the interface 'ModelCreation' itself and /or "
                + " the return value of all of its '#initialize()' - methods. "
                + " No new row objects will be created.");
        return null;
    }
    if (StringUtils.hasText(initString)) {
        initialize(entity, initString);
    }
    if (model != null) {
        initialize(entity, model);
    }
    return (ModelCreation) entity;
}

From source file:org.springframework.boot.bind.PropertiesConfigurationFactory.java

/**
 * Create a new {@link PropertiesConfigurationFactory} instance.
 * @param type the target type//from w  ww.  j a  va 2  s  . co m
 * @see #PropertiesConfigurationFactory(Class)
 */
@SuppressWarnings("unchecked")
public PropertiesConfigurationFactory(Class<?> type) {
    Assert.notNull(type);
    this.target = (T) BeanUtils.instantiate(type);
}

From source file:org.springframework.boot.SpringApplication.java

/**
 * Strategy method used to create the {@link ApplicationContext}. By default this
 * method will respect any explicitly set application context or application context
 * class before falling back to a suitable default.
 * @return the application context (not yet refreshed)
 * @see #setApplicationContextClass(Class)
 *//*from ww w.j  av  a 2 s.  c  om*/
protected ConfigurableApplicationContext createApplicationContext() {
    Class<?> contextClass = this.applicationContextClass;
    if (contextClass == null) {
        try {
            contextClass = Class
                    .forName(this.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);
        } catch (ClassNotFoundException ex) {
            throw new IllegalStateException("Unable create a default ApplicationContext, "
                    + "please specify an ApplicationContextClass", ex);
        }
    }
    return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);
}

From source file:org.springframework.flex.core.io.SpringPropertyProxy.java

/**
 * Factory method for creating correctly configured Spring property proxy instances.
 * @param beanType the type being introspected
 * @param useDirectFieldAccess whether to access fields directly
 * @param conversionService the conversion service to use for property type conversion
 * @return a properly configured property proxy
 *///www  .j a  va 2s. c  om
public static SpringPropertyProxy proxyFor(Class<?> beanType, boolean useDirectFieldAccess,
        ConversionService conversionService) {
    if (PropertyProxyUtils.hasAmfCreator(beanType)) {
        SpringPropertyProxy proxy = new DelayedWriteSpringPropertyProxy(beanType, useDirectFieldAccess,
                conversionService);
        return proxy;
    } else {
        Assert.isTrue(beanType.isEnum() || ClassUtils.hasConstructor(beanType),
                "Failed to create SpringPropertyProxy for " + beanType.getName() + " - Classes mapped "
                        + "for deserialization from AMF must have either a no-arg default constructor, "
                        + "or a constructor annotated with " + AmfCreator.class.getName());
        SpringPropertyProxy proxy = new SpringPropertyProxy(beanType, useDirectFieldAccess, conversionService);

        try {
            //If possible, create an instance to introspect and cache the property names               
            Object instance = BeanUtils.instantiate(beanType);
            proxy.setPropertyNames(
                    PropertyProxyUtils.findPropertyNames(conversionService, useDirectFieldAccess, instance));
        } catch (BeanInstantiationException ex) {
            //Property names can't be cached, but this is ok
        }

        return proxy;
    }
}

From source file:org.springframework.osgi.extender.internal.blueprint.activator.BlueprintContainerProcessor.java

public BlueprintContainerProcessor(EventAdminDispatcher dispatcher, BlueprintListenerManager listenerManager,
        Bundle extenderBundle) {/*from   w w w .  jav  a 2s. c  o m*/
    this.dispatcher = dispatcher;
    this.listenerManager = listenerManager;
    this.extenderBundle = extenderBundle;

    Class<?> processorClass = ClassUtils.resolveClassName(
            "org.springframework.osgi.blueprint.container.support.internal.config.CycleOrderingProcessor",
            BundleContextAware.class.getClassLoader());

    cycleBreaker = (BeanFactoryPostProcessor) BeanUtils.instantiate(processorClass);
}

From source file:org.springframework.retry.backoff.BackOffPolicySerializationTests.java

@Parameters(name = "{index}: {0}")
public static List<Object[]> policies() {
    List<Object[]> result = new ArrayList<Object[]>();
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true);
    scanner.addIncludeFilter(new AssignableTypeFilter(BackOffPolicy.class));
    scanner.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*Test.*")));
    scanner.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*Mock.*")));
    scanner.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*Configuration.*")));
    Set<BeanDefinition> candidates = scanner.findCandidateComponents("org.springframework.retry");
    for (BeanDefinition beanDefinition : candidates) {
        try {/* w  ww . ja v a 2s .  com*/
            result.add(new Object[] { BeanUtils
                    .instantiate(ClassUtils.resolveClassName(beanDefinition.getBeanClassName(), null)) });
        } catch (Exception e) {
            logger.warn("Cannot create instance of " + beanDefinition.getBeanClassName());
        }
    }
    return result;
}

From source file:org.springframework.retry.policy.RetryContextSerializationTests.java

@Parameters(name = "{index}: {0}")
public static List<Object[]> policies() {
    List<Object[]> result = new ArrayList<Object[]>();
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(true);
    scanner.addIncludeFilter(new AssignableTypeFilter(RetryPolicy.class));
    scanner.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*Test.*")));
    scanner.addExcludeFilter(new RegexPatternTypeFilter(Pattern.compile(".*Mock.*")));
    Set<BeanDefinition> candidates = scanner.findCandidateComponents("org.springframework.retry.policy");
    for (BeanDefinition beanDefinition : candidates) {
        try {//from w  w  w .j  a  v a2  s .  co m
            result.add(new Object[] { BeanUtils
                    .instantiate(ClassUtils.resolveClassName(beanDefinition.getBeanClassName(), null)) });
        } catch (Exception e) {
            logger.warn("Cannot create instance of " + beanDefinition.getBeanClassName(), e);
        }
    }
    ExceptionClassifierRetryPolicy extra = new ExceptionClassifierRetryPolicy();
    extra.setExceptionClassifier(new SubclassClassifier<Throwable, RetryPolicy>(new AlwaysRetryPolicy()));
    result.add(new Object[] { extra });
    return result;
}

From source file:org.springframework.security.oauth2.client.test.OAuth2ContextSetup.java

private OAuth2ProtectedResourceDetails creatResource(Object target, OAuth2ContextConfiguration contextLoader) {
    Class<? extends OAuth2ProtectedResourceDetails> type = contextLoader.value();
    if (type == OAuth2ProtectedResourceDetails.class) {
        type = contextLoader.resource();
    }//from ww  w  . ja  v  a2  s .co  m
    Constructor<? extends OAuth2ProtectedResourceDetails> constructor = ClassUtils
            .getConstructorIfAvailable(type, TestAccounts.class);
    if (constructor != null && testAccounts != null) {
        return BeanUtils.instantiateClass(constructor, testAccounts);
    }
    constructor = ClassUtils.getConstructorIfAvailable(type, Environment.class);
    if (constructor != null && environment != null) {
        return BeanUtils.instantiateClass(constructor, environment);
    }
    constructor = ClassUtils.getConstructorIfAvailable(type, Object.class);
    if (constructor != null) {
        return BeanUtils.instantiateClass(constructor, target);
    }
    // Fallback to default constructor
    return BeanUtils.instantiate(type);
}

From source file:org.springframework.web.socket.handler.BeanCreatingHandlerProvider.java

public T getHandler() {
    if (this.beanFactory == null) {
        logger.warn("No BeanFactory available, attempting to use default constructor");
        return BeanUtils.instantiate(this.handlerType);
    } else {/*from w w  w.  ja va  2  s. c o m*/
        return this.beanFactory.createBean(this.handlerType);
    }
}

From source file:org.springframework.web.socket.support.BeanCreatingHandlerProvider.java

public T getHandler() {
    if (logger.isTraceEnabled()) {
        logger.trace("Creating instance for handler type " + this.handlerType);
    }/*w w  w .j  a va 2s. c o m*/
    if (this.beanFactory == null) {
        logger.warn("No BeanFactory available, attempting to use default constructor");
        return BeanUtils.instantiate(this.handlerType);
    } else {
        return this.beanFactory.createBean(this.handlerType);
    }
}