Example usage for org.springframework.beans.factory.config ConstructorArgumentValues ConstructorArgumentValues

List of usage examples for org.springframework.beans.factory.config ConstructorArgumentValues ConstructorArgumentValues

Introduction

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

Prototype

public ConstructorArgumentValues() 

Source Link

Document

Create a new empty ConstructorArgumentValues object.

Usage

From source file:org.mybatis.spring.annotation.EnableMapperScanningTest.java

@Test
public void testScanWithExplicitSqlSessionTemplate() throws Exception {
    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(SqlSessionTemplate.class);
    ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues();
    constructorArgs.addGenericArgumentValue(new RuntimeBeanReference("sqlSessionFactory"));
    definition.setConstructorArgumentValues(constructorArgs);
    applicationContext.registerBeanDefinition("sqlSessionTemplate", definition);

    applicationContext.register(AppConfigWithSqlSessionTemplate.class);

    startContext();/*from ww  w  .  ja  va 2  s.  c o m*/

    // all interfaces with methods should be loaded
    applicationContext.getBean("mapperInterface");
    applicationContext.getBean("mapperSubinterface");
    applicationContext.getBean("mapperChildInterface");
    applicationContext.getBean("annotatedMapper");

}

From source file:com.seovic.validation.config.ValidationBeanDefinitionParser.java

private static BeanDefinition parseErrorMessageAction(Element message, ParserContext parserContext) {
    String messageId = message.getAttribute("id");
    String[] providers = message.getAttribute("providers").split(",");
    String typeName = ErrorMessageAction.class.getName();

    ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues();
    ctorArgs.addGenericArgumentValue(messageId);
    ctorArgs.addGenericArgumentValue(providers);

    String when = message.getAttribute("when");
    MutablePropertyValues properties = new MutablePropertyValues();
    if (StringUtils.hasText(when)) {
        properties.addPropertyValue("when", when);
    }//from w w w  .  ja  va2 s .c o  m
    AbstractBeanDefinition action;
    try {
        action = BeanDefinitionReaderUtils.createBeanDefinition(null, typeName,
                parserContext.getReaderContext().getBeanClassLoader());
    } catch (ClassNotFoundException e) {
        throw new BeanCreationException("Error occured during creation of bean definition", e);
    }
    action.setConstructorArgumentValues(ctorArgs);
    action.setPropertyValues(properties);
    return action;
}

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

protected AbstractBeanDefinition createBeanDefinition() {
    AbstractBeanDefinition bd;/*ww  w.java 2 s.c o m*/
    if (constructorArgs.size() > 0) {
        ConstructorArgumentValues cav = new ConstructorArgumentValues();
        for (Object constructorArg : constructorArgs) {
            cav.addGenericArgumentValue(constructorArg);
        }
        if (StringUtils.isBlank(parentName)) {
            bd = new RootBeanDefinition(clazz, cav, null);
        } else {
            bd = new ChildBeanDefinition(parentName, clazz, cav, null);
        }
        bd.setSingleton(singleton);
    } else {
        if (StringUtils.isBlank(parentName)) {
            bd = new RootBeanDefinition(clazz, singleton);
        } else {
            bd = new ChildBeanDefinition(parentName, clazz, null, null);
            bd.setSingleton(singleton);
        }

    }
    wrapper = new BeanWrapperImpl(bd);
    return bd;
}

From source file:org.mybatis.spring.config.NamespaceTest.java

private GenericApplicationContext setupSqlSessionTemplate() {

    GenericApplicationContext genericApplicationContext = setupSqlSessionFactory();
    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(SqlSessionTemplate.class);
    ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues();
    constructorArgs.addGenericArgumentValue(new RuntimeBeanReference("sqlSessionFactory"));
    definition.setConstructorArgumentValues(constructorArgs);
    genericApplicationContext.registerBeanDefinition("sqlSessionTemplate", definition);
    return genericApplicationContext;
}

From source file:org.mybatis.spring.mapper.MapperScannerConfigurerTest.java

@Test
public void testScanWithExplicitSqlSessionTemplate() throws Exception {
    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(SqlSessionTemplate.class);
    ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues();
    constructorArgs.addGenericArgumentValue(new RuntimeBeanReference("sqlSessionFactory"));
    definition.setConstructorArgumentValues(constructorArgs);
    applicationContext.registerBeanDefinition("sqlSessionTemplate", definition);

    applicationContext.getBeanDefinition("mapperScanner").getPropertyValues().add("sqlSessionTemplateBeanName",
            "sqlSessionTemplate");

    testInterfaceScan();/*from   w w w  .  j  a  va 2 s  .co  m*/
}

From source file:org.jsr107.ri.annotations.spring.config.AnnotationDrivenJCacheBeanDefinitionParser.java

/**
 * Create the {@link RuntimeBeanReference} used to apply the caching interceptor
 *
 * @return Reference to the {@link RuntimeBeanReference}. Should never be null.
 *//*w  w w .  j  av a2 s.c o m*/
protected RuntimeBeanReference setupPointcut(ParserContext parserContext, Object elementSource,
        RuntimeBeanReference cacheOperationSourceRuntimeReference,
        RuntimeBeanReference cacheInterceptorSourceRuntimeReference) {

    final RootBeanDefinition pointcut = new RootBeanDefinition(CacheStaticMethodMatcherPointcut.class);
    pointcut.setSource(elementSource);
    pointcut.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

    final ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
    constructorArgumentValues.addIndexedArgumentValue(0, cacheOperationSourceRuntimeReference);
    constructorArgumentValues.addIndexedArgumentValue(1, cacheInterceptorSourceRuntimeReference);
    pointcut.setConstructorArgumentValues(constructorArgumentValues);

    final String pointcutBeanName = pointcut.getBeanClassName() + "_"
            + cacheInterceptorSourceRuntimeReference.getBeanName();

    final BeanDefinitionRegistry registry = parserContext.getRegistry();
    registry.registerBeanDefinition(pointcutBeanName, pointcut);

    return new RuntimeBeanReference(pointcutBeanName);
}

From source file:com.capgemini.boot.core.factory.internal.SettingBackedBeanFactoryPostProcessor.java

private BeanDefinition createBeanDefinition(Object settings, Object setting, String factoryBeanName,
        Method factoryMethod) {/*from w  ww  .  java2s. co  m*/
    final GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setFactoryBeanName(factoryBeanName);
    definition.setFactoryMethodName(factoryMethod.getName());

    //Create method arguments (confusingly called constructor arguments in spring)
    final ConstructorArgumentValues arguments = new ConstructorArgumentValues();
    arguments.addIndexedArgumentValue(0, setting);

    final int parameterCount = factoryMethod.getParameterTypes().length;
    if (parameterCount == 2) {
        arguments.addIndexedArgumentValue(1, settings);
    } else if (parameterCount > 2) {
        throw getExceptionFactory().createInvalidArgumentsException(factoryMethod);
    }

    //TODO more checking of method arguments to ensure they're correct

    definition.setConstructorArgumentValues(arguments);

    return definition;
}

From source file:org.jsr107.ri.annotations.spring.config.AnnotationDrivenJCacheBeanDefinitionParser.java

/**
 * Create {@link org.aopalliance.intercept.MethodInterceptor} that is applies the caching logic to advised methods.
 *
 * @return Reference to the {@link org.aopalliance.intercept.MethodInterceptor}. Should never be null.
 *//*from w  w w .j  av  a 2  s.  c  om*/
protected RuntimeBeanReference setupInterceptor(Class<? extends AbstractCacheInterceptor<?>> interceptorClass,
        ParserContext parserContext, Object elementSource,
        RuntimeBeanReference cacheOperationSourceRuntimeReference) {

    final RootBeanDefinition interceptor = new RootBeanDefinition(interceptorClass);
    interceptor.setSource(elementSource);
    interceptor.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

    final ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
    constructorArgumentValues.addIndexedArgumentValue(0, cacheOperationSourceRuntimeReference);
    interceptor.setConstructorArgumentValues(constructorArgumentValues);

    final XmlReaderContext readerContext = parserContext.getReaderContext();
    final String interceptorBeanName = readerContext.registerWithGeneratedName(interceptor);
    return new RuntimeBeanReference(interceptorBeanName);
}

From source file:com.fitbur.testify.di.spring.SpringServiceLocator.java

@Override
public void addService(ServiceDescriptor descriptor) {
    checkState(!context.containsBeanDefinition(descriptor.getName()),
            "Service with the name '%s' already exists.", descriptor.getName());

    GenericBeanDefinition bean = new GenericBeanDefinition();
    bean.setBeanClass(descriptor.getType());
    bean.setAutowireCandidate(descriptor.getDiscoverable());
    bean.setPrimary(descriptor.getPrimary());
    bean.setLazyInit(descriptor.getLazy());
    bean.setRole(ROLE_APPLICATION);/*from ww  w  .j  a v  a  2  s . c  o m*/

    if (descriptor.getInjectable()) {
        bean.setAutowireMode(AUTOWIRE_CONSTRUCTOR);
    } else {
        bean.setAutowireMode(AUTOWIRE_NO);
        ConstructorArgumentValues values = new ConstructorArgumentValues();

        Object[] arguments = descriptor.getArguments();
        for (int i = 0; i < arguments.length; i++) {
            Object arg = arguments[i];

            if (arg == null) {
                //TODO: warn user that the argument was not specified and there
                //for the real instance will be injected.
                continue;
            }

            values.addIndexedArgumentValue(i, arg);
        }

        bean.setConstructorArgumentValues(values);
    }

    ServiceScope scope = descriptor.getScope();
    switch (scope) {
    case PROTOTYPE:
        bean.setScope("prototype");
        break;
    case SINGLETON:
        bean.setScope("singleton");
        break;
    case REQUEST:
        bean.setScope("request");
        break;
    case SESSION:
        bean.setScope("session");
        break;
    case APPLICATION:
        bean.setScope("application");
        break;
    default:
        checkState(false, "Scope '{}' is not supported by Spring IoC.", scope.name());

    }
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
    beanFactory.registerBeanDefinition(descriptor.getName(), bean);
}

From source file:org.atomserver.utils.test.TestingAtomServer.java

private Server createServer(int port, String atomserverServletContext) {
    // set up the server and the atomserver web application context
    Server server = new Server(port);
    Context context = new Context(server, "/" + atomserverServletContext, true /*sessions*/,
            false /*no security*/);

    // we need to hard-code certain system properties to get the behavior we want here - we
    // will re-set them when we're done
    Properties properties = (Properties) System.getProperties().clone();
    System.setProperty("atomserver.env", "asdev-hsql-mem");
    System.setProperty("atomserver.servlet.context", atomserverServletContext);

    // TODO: this should be removed
    System.setProperty("atomserver.servlet.mapping", "v1");

    // our Spring application context will start off by loading the basic built-in bean
    // definitions
    appContext = new GenericWebApplicationContext();

    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext);
    xmlReader.loadBeanDefinitions(new ClassPathResource("org/atomserver/spring/propertyConfigurerBeans.xml"));
    xmlReader.loadBeanDefinitions(new ClassPathResource("org/atomserver/spring/databaseBeans.xml"));
    xmlReader.loadBeanDefinitions(new ClassPathResource("org/atomserver/spring/logBeans.xml"));
    xmlReader.loadBeanDefinitions(new ClassPathResource("org/atomserver/spring/storageBeans.xml"));
    xmlReader.loadBeanDefinitions(new ClassPathResource("org/atomserver/spring/abderaBeans.xml"));

    // if we were given a Spring config location, we use that -- otherwise we configure the
    // workspaces that were set up through the API
    if (springBeansLocation != null) {
        xmlReader.loadBeanDefinitions(new ClassPathResource(springBeansLocation));
    } else {//from   w w w  .  j av a  2 s  . com
        RootBeanDefinition workspaces = new RootBeanDefinition(HashSet.class);
        ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
        constructorArgumentValues.addGenericArgumentValue(workspaceSet);
        workspaces.setConstructorArgumentValues(constructorArgumentValues);

        appContext.registerBeanDefinition("org.atomserver-workspaces", workspaces);
    }

    // override the base content storage to use DB-based storage
    RootBeanDefinition storage = new RootBeanDefinition(DBBasedContentStorage.class);
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.addPropertyValue("contentDAO", new RuntimeBeanReference("org.atomserver-contentDAO"));
    propertyValues.addPropertyValue("entriesDAO", new RuntimeBeanReference("org.atomserver-entriesDAO"));
    storage.setPropertyValues(propertyValues);
    appContext.registerBeanDefinition("org.atomserver-contentStorage", storage);

    // clear the existing ENV
    ConfigurationAwareClassLoader.invalidateENV();

    // refresh the context to actually instantiate everything.
    appContext.refresh();

    // re-set the system properties
    System.setProperties(properties);

    // clear the update ENV
    ConfigurationAwareClassLoader.invalidateENV();

    // put our app context into the servlet context
    context.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext);

    // load and init the service context for v1
    final ServiceContext serviceContext = (ServiceContext) appContext.getBean(ServiceContext.class.getName());
    serviceContext.init(new Abdera(), Collections.EMPTY_MAP);

    // create a new AtomServerServlet - but override the createServiceContext method
    AtomServerServlet servlet = new AtomServerServlet() {
        protected ServiceContext createServiceContext() {
            return serviceContext;
        }
    };

    // load and init the service context for v2
    final ServiceContext serviceContextV2 = (ServiceContext) appContext
            .getBean("org.atomserver-serviceContext.v2");
    serviceContextV2.init(new Abdera(), Collections.EMPTY_MAP);

    // create a new AtomServerServlet - but override the createServiceContext method
    AtomServerServlet servletV2 = new AtomServerServlet() {
        protected ServiceContext createServiceContext() {
            return serviceContextV2;
        }
    };

    // register the servlets
    context.addServlet(new ServletHolder(servlet), "/v1/*");
    context.addServlet(new ServletHolder(servletV2), "/v2/*");

    EntriesDAOiBatisImpl entriesDAO = (EntriesDAOiBatisImpl) appContext.getBean("org.atomserver-entriesDAO");
    entriesDAO.setUseWorkspaceCollectionCache(false);

    // ready to be started
    return server;
}