Example usage for org.springframework.context ApplicationContext containsBean

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

Introduction

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

Prototype

boolean containsBean(String name);

Source Link

Document

Does this bean factory contain a bean definition or externally registered singleton instance with the given name?

Usage

From source file:net.javacrumbs.springws.test.helper.DefaultStrategiesHelperFactory.java

public static WebServiceMessageFactory createMessageFactory(ApplicationContext applicationContext) {
    if (applicationContext != null && applicationContext.containsBean(DEFAULT_MESSAGE_FACTORY_BEAN_NAME)) {
        return (WebServiceMessageFactory) applicationContext.getBean(DEFAULT_MESSAGE_FACTORY_BEAN_NAME,
                WebServiceMessageFactory.class);
    } else {//from  ww  w.ja v  a 2 s.  co  m
        logger.debug("No WebServiceMessageFactory found, using default");
        return (WebServiceMessageFactory) getDefaultStrategiesHelper()
                .getDefaultStrategy(WebServiceMessageFactory.class, applicationContext);
    }
}

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

/**Container,container?*/
public static Container addDefault(ApplicationContext ctx) {
    if (!ctx.containsBean(Container.DEFAULT_CONTAINER_ID)) {
        Container b = getContainerByName(Container.DEFAULT_CONTAINER_ID, ctx, true);
        if (ctx instanceof ConfigurableApplicationContext) {
            ConfigurableApplicationContext cctx = (ConfigurableApplicationContext) ctx;
            new ContainerPostProcessor(b).postProcessBeanFactory(cctx.getBeanFactory());
        }/*from   w  w  w  . j  a  va2 s  .  com*/
    }
    return Container.class.cast(ctx.getBean(Container.DEFAULT_CONTAINER_ID, Container.class));
}

From source file:org.carewebframework.api.spring.SpringUtil.java

/**
 * Returns the bean with an id matching the specified id, or null if none found.
 * /*  ww w.ja v a 2  s .  co  m*/
 * @param id Bean id
 * @return Returns the bean instance whose id matches the specified id, or null if none found or
 *         if the application context cannot be determined.
 */
public static Object getBean(final String id) {
    final ApplicationContext appContext = getAppContext();
    return appContext == null ? null : appContext.containsBean(id) ? appContext.getBean(id) : null;
}

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

private static Container getContainerByName(String name, ApplicationContext context, boolean create) {
    if (!context.containsBean(name) && (create || Container.DEFAULT_CONTAINER_ID.equals(name))) {
        SpringContainer b = new SpringContainer();
        ConfigurableApplicationContext cctx = (ConfigurableApplicationContext) context;
        cctx.getBeanFactory().registerSingleton(name, b);
        b.setApplicationContext(context);
    }//from   w  w  w. j  a va  2  s.c  om
    return context.getBean(name, Container.class);
}

From source file:org.carewebframework.api.spring.SpringUtil.java

/**
 * Returns the bean with an id matching the specified id, or null if none found.
 * /* ww w . j  av a  2  s . co m*/
 * @param id Bean id
 * @param clazz Expected return type.
 * @return Returns the bean instance whose id matches the specified id, or null if none found or
 *         if the application context cannot be determined.
 */
public static <T> T getBean(String id, Class<T> clazz) {
    ApplicationContext appContext = getAppContext();
    return appContext == null ? null
            : appContext.containsBean(id) && appContext.isTypeMatch(id, clazz) ? appContext.getBean(id, clazz)
                    : null;
}

From source file:org.red5.server.api.ScopeUtils.java

/**
 * Returns scope services (e.g. SharedObject, etc) for the scope. Method
 * uses either bean name passes as a string or class object.
 *
 * @param scope/*from w  w  w .ja  va2  s .co  m*/
 *            The scope service belongs to
 * @param name
 *            Bean name
 * @param defaultClass
 *            Class of service
 * @return            Service object
 */
protected static Object getScopeService(IScope scope, String name, Class defaultClass) {
    if (scope == null) {
        return null;
    }

    final IContext context = scope.getContext();
    ApplicationContext appCtx = context.getApplicationContext();
    Object result;
    if (!appCtx.containsBean(name)) {
        if (defaultClass == null) {
            return null;
        }

        try {
            result = defaultClass.newInstance();
        } catch (Exception e) {
            log.error(e);
            return null;
        }
    } else {
        result = appCtx.getBean(name);
    }

    return result;
}

From source file:org.red5.server.util.ScopeUtils.java

/**
 * Returns scope services (e.g. SharedObject, etc) for the scope. Method uses either bean name passes as a string or class object.
 *
 * @param scope/*  w w w  . ja  v  a 2 s.  co  m*/
 *            The scope service belongs to
 * @param name
 *            Bean name
 * @param defaultClass
 *            Class of service
 * @return Service object
 */
protected static Object getScopeService(IScope scope, String name, Class<?> defaultClass) {
    if (scope != null) {
        final IContext context = scope.getContext();
        ApplicationContext appCtx = context.getApplicationContext();
        Object result;
        if (!appCtx.containsBean(name)) {
            if (defaultClass == null) {
                return null;
            }
            try {
                result = defaultClass.newInstance();
            } catch (Exception e) {
                log.error("{}", e);
                return null;
            }
        } else {
            result = appCtx.getBean(name);
        }
        return result;
    }
    return null;
}

From source file:net.sourceforge.vulcan.spring.DelegatingResourceBundleMessageSource.java

public void addDelegate(ApplicationContext context) {
    if (context.containsBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME)) {
        MessageSource msgSrc = (MessageSource) context
                .getBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME);
        if (msgSrc instanceof AbstractMessageSource) {
            final AbstractMessageSource abstractMessageSource = (AbstractMessageSource) msgSrc;
            abstractMessageSource.setUseCodeAsDefaultMessage(false);
        }//from  w  w w  .ja  va 2s.  c  o m
    }

    delegates.add(context);
}

From source file:de.forsthaus.CleanDemoDataParsingJob.java

/**
 * Resets the admin user name and password because some guys are changing
 * these names and forgot to reset to the original values. :-(
 *//*from   www .  j  av a  2 s .co  m*/
private void doResetAdminUser() {
    System.out.println("###### ==> Run Job for resetting the demo data.");

    ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();

    if (ctx != null && ctx.containsBean("userService")) {
        NonWebRequestDBAction nonWebRequestDBAction = (NonWebRequestDBAction) ctx
                .getBean("nonWebRequestDBAction");
        if (nonWebRequestDBAction != null) {
            System.out.println("###### ==> Reset admin name/password.");
            nonWebRequestDBAction.resetAdminPassword();
        }
    }
}

From source file:org.soulwing.cas.filter.FilterToBeanProxy.java

private Filter getTargetBean(ApplicationContext appContext, String beanName) throws ServletException {
    if (appContext.containsBean(beanName)) {
        Object bean = appContext.getBean(beanName);
        if (bean instanceof Filter) {
            return (Filter) bean;
        } else {/*from w w  w  .j av  a  2s  .c om*/
            throw new ServletException(bean.getClass().getCanonicalName() + MUST_IMPLEMENT_FILTER);
        }
    } else {
        throw new ServletException(beanName + BEAN_NOT_FOUND);
    }
}