Example usage for org.springframework.beans.factory.config BeanDefinition getConstructorArgumentValues

List of usage examples for org.springframework.beans.factory.config BeanDefinition getConstructorArgumentValues

Introduction

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

Prototype

ConstructorArgumentValues getConstructorArgumentValues();

Source Link

Document

Return the constructor argument values for this bean.

Usage

From source file:co.paralleluniverse.common.spring.SpringContainerHelper.java

public static Collection<String> getBeanDependencies(BeanDefinition beanDefinition) {
    Set<String> dependencies = new HashSet<String>();
    if (beanDefinition.getDependsOn() != null)
        dependencies.addAll(Arrays.asList(beanDefinition.getDependsOn()));

    for (ValueHolder value : beanDefinition.getConstructorArgumentValues().getGenericArgumentValues()) {
        if (value.getValue() instanceof BeanReference)
            dependencies.add(((BeanReference) value.getValue()).getBeanName());
    }/*from w  ww . j  a  va  2  s .c om*/
    for (ValueHolder value : beanDefinition.getConstructorArgumentValues().getIndexedArgumentValues()
            .values()) {
        if (value.getValue() instanceof BeanReference)
            dependencies.add(((BeanReference) value.getValue()).getBeanName());
    }
    for (PropertyValue value : beanDefinition.getPropertyValues().getPropertyValueList()) {
        if (value.getValue() instanceof BeanReference)
            dependencies.add(((BeanReference) value.getValue()).getBeanName());
    }
    return dependencies;
}

From source file:co.paralleluniverse.common.spring.SpringContainerHelper.java

public static ConfigurableApplicationContext createContext(String defaultDomain, Resource xmlResource,
        Object properties, BeanFactoryPostProcessor beanFactoryPostProcessor) {
    LOG.info("JAVA: {} {}, {}", new Object[] { System.getProperty("java.runtime.name"),
            System.getProperty("java.runtime.version"), System.getProperty("java.vendor") });
    LOG.info("OS: {} {}, {}", new Object[] { System.getProperty("os.name"), System.getProperty("os.version"),
            System.getProperty("os.arch") });
    LOG.info("DIR: {}", System.getProperty("user.dir"));

    final DefaultListableBeanFactory beanFactory = createBeanFactory();
    final GenericApplicationContext context = new GenericApplicationContext(beanFactory);
    context.registerShutdownHook();//from ww  w  .  java2 s  .c  o m

    final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    propertyPlaceholderConfigurer
            .setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);

    if (properties != null) {
        if (properties instanceof Resource)
            propertyPlaceholderConfigurer.setLocation((Resource) properties);
        else if (properties instanceof Properties)
            propertyPlaceholderConfigurer.setProperties((Properties) properties);
        else
            throw new IllegalArgumentException(
                    "Properties argument - " + properties + " - is of an unhandled type");
    }
    context.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);

    // MBean exporter
    //final MBeanExporter mbeanExporter = new AnnotationMBeanExporter();
    //mbeanExporter.setServer(ManagementFactory.getPlatformMBeanServer());
    //beanFactory.registerSingleton("mbeanExporter", mbeanExporter);
    context.registerBeanDefinition("mbeanExporter", getMBeanExporterBeanDefinition(defaultDomain));

    // inject bean names into components
    context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            for (String beanName : beanFactory.getBeanDefinitionNames()) {
                try {
                    final BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
                    if (beanDefinition.getBeanClassName() != null) { // van be null for factory methods
                        final Class<?> beanClass = Class.forName(beanDefinition.getBeanClassName());
                        if (Component.class.isAssignableFrom(beanClass))
                            beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, beanName);
                    }
                } catch (Exception ex) {
                    LOG.error("Error loading bean " + beanName + " definition.", ex);
                    throw new Error(ex);
                }
            }
        }
    });

    if (beanFactoryPostProcessor != null)
        context.addBeanFactoryPostProcessor(beanFactoryPostProcessor);

    beanFactory.registerCustomEditor(org.w3c.dom.Element.class,
            co.paralleluniverse.common.util.DOMElementProprtyEditor.class);

    final Map<String, Object> beans = new HashMap<String, Object>();

    beanFactory.addBeanPostProcessor(new BeanPostProcessor() {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            LOG.info("Loading bean {} [{}]", beanName, bean.getClass().getName());
            beans.put(beanName, bean);

            if (bean instanceof Service) {
                final BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
                Collection<String> dependencies = getBeanDependencies(beanDefinition);

                for (String dependeeName : dependencies) {
                    Object dependee = beanFactory.getBean(dependeeName);
                    if (dependee instanceof Service) {
                        ((Service) dependee).addDependedBy((Service) bean);
                        ((Service) bean).addDependsOn((Service) dependee);
                    }
                }
            }
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            LOG.info("Bean {} [{}] loaded", beanName, bean.getClass().getName());
            return bean;
        }
    });

    final XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) beanFactory);
    xmlReader.loadBeanDefinitions(xmlResource);

    // start container
    context.refresh();

    // Call .postInit() on all Components
    // There's probably a better way to do this
    try {
        for (Map.Entry<String, Object> entry : beans.entrySet()) {
            final String beanName = entry.getKey();
            final Object bean = entry.getValue();
            if (bean instanceof Component) {
                LOG.info("Performing post-initialization on bean {} [{}]", beanName, bean.getClass().getName());
                ((Component) bean).postInit();
            }
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }

    return context;
}

From source file:test.pl.chilldev.web.spring.config.StylesheetBeanDefinitionParserTest.java

@Test
public void parse() {
    GenericBeanDefinition bean = new GenericBeanDefinition();
    StylesheetBeanDefinitionParser parser = new StylesheetBeanDefinitionParser(bean);

    String href = "foo.css";
    String type = "text/css";
    String media = "print";

    when(this.element.getAttribute("href")).thenReturn(href);
    when(this.element.hasAttribute("media")).thenReturn(true);
    when(this.element.getAttribute("media")).thenReturn(media);
    when(this.element.hasAttribute("type")).thenReturn(true);
    when(this.element.getAttribute("type")).thenReturn(type);

    parser.parse(this.element, null);

    List<BeanDefinition> stylesheets = (List<BeanDefinition>) bean.getPropertyValues()
            .getPropertyValue(StylesheetBeanDefinitionParser.PROPERTY_STYLESHEETS).getValue();
    BeanDefinition stylesheet = stylesheets.get(0);
    ConstructorArgumentValues arguments = stylesheet.getConstructorArgumentValues();

    assertEquals("StylesheetBeanDefinitionParser.parse() should register stylesheet with it's location.", href,
            arguments.getIndexedArgumentValue(0, null).getValue());
    assertEquals("StylesheetBeanDefinitionParser.parse() should register stylesheet with it's MIME type.", type,
            arguments.getIndexedArgumentValue(1, null).getValue());
    assertEquals("StylesheetBeanDefinitionParser.parse() should register stylesheet with it's media query.",
            media, arguments.getIndexedArgumentValue(2, null).getValue());
}

From source file:test.pl.chilldev.web.spring.config.ScriptBeanDefinitionParserTest.java

@Test
public void parse() {
    GenericBeanDefinition bean = new GenericBeanDefinition();
    ScriptBeanDefinitionParser parser = new ScriptBeanDefinitionParser(bean);

    String src = "foo.js";
    String type = "application/javascript";
    String flow = "ASYNC";
    String charset = "utf-8";

    when(this.element.hasAttribute("charset")).thenReturn(true);
    when(this.element.getAttribute("charset")).thenReturn(charset);
    when(this.element.hasAttribute("flow")).thenReturn(true);
    when(this.element.getAttribute("flow")).thenReturn(flow);
    when(this.element.hasAttribute("type")).thenReturn(true);
    when(this.element.getAttribute("type")).thenReturn(type);
    when(this.element.getAttribute("src")).thenReturn(src);

    parser.parse(this.element, null);

    List<BeanDefinition> scripts = (List<BeanDefinition>) bean.getPropertyValues()
            .getPropertyValue(ScriptBeanDefinitionParser.PROPERTY_SCRIPTS).getValue();
    BeanDefinition script = scripts.get(0);
    ConstructorArgumentValues arguments = script.getConstructorArgumentValues();

    assertEquals("ScriptBeanDefinitionParser.parse() should register script with it's location.", src,
            arguments.getIndexedArgumentValue(0, null).getValue());
    assertEquals("ScriptBeanDefinitionParser.parse() should register script with it's MIME type.", type,
            arguments.getIndexedArgumentValue(1, null).getValue());
    assertEquals("ScriptBeanDefinitionParser.parse() should register script with it's loading flow.", flow,
            arguments.getIndexedArgumentValue(2, null).getValue());
    assertEquals("ScriptBeanDefinitionParser.parse() should register script with it's charset.", charset,
            arguments.getIndexedArgumentValue(3, null).getValue());
}

From source file:test.pl.chilldev.web.spring.config.LinkBeanDefinitionParserTest.java

@Test
public void parse() {
    GenericBeanDefinition bean = new GenericBeanDefinition();
    LinkBeanDefinitionParser parser = new LinkBeanDefinitionParser(bean);

    String href = "blog.rss";
    String media = "print";
    String type = "application/rss+xml";

    when(this.element.getAttribute("href")).thenReturn(href);
    when(this.element.hasAttribute("media")).thenReturn(true);
    when(this.element.getAttribute("media")).thenReturn(media);
    when(this.element.hasAttribute("type")).thenReturn(true);
    when(this.element.getAttribute("type")).thenReturn(type);

    parser.parse(this.element, null);

    List<BeanDefinition> links = (List<BeanDefinition>) bean.getPropertyValues()
            .getPropertyValue(LinkBeanDefinitionParser.PROPERTY_LINKS).getValue();
    BeanDefinition link = links.get(0);
    ConstructorArgumentValues arguments = link.getConstructorArgumentValues();

    assertEquals("LinkBeanDefinitionParser.parse() should register link with it's location.", href,
            arguments.getIndexedArgumentValue(0, null).getValue());
    assertTrue("LinkBeanDefinitionParser.parse() should register link with all it's relations.",
            ((Set) (arguments.getIndexedArgumentValue(1, null).getValue())).contains(this.relation));
    assertEquals("LinkBeanDefinitionParser.parse() should register link with it's MIME type.", type,
            arguments.getIndexedArgumentValue(2, null).getValue());
    assertEquals("LinkBeanDefinitionParser.parse() should register link with it's media query.", media,
            arguments.getIndexedArgumentValue(3, null).getValue());
}

From source file:net.sourceforge.safr.core.spring.config.SecurityAnnotationDrivenBeanDefinitionParser.java

private void setConstructorArgument(BeanDefinition beanDefinition, Object constructorArgument) {
    beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(constructorArgument);
}

From source file:com.himanshu.spring.context.loader.sameconfigallcontext.bean.visitor.BeanVisitor.java

public void visit(BeanDefinition beanDefinition) {
    visitProperties(beanDefinition.getPropertyValues());
    visitGenericConstructorArgs(beanDefinition.getConstructorArgumentValues().getGenericArgumentValues());
    visitIndexedConstructorArgs(//from w  ww.  j  a v  a  2s .  c o  m
            beanDefinition.getConstructorArgumentValues().getIndexedArgumentValues().values());
}

From source file:com.minderupt.integration.pushmsg.config.APNSPushMessageOutboundChannelAdapterParserTest.java

@Test
public void testGetBeanDefinitionFromElementWithApnsHostPortCertificatePathBasicTest() throws Exception {

    Element element = mock(Element.class);
    when(element.getAttribute(ParserConstants.ATTRIBUTE_NAME_APNS_HOST)).thenReturn("apns.test.host.com");
    when(element.getAttribute(ParserConstants.ATTRIBUTE_NAME_APNS_PORT)).thenReturn("9160");
    when(element.getAttribute(ParserConstants.ATTRIBUTE_NAME_CERTIFICATE_PATH))
            .thenReturn("/path/to/certificate");

    ParserContext parserContext = new ParserContext(mock(XmlReaderContext.class),
            mock(BeanDefinitionParserDelegate.class));

    BeanDefinition beanDefinition = parseConsumer(element, parserContext);
    assertNotNull(beanDefinition);/*w  w  w. ja v a 2  s  .c o  m*/
    assertEquals("/path/to/certificate",
            beanDefinition.getConstructorArgumentValues().getArgumentValue(0, String.class).getValue());
    assertEquals("apns.test.host.com",
            beanDefinition.getConstructorArgumentValues().getArgumentValue(1, String.class).getValue());
    assertEquals(9160,
            beanDefinition.getConstructorArgumentValues().getArgumentValue(2, Integer.class).getValue());

}

From source file:org.wallride.autoconfigure.WebAdminComponentScanRegistrar.java

private void updateWebAdminComponentScanBeanPostProcessor(BeanDefinitionRegistry registry,
        Set<String> packagesToScan) {
    BeanDefinition definition = registry.getBeanDefinition(BEAN_NAME);
    ConstructorArgumentValues.ValueHolder constructorArguments = definition.getConstructorArgumentValues()
            .getGenericArgumentValue(String[].class);
    Set<String> mergedPackages = new LinkedHashSet<>();
    mergedPackages.addAll(Arrays.asList((String[]) constructorArguments.getValue()));
    mergedPackages.addAll(packagesToScan);
    constructorArguments.setValue(toArray(mergedPackages));
}

From source file:org.wallride.autoconfigure.WebGuestComponentScanRegistrar.java

private void updateWebGuestComponentScanBeanPostProcessor(BeanDefinitionRegistry registry,
        Set<String> packagesToScan) {
    BeanDefinition definition = registry.getBeanDefinition(BEAN_NAME);
    ConstructorArgumentValues.ValueHolder constructorArguments = definition.getConstructorArgumentValues()
            .getGenericArgumentValue(String[].class);
    Set<String> mergedPackages = new LinkedHashSet<>();
    mergedPackages.addAll(Arrays.asList((String[]) constructorArguments.getValue()));
    mergedPackages.addAll(packagesToScan);
    constructorArguments.setValue(toArray(mergedPackages));
}