Example usage for org.springframework.context ApplicationContext getAutowireCapableBeanFactory

List of usage examples for org.springframework.context ApplicationContext getAutowireCapableBeanFactory

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getAutowireCapableBeanFactory.

Prototype

AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;

Source Link

Document

Expose AutowireCapableBeanFactory functionality for this context.

Usage

From source file:org.echocat.jomon.spring.BeanProvider.java

@Nonnull
protected ApplicationContext getApplicationContext() {
    final ApplicationContext context = _applicationContext;
    if (context == null) {
        throw new IllegalStateException("setApplicationContext() was not called yet.");
    }//www  . j a  v a 2  s .  co m
    context.getAutowireCapableBeanFactory();
    return context;
}

From source file:de.zib.gndms.gndmc.test.gorfx.GORFXClientMain.java

@Override
public void run() throws Exception {

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/client-context.xml");
    FullGORFXClient gorfxClient = (FullGORFXClient) context.getAutowireCapableBeanFactory()
            .createBean(FullGORFXClient.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    gorfxClient.setServiceURL(gndmsEpUrl);

    if (gorfxClient.getRestTemplate() == null)
        throw new IllegalStateException("restTemplate is null");

    System.out.println("connecting to: \"" + gndmsEpUrl + "\"");
    Uri gorfxUrl = new Uri(gndmsEpUrl);
    if ("https".equals(gorfxUrl.getScheme())) {
        System.out.println("https specified. enabling SSL");
        SetupSSL setupSSL = KeyStoreTest.initSSL(ksp, ksp, tsp);
        setupSSL.setupDefaultSSLContext(ksp);
    }//from www. j  a  va2  s .  c om

    System.out.println("requesting facets");
    ResponseEntity<Facets> res = gorfxClient.listAvailableFacets(dn);
    System.out.println("StatusCode: " + res.getStatusCode());
    showHeader(res.getHeaders());
    System.out.println("Body: ");
    Facets f = res.getBody();
    for (Facet fa : f.getFacets()) {
        System.out.println(fa.getName() + " " + fa.getUrl());
    }

}

From source file:de.zib.gndms.gndmc.test.gorfx.GORFXClientMain.java

private void getTaskFlowStatus(String id, String type, String wid) {
    Facets facets;/*from  www.j a v  a  2s  .  com*/
    Facet f;
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:META-INF/client-context.xml");
    TaskFlowClient tfClient = (TaskFlowClient) context.getAutowireCapableBeanFactory()
            .createBean(TaskFlowClient.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    tfClient.setServiceURL(gndmsEpUrl);

    System.out.println("Step 6: requesting facets of task flow, retrieving status results");
    ResponseEntity<Facets> res6 = tfClient.getFacets(type, id, dn);
    // should define facets status, result, errors
    facets = res6.getBody();
    f = facets.findFacet("status");
    if (f == null) {
        System.out.println("Status facet not found");
    } else {
        System.out.println(f.getName() + " " + f.getUrl());
        ResponseEntity<TaskFlowStatus> res7 = tfClient.getStatus(type, id, dn, wid);
        System.out.println(res7.getBody().toString());
    }

    f = facets.findFacet("result");
    if (f == null) {
        System.out.println("Result facet not found");
    } else {
        System.out.println(f.getName() + " " + f.getUrl());
        ResponseEntity<Specifier<TaskResult>> res8 = tfClient.getResult(type, id, dn, wid);
        System.out.println(res8.getBody().toString());
    }

    f = facets.findFacet("errors");
    if (f == null) {
        System.out.println("Errors facet not found");
    } else {
        System.out.println(f.getName() + " " + f.getUrl());
        ResponseEntity<TaskFlowFailure> res9 = tfClient.getErrors(type, id, dn, wid);
        if (res9.getStatusCode().equals(HttpStatus.OK)) {
            System.out.println("Failure" + res9.getBody().getFailureMessage());
        } else {
            System.out.println("No errors");

        }
    }
}

From source file:com.mastercard.test.spring.security.SpringSecurityJUnit4ClassRunner.java

/**
 * Construct a new WithSecurityContextFactory for the provided class name.
 * @param clazz The name of the class implementing the WithSecurityContextFactory interface.
 * @return The instance if it could be constructed, otherwise null.
 *///from w w w .j  ava 2s  .c  o  m
private WithSecurityContextFactory buildWithSecurityContextFactory(
        Class<? extends WithSecurityContextFactory<? extends Annotation>> clazz) {
    WithSecurityContextFactory retVal;

    ApplicationContext context = getApplicationContext(getTestClass().getJavaClass());

    try {
        retVal = context.getAutowireCapableBeanFactory().createBean(clazz);
    } catch (IllegalStateException e) {
        return BeanUtils.instantiateClass(clazz);
    } catch (Exception e) {
        throw new RuntimeException("Unable to construct an instance of " + clazz.getName(), e);
    }

    return retVal;
}

From source file:org.solmix.runtime.support.spring.SpringConfigurer.java

public synchronized void configureBean(String bn, Object beanInstance, boolean checkWildcards) {

    if (null == appContexts) {
        return;//from   w ww  .  ja va 2 s. c  om
    }

    if (null == bn) {
        bn = getBeanName(beanInstance);
    }

    if (null == bn) {
        return;
    }
    //configure bean with * pattern style.
    if (checkWildcards) {
        configureWithWildCard(bn, beanInstance);
    }

    final String beanName = bn;
    setBeanWiringInfoResolver(new BeanWiringInfoResolver() {
        @Override
        public BeanWiringInfo resolveWiringInfo(Object instance) {
            if (!"".equals(beanName)) {
                return new BeanWiringInfo(beanName);
            }
            return null;
        }
    });

    for (ApplicationContext appContext : appContexts) {
        if (appContext.containsBean(bn)) {
            this.setBeanFactory(appContext.getAutowireCapableBeanFactory());
        }
    }

    try {
        //this will prevent a call into the AbstractBeanFactory.markBeanAsCreated(...)
        //which saves ALL the names into a HashSet.  For URL based configuration,
        //this can leak memory
        if (beanFactory instanceof AbstractBeanFactory) {
            ((AbstractBeanFactory) beanFactory).getMergedBeanDefinition(bn);
        }
        super.configureBean(beanInstance);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Successfully performed injection,used beanName:{}", beanName);
        }
    } catch (NoSuchBeanDefinitionException ex) {
        // users often wonder why the settings in their configuration files seem
        // to have no effect - the most common cause is that they have been using
        // incorrect bean ids
        if (LOG.isDebugEnabled()) {
            LOG.debug("No matching bean {}", beanName);
        }
    }
}

From source file:org.solmix.runtime.support.spring.SpringConfigurer.java

/**
 * {@inheritDoc}//from   w  ww  . java  2  s  .c o  m
 * 
 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
 */
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
    appContexts = new CopyOnWriteArraySet<ApplicationContext>();
    addApplicationContext(ac);
    this.beanFactory = ac.getAutowireCapableBeanFactory();
    super.setBeanFactory(this.beanFactory);

}

From source file:org.solmix.runtime.support.spring.SpringConfigurer.java

private void initWildcardDefinitionMap() {
    if (null != appContexts) {
        for (ApplicationContext appContext : appContexts) {
            for (String n : appContext.getBeanDefinitionNames()) {
                if (isWildcardBeanName(n)) {
                    AutowireCapableBeanFactory bf = appContext.getAutowireCapableBeanFactory();
                    BeanDefinitionRegistry bdr = (BeanDefinitionRegistry) bf;
                    BeanDefinition bd = bdr.getBeanDefinition(n);
                    String className = bd.getBeanClassName();
                    if (null != className) {
                        String orig = n;
                        if (n.charAt(0) == '*') {
                            //old wildcard
                            n = "." + n.replaceAll("\\.", "\\.");
                        }//from  www.  j a  v a 2  s .  c  o  m
                        try {
                            Matcher matcher = Pattern.compile(n).matcher("");
                            List<MatcherHolder> m = wildCardBeanDefinitions.get(className);
                            if (m == null) {
                                m = new ArrayList<MatcherHolder>();
                                wildCardBeanDefinitions.put(className, m);
                            }
                            MatcherHolder holder = new MatcherHolder(orig, matcher);
                            m.add(holder);
                        } catch (PatternSyntaxException npe) {
                            //not a valid patter, we'll ignore
                        }
                    } else {
                        LOG.warn("Wildcars with not class {}", n);
                    }
                }
            }
        }
    }
}

From source file:com.longio.spring.LioBootstrap.java

private void resolveLservice(ApplicationContext app, DefaultListableBeanFactory bf) {
    for (String name : bf.getBeanDefinitionNames()) {
        AbstractBeanDefinition bd = (AbstractBeanDefinition) bf.getBeanDefinition(name);

        if (!bd.hasBeanClass()) {
            continue;
        }//from  w w  w.  jav a  2s .c om

        if (!name.endsWith("Service")) {
            continue;
        }

        Class<?> cls = bd.getBeanClass();
        if (cls != LioFactoryBean.class) {
            continue;
        }

        Object obj = app.getBean(name);
        app.getAutowireCapableBeanFactory().autowireBean(obj);
        Lservice ls = obj.getClass().getAnnotation(Lservice.class);
        if (ls != null) {
            String pkg = ls.path();
            Connector connector = getConnector(bf);
            for (Dispatcher d : connector.getDispatcheres(pkg)) {
                MethodRefFactory mrf = new DefaultMethodRefFactory(cmdLookup);
                d.registerMethodRefs(mrf.createMethodRefs(obj));
            }
            logger.info("load longio [" + pkg + "] service");
        }
        LsAutowired lsa = obj.getClass().getAnnotation(LsAutowired.class);
        if (lsa != null) {
            String pkg = lsa.path();
            logger.info("load longio  client [" + pkg + "] service");
        }

    }

}

From source file:com.longio.spring.LioBootstrap.java

private void resolveLfilters(ApplicationContext app, DefaultListableBeanFactory bf) {

    List<MessageFilter> filters = new ArrayList<MessageFilter>();
    for (String name : bf.getBeanDefinitionNames()) {
        AbstractBeanDefinition bd = (AbstractBeanDefinition) bf.getBeanDefinition(name);
        //System.out.println("+++++++++++++++" + name);
        if (!bd.hasBeanClass()) {
            continue;
        }/*from w  w w.  j ava 2  s .  com*/

        if (!name.endsWith("Filter")) {
            continue;
        }

        Class<?> cls = bd.getBeanClass();
        if (cls != LioFactoryBean.class) {
            continue;
        }

        Object obj = app.getBean(name);
        app.getAutowireCapableBeanFactory().autowireBean(obj);
        LsFilter lf = obj.getClass().getAnnotation(LsFilter.class);
        if (lf != null) {
            filters.add((MessageFilter) obj);
        }
    }

    Connector connector = getConnector(bf);
    for (Dispatcher d : connector.getDispatcheres("*")) {
        d.registerMessageFilters(filters);
        for (MessageFilter filter : filters) {
            logger.info("load longio [" + filter.getClass().getCanonicalName() + "] message filter");
        }

    }

}