Example usage for org.springframework.beans.factory.config AutowireCapableBeanFactory AUTOWIRE_CONSTRUCTOR

List of usage examples for org.springframework.beans.factory.config AutowireCapableBeanFactory AUTOWIRE_CONSTRUCTOR

Introduction

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

Prototype

int AUTOWIRE_CONSTRUCTOR

To view the source code for org.springframework.beans.factory.config AutowireCapableBeanFactory AUTOWIRE_CONSTRUCTOR.

Click Source Link

Document

Constant that indicates autowiring the greediest constructor that can be satisfied (involves resolving the appropriate constructor).

Usage

From source file:com.github.pjungermann.config.validation.ConfigValidatorTest.java

static void autowireByConstructor(Class... classes) {
    for (Class clazz : classes) {
        applicationContext.registerBeanDefinition(clazz.getName(),
                BeanDefinitionBuilder.rootBeanDefinition(clazz)
                        .setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR).getBeanDefinition());
    }/*  ww w  . j av a 2s.c o m*/
}

From source file:hudson.util.spring.DefaultBeanConfiguration.java

public void setProperty(String property, Object newValue) {
    if (PARENT.equals(property)) {
        setParent(newValue);/* ww  w  .j  a  v a  2 s.co m*/
    } else {
        AbstractBeanDefinition bd = getBeanDefinition();
        if (AUTOWIRE.equals(property)) {
            if (BY_NAME.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
            } else if (BY_TYPE.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
            } else if (Boolean.TRUE.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
            } else if (BY_CONSTRUCTOR.equals(newValue)) {
                bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
            }
        }
        // constructorArgs
        else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
            ConstructorArgumentValues cav = new ConstructorArgumentValues();
            List args = (List) newValue;
            for (Object e : args) {
                cav.addGenericArgumentValue(e);
            }
            bd.setConstructorArgumentValues(cav);
        }
        // destroyMethod
        else if (DESTROY_METHOD.equals(property)) {
            if (newValue != null)
                bd.setDestroyMethodName(newValue.toString());
        }
        // factoryBean
        else if (FACTORY_BEAN.equals(property)) {
            if (newValue != null)
                bd.setFactoryBeanName(newValue.toString());
        }
        // factoryMethod
        else if (FACTORY_METHOD.equals(property)) {
            if (newValue != null)
                bd.setFactoryMethodName(newValue.toString());
        }
        // initMethod
        else if (INIT_METHOD.equals(property)) {
            if (newValue != null)
                bd.setInitMethodName(newValue.toString());
        } else if (wrapper.isWritableProperty(property)) {

            wrapper.setPropertyValue(property, newValue);
        }
        // autowire
        else {
            super.setProperty(property, newValue);
        }
    }
}

From source file:com.github.pjungermann.config.specification.dsl.groovy.GroovyDSLSpecificationReaderTest.java

@Before
public void setUp() {
    applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton(NullableConstraintFactory.class.getName(),
            NullableConstraintFactory.class);
    applicationContext.registerSingleton(MatchesConstraintFactory.class.getName(),
            MatchesConstraintFactory.class);
    applicationContext.registerSingleton(RangeConstraintFactory.class.getName(), RangeConstraintFactory.class);
    applicationContext.registerSingleton(SizeConstraintFactory.class.getName(), SizeConstraintFactory.class);
    applicationContext.registerSingleton(MaxSizeConstraintFactory.class.getName(),
            MaxSizeConstraintFactory.class);
    applicationContext.registerSingleton("typeConverter", AsTypeConverter.class);
    BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(ConstraintRegistry.class)
            .setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR).getBeanDefinition();
    applicationContext.registerBeanDefinition("constraintRegistry", beanDefinition);
    applicationContext.refresh();/* www.  j  a v a2s . c  o m*/

    reader = new GroovyDSLSpecificationReader(applicationContext);
}

From source file:net.vitarara.groovyworks.GroovyStrutsSpringObjectFactory.java

@Inject
public void setServletContext(ServletContext servletContext) {
    log.info("Groovy Spring Plugin: Initializing");

    // Create the Groovy classloader and make it a child of the current thread's class loader.
    CompilerConfiguration cc = new CompilerConfiguration();
    cc.setRecompileGroovySource(true);// w w  w.  j  a va2 s . c  o  m
    gcl = new GroovyClassLoader(Thread.currentThread().getContextClassLoader(), cc);
    gcl.setShouldRecompile(true);
    // gcl = new GroovyClassLoader (Thread.currentThread().getContextClassLoader () );
    // gcl.setShouldRecompile (false);
    log.info("GroovyClassLoader recompilation set to: " + gcl.isShouldRecompile());

    log.info("Initializing Struts-Spring integration...");

    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    if (appContext == null) {
        // uh oh! looks like the lifecycle listener wasn't installed. Let's inform the user
        String message = "********** FATAL ERROR STARTING UP SPRING-STRUTS INTEGRATION **********\n"
                + "Looks like the Spring listener was not configured for your web app! \n"
                + "Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.\n"
                + "You might need to add the following to web.xml: \n" + "    <listener>\n"
                + "        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>\n"
                + "    </listener>";
        log.fatal(message);
        return;
    }

    this.setApplicationContext(appContext);

    int type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; // default
    if ("name".equals(autoWire)) {
        type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
    } else if ("type".equals(autoWire)) {
        type = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
    } else if ("auto".equals(autoWire)) {
        type = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
    } else if ("constructor".equals(autoWire)) {
        type = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
    }
    this.setAutowireStrategy(type);

    this.setUseClassCache(useClassCache);

    log.info("... initialized Struts-Spring integration successfully");

    // Set the appContext's class loader to the GroovyClassLoader. The refresh the context.
    // ((org.springframework.core.io.DefaultResourceLoader) appContext).setClassLoader (gcl);
    // ((ConfigurableWebApplicationContext) appContext).refresh ();

    log.info("Groovy Spring Plugin: Initialization complete");
}

From source file:com.opensymphony.xwork2.spring.SpringObjectFactory.java

/**
 * Sets the autowiring strategy/*from   w w  w .  ja v a  2s  .  c o m*/
 *
 * @param autowireStrategy the autowire strategy
 */
public void setAutowireStrategy(int autowireStrategy) {
    switch (autowireStrategy) {
    case AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT:
        LOG.info("Setting autowire strategy to autodetect");
        this.autowireStrategy = autowireStrategy;
        break;
    case AutowireCapableBeanFactory.AUTOWIRE_BY_NAME:
        LOG.info("Setting autowire strategy to name");
        this.autowireStrategy = autowireStrategy;
        break;
    case AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE:
        LOG.info("Setting autowire strategy to type");
        this.autowireStrategy = autowireStrategy;
        break;
    case AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR:
        LOG.info("Setting autowire strategy to constructor");
        this.autowireStrategy = autowireStrategy;
        break;
    case AutowireCapableBeanFactory.AUTOWIRE_NO:
        LOG.info("Setting autowire strategy to none");
        this.autowireStrategy = autowireStrategy;
        break;
    default:
        throw new IllegalStateException("Invalid autowire type set");
    }
}

From source file:org.openvpms.component.business.service.scheduler.JobRunner.java

/**
 * Creates the job./*ww  w  .  ja va  2 s  . co  m*/
 *
 * @return a new job
 * @throws ClassNotFoundException if the job class cannot be found
 */
private Job createJob() throws ClassNotFoundException {
    Job result;
    IMObjectBean bean = new IMObjectBean(configuration, service);
    Class type = Class.forName(bean.getString("class"));
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory(context);
    factory.registerSingleton("jobConfiguration", configuration);
    Object job = factory.createBean(type, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true);
    if (job instanceof Runnable) {
        result = new DelegatingJob((Runnable) job);
    } else {
        result = (Job) job;
    }
    return result;
}

From source file:org.hbird.business.configurator.ConfiguratorComponentDriver.java

/**
 * Request to start a component.//from www .  j av a  2s. c  o m
 * 
 * @param command The start component request
 * @param context The camel context in which the component is running.
 * @throws Exception
 */
public synchronized void startComponent(@Body StartComponent command, CamelContext context) throws Exception {
    IStartableEntity entity = command.getEntity();
    String id = entity.getID();
    String name = entity.getName();
    String driverName = entity.getDriverName();
    entity.setContext(context);
    LOG.info(
            "Received start request for IStartableEntity ID: '{}', name: '{}'. Will use driver '{}' and CamelContex '{}'.",
            new Object[] { id, name, driverName, context.getName() });

    if (components.containsKey(id)) {
        LOG.error("Received second request for start of the same IStartableEntity - '{}'.", id);
    } else {
        /* Find the component builder and get it to setup and start the component. */
        if (driverName == null) {
            LOG.error("Cannot start IStartableEntity '{}'. No driver set.", id);
        } else {
            try {
                AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
                SoftwareComponentDriver<?> builder = (SoftwareComponentDriver<?>) factory.autowire(
                        Class.forName(driverName), AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
                builder.setCommand(command);
                builder.setContext((ModelCamelContext) context);
                context.addRoutes(builder);

                /* Register component in list of components maintained by this configurator. */
                components.put(id, builder.getRouteCollection());
                context.createProducerTemplate().asyncSendBody(ENDPOINT_TO_EVENTS,
                        createStartEvent(this.entity.getID(), id));
            } catch (Exception e) {
                LOG.error("Failed to start IStartableEntity '{}'", id, e);
            }
        }
    }
}

From source file:dirty.mockito.junit.rules.ActiveTestRule.java

/**
 * @param target//  www .ja  v a 2  s  . com
 *        the JUnit test we're intercepting
 */
@SuppressWarnings("unchecked")
private void instantiateObjectToTest(final Object target) {
    for (final Field field : target.getClass().getDeclaredFields()) {
        if (field.getType().equals(classUnderTest)) {

            final T object;
            if (JpaDaoSupport.class.isAssignableFrom(classUnderTest)) {
                final String beanName = field.getName();
                registerJpaDaoBeanDefinition(beanName);
                object = beanFactory.getBean(beanName, classUnderTest);
            } else {
                object = (T) beanFactory.createBean(classUnderTest,
                        AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true);
            }
            try {
                Reflection.set(field).of(target).to(object);
            } catch (final IllegalAccessException e) {
                throw new MockitoException("Problems instantiating test object", e);
            }
        }
    }
}

From source file:com.opensymphony.xwork2.spring.SpringObjectFactory.java

/**
 * @param clazz class of bean//from  ww w. jav  a 2s.  co  m
 * @param extraContext additional context parameters
 * @return bean
 * @throws Exception in case of any errors
 */
@Override
public Object buildBean(Class clazz, Map<String, Object> extraContext) throws Exception {
    Object bean;

    try {
        // Decide to follow autowire strategy or use the legacy approach which mixes injection strategies
        if (alwaysRespectAutowireStrategy) {
            // Leave the creation up to Spring
            bean = autoWiringFactory.createBean(clazz, autowireStrategy, false);
            injectApplicationContext(bean);
            return injectInternalBeans(bean);
        } else if (enableAopSupport) {
            bean = autoWiringFactory.createBean(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
            bean = autoWireBean(bean, autoWiringFactory);
            bean = autoWiringFactory.initializeBean(bean, bean.getClass().getName());
            return bean;
        } else {
            bean = autoWiringFactory.autowire(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
            bean = autoWiringFactory.initializeBean(bean, bean.getClass().getName());
            return autoWireBean(bean, autoWiringFactory);
        }
    } catch (UnsatisfiedDependencyException e) {
        LOG.error("Error building bean", e);
        // Fall back
        return autoWireBean(super.buildBean(clazz, extraContext), autoWiringFactory);
    }
}