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:org.constretto.spring.namespacehandler.ImportWithSpecifiedResolverTest.java

@Test
public void givenDevelopmentEnvironmentTheCorrectImportsAreProcessed() {
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
            "org/constretto/spring/namespacehandler/ImportWithSpecifiedResolverTest-context.xml");
    Assert.assertTrue(ctx.containsBean("inMainConfig"));
    Assert.assertFalse(ctx.containsBean("inJunitConfig"));
    Assert.assertTrue(ctx.containsBean("inDevelopmentConfig"));
}

From source file:org.trpr.platform.batch.impl.job.ha.service.CuratorJobSyncHandlerFactory.java

/**
 * Interface method implementation. //w ww  . j  a v  a 2s  .com
 * Constructs and returns as instance of {@link CuratorJobSyncHandler} for Trooper batch runtime.
 * @see org.springframework.beans.factory.FactoryBean#getObject()
 */
@Override
public CuratorJobSyncHandler getObject() throws Exception {
    ApplicationContext context = SpringBatchComponentContainer.getCommonBatchBeansContext();
    if (!context.containsBean(this.SYNC_HANDLER_BEAN_NAME)) {
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context
                .getAutowireCapableBeanFactory();
        BeanDefinitionBuilder builder = BeanDefinitionBuilder
                .rootBeanDefinition(CuratorJobSyncHandler.class.getName())
                .addConstructorArgValue(jobConfigurationService).addConstructorArgValue(curatorFramework)
                .addConstructorArgValue(bootstrapMonitorBean);
        beanFactory.registerBeanDefinition(this.SYNC_HANDLER_BEAN_NAME, builder.getBeanDefinition());
    }
    return (CuratorJobSyncHandler) context.getBean(this.SYNC_HANDLER_BEAN_NAME);
}

From source file:be.hikage.springtemplate.TemplateTest.java

@Test
public void testGood() {

    ApplicationContext context = new ClassPathXmlApplicationContext("test-config-good.xml", this.getClass());
    assertTrue("Bean simple-dev must be defined", context.containsBean("simple-dev"));
    assertEquals(SimpleBean.class, context.getBean("simple-dev").getClass());
    SimpleBean simpleBean = (SimpleBean) context.getBean("simple-dev");

    assertEquals("constructorData.dev", simpleBean.getConstructorValue());
    assertEquals("ExternalizedConstructor", simpleBean.getExternalizedConstructorValue());

    assertEquals("propertyData.dev", simpleBean.getPropertyValue());
    assertEquals("ExternalizedProperty", simpleBean.getExternalizedPropertyValue());

    assertTrue("Bean container-dev must be defined", context.containsBean("container-dev"));
    assertEquals(ContainerBean.class, context.getBean("container-dev").getClass());

    ConfigurableApplicationContext listableBeanFactory = (ConfigurableApplicationContext) context;
    RootBeanDefinition beanDefinition = (RootBeanDefinition) listableBeanFactory.getBeanFactory()
            .getBeanDefinition("container-dev");
    assertArrayEquals(new String[] { "simple-dev" }, beanDefinition.getDependsOn());

}

From source file:nl.tjonahen.javaee7.cdi.springbridge.springsupport.ApplicationContextLocatorImpl.java

/**
 * Constructor.//from w w w . j  av a 2  s  .  c  o m
 */
public ApplicationContextLocatorImpl() {
    final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    final WeakReference<ApplicationContext> reference = MAP.get(contextClassLoader);

    if (reference == null || reference.get() == null) {
        throw new IllegalStateException(
                "Application context must be associated with the current classloader first");
    }
    /* 
     * To prevent GC from killing this until web app is gone. 
     */
    final ApplicationContext applicationContext = reference.get();
    final String beanName = ApplicationContextLocator.class.getName();
    if (!applicationContext.containsBean(beanName)) {
        ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) applicationContext)
                .getBeanFactory();
        beanFactory.registerSingleton(beanName, this);
    }

}

From source file:org.ops4j.pax.carrot.spring.SpringTestContextFixtureLoader.java

private Object lookupBeanByName(String fixtureName) throws Exception {

    if (context.getConfiguration() instanceof Class<?>) {
        Class<?> configClass = (Class<?>) context.getConfiguration();
        ContextConfiguration cc = configClass.getAnnotation(ContextConfiguration.class);
        if (cc != null) {
            TestContextManager contextManager = new TestContextManager(configClass);
            ApplicationContextHolder applicationContextHolder = new ApplicationContextHolder();
            contextManager.registerTestExecutionListeners(applicationContextHolder);
            contextManager.prepareTestInstance(configClass.newInstance());
            ApplicationContext applicationContext = applicationContextHolder.getApplicationContext();
            if (applicationContext.containsBean(fixtureName)) {
                return applicationContext.getBean(fixtureName);
            }/*from  w  ww . j a  v a2  s . c  o  m*/
        }
    }
    return null;
}

From source file:com.vaadin.terminal.gwt.server.SpringApplicationOSGiServlet.java

@Override
protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {
    ApplicationContext springContext = getApplicationContext();

    if (!springContext.containsBean(beanParam)) {

        throw new ClassNotFoundException("No application bean found under name " + beanParam);
    }// w  w  w .j  a  va 2  s. c  o  m

    return (Class<? extends Application>) springContext.getBean(beanParam).getClass();
}

From source file:com.vaadin.terminal.gwt.server.SpringApplicationOSGiServlet.java

@Override
protected Application getNewApplication(HttpServletRequest request) throws ServletException {

    // Creates a new application instance
    try {// www. ja  v  a 2  s  .c  om
        // Retrieve the Spring ApplicationContext registered as a service
        ApplicationContext springContext = getApplicationContext();

        if (!springContext.containsBean(beanParam)) {

            throw new ClassNotFoundException("No application bean found under name " + beanParam);
        }

        final Application application = (Application) springContext.getBean(beanParam);

        return application;

    } catch (ClassNotFoundException e) {
        throw new ServletException("getNewApplication failed", e);
    }
}

From source file:org.grails.plugins.zkui.metaclass.RedirectDynamicMethod.java

/**
 * Constructor./*from w  w w. j a  v a  2  s. c o  m*/
 *
 * @param applicationContext
 */
public RedirectDynamicMethod(ApplicationContext applicationContext) {
    super(METHOD_PATTERN);

    if (applicationContext.containsBean(UrlMappingsHolder.BEAN_ID)) {
        urlMappingsHolder = (UrlMappingsHolder) applicationContext.getBean(UrlMappingsHolder.BEAN_ID);
    }

    GrailsApplication application = (GrailsApplication) applicationContext
            .getBean(GrailsApplication.APPLICATION_ID);
    Object o = application.getFlatConfig().get(GRAILS_VIEWS_ENABLE_JSESSIONID);
    if (o instanceof Boolean) {
        useJessionId = (Boolean) o;
    }
    this.applicationContext = applicationContext;
}

From source file:org.activequant.util.spring.ServiceLocator.java

/**
 * Gets the Spring ApplicationContext.//  w  ww  .  j  av a 2s  .  c  om
 */
public synchronized ApplicationContext getContext() {
    if (this.beanFactoryReference == null) {
        // init defaults
        if (this.beanFactoryReferenceLocation == null) {
            this.beanFactoryReferenceLocation = DEFAULT_BEAN_REFERENCE_LOCATION;
        }
        if (this.beanRefFactoryReferenceId == null) {
            this.beanRefFactoryReferenceId = DEFAULT_BEAN_REFERENCE_ID;
        }
        // init context
        ApplicationContext context = resolveApplicationContext(beanFactoryReferenceLocation);

        // detect factory
        if (context.containsBean(DEFAULT_BEAN_REFERENCE_ID)) {
            // use bean factory (factory bean defined)
            beanFactoryReference = new ContextBeanFactoryReference(
                    (ApplicationContext) context.getBean(DEFAULT_BEAN_REFERENCE_ID));
        } else {
            // use context directly (no factory bean defined)
            beanFactoryReference = new ContextBeanFactoryReference(context);
        }
    }
    return (ApplicationContext) this.beanFactoryReference.getFactory();
}

From source file:ch.rasc.wampspring.method.PayloadArgumentResolver.java

public PayloadArgumentResolver(ApplicationContext applicationContext,
        MethodParameterConverter methodParameterConverter) {
    this.methodParameterConverter = methodParameterConverter;

    if (applicationContext.containsBean(MVC_VALIDATOR_NAME)) {
        this.validator = applicationContext.getBean(MVC_VALIDATOR_NAME, Validator.class);
    } else if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
        Class<?> clazz;//  w  ww. j  a va 2 s.c  o m
        try {
            String className = "org.springframework.validation.beanvalidation.OptionalValidatorFactoryBean";
            clazz = ClassUtils.forName(className, AbstractMessageBrokerConfiguration.class.getClassLoader());
        } catch (Throwable ex) {
            throw new BeanInitializationException("Could not find default validator class", ex);
        }
        this.validator = (Validator) BeanUtils.instantiate(clazz);
    } else {
        this.validator = new Validator() {
            @Override
            public boolean supports(Class<?> clazz) {
                return false;
            }

            @Override
            public void validate(Object target, Errors errors) {
                // nothing here
            }
        };
    }
}