org.springframework.context.ApplicationContext#containsBean ( )源码实例Demo

下面列出了org.springframework.context.ApplicationContext#containsBean ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: red5-server-common   文件: 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
 *            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.getDeclaredConstructor().newInstance();
            } catch (Exception e) {
                log.error("{}", e);
                return null;
            }
        } else {
            result = appCtx.getBean(name);
        }
        return result;
    }
    return null;
}
 
源代码2 项目: lams   文件: BeanNameViewResolver.java
@Override
public View resolveViewName(String viewName, Locale locale) throws BeansException {
	ApplicationContext context = getApplicationContext();
	if (!context.containsBean(viewName)) {
		if (logger.isDebugEnabled()) {
			logger.debug("No matching bean found for view name '" + viewName + "'");
		}
		// Allow for ViewResolver chaining...
		return null;
	}
	if (!context.isTypeMatch(viewName, View.class)) {
		if (logger.isDebugEnabled()) {
			logger.debug("Found matching bean for view name '" + viewName +
					"' - to be ignored since it does not implement View");
		}
		// Since we're looking into the general ApplicationContext here,
		// let's accept this as a non-match and allow for chaining as well...
		return null;
	}
	return context.getBean(viewName, View.class);
}
 
@Override
public View resolveViewName(String viewName, Locale locale) throws BeansException {
	ApplicationContext context = getApplicationContext();
	if (!context.containsBean(viewName)) {
		if (logger.isDebugEnabled()) {
			logger.debug("No matching bean found for view name '" + viewName + "'");
		}
		// Allow for ViewResolver chaining...
		return null;
	}
	if (!context.isTypeMatch(viewName, View.class)) {
		if (logger.isDebugEnabled()) {
			logger.debug("Found matching bean for view name '" + viewName +
					"' - to be ignored since it does not implement View");
		}
		// Since we're looking into the general ApplicationContext here,
		// let's accept this as a non-match and allow for chaining as well...
		return null;
	}
	return context.getBean(viewName, View.class);
}
 
源代码4 项目: spring-analysis-note   文件: AbstractView.java
/**
 * Return the {@link RequestDataValueProcessor} to use.
 * <p>The default implementation looks in the {@link #getApplicationContext()
 * Spring configuration} for a {@code RequestDataValueProcessor} bean with
 * the name {@link #REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME}.
 * @return the RequestDataValueProcessor, or null if there is none at the
 * application context.
 */
@Nullable
protected RequestDataValueProcessor getRequestDataValueProcessor() {
	ApplicationContext context = getApplicationContext();
	if (context != null && context.containsBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		return context.getBean(REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
	return null;
}
 
private Validator getConfigurationPropertiesValidator(
        ApplicationContext applicationContext, String validatorBeanName) {
    if (applicationContext.containsBean(validatorBeanName)) {
        return applicationContext.getBean(validatorBeanName, Validator.class);
    }
    return null;
}
 
/**
 * Loads this executor into the ApplicationContext, if it
 *  isn't already there
 */
public static void registerIfNeeded(ApplicationContext ctx)
{
   if(!ctx.containsBean(ENGINE_NAME))
   {
      // Create, and do dependencies
      DummyHelloWorldRenditionEngine hw = new DummyHelloWorldRenditionEngine();
      hw.setRuntimeActionService(
            (RuntimeActionService)ctx.getBean("actionService")
      );
      hw.setNodeService(
            (NodeService)ctx.getBean("NodeService")
      );
      hw.setContentService(
            (ContentService)ctx.getBean("ContentService")
      );
      hw.setRenditionService(
            (RenditionService)ctx.getBean("RenditionService")
      );
      hw.setBehaviourFilter(
            (BehaviourFilter)ctx.getBean("policyBehaviourFilter")
      );
      hw.setRenditionLocationResolver(
            (RenditionLocationResolver)ctx.getBean("renditionLocationResolver")
      );
      
      // Register
       ((ConfigurableApplicationContext) ctx).getBeanFactory().registerSingleton(
            ENGINE_NAME, hw
      );
      hw.init();
   }
}
 
/**
* Loads this executor into the ApplicationContext, if it isn't already there
*/
public static void registerIfNeeded(ApplicationContext ctx)
{
    if (!ctx.containsBean(SleepActionExecuter.NAME))
    {
        // Create, and do dependencies
        SleepActionExecuter executor = new SleepActionExecuter();
        executor.setTrackStatus(true);
        executor.actionTrackingService = (ActionTrackingService) ctx.getBean("actionTrackingService");
        // Register
        ((ConfigurableApplicationContext) ctx).getBeanFactory().registerSingleton(SleepActionExecuter.NAME, executor);
    }
}
 
源代码8 项目: urule   文件: KnowledgeServiceImpl.java
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	boolean hasBean=applicationContext.containsBean(KnowledgePackageService.BEAN_ID);
	if(hasBean){
		knowledgePackageService=(KnowledgePackageService)applicationContext.getBean(KnowledgePackageService.BEAN_ID);		
	}
}
 
源代码9 项目: dubbox   文件: SpringExtensionFactory.java
@SuppressWarnings("unchecked")
public <T> T getExtension(Class<T> type, String name) {
    for (ApplicationContext context : contexts) {
        if (context.containsBean(name)) {
            Object bean = context.getBean(name);
            if (type.isInstance(bean)) {
                return (T) bean;
            }
        }
    }
    return null;
}
 
源代码10 项目: dubbox-hystrix   文件: SpringExtensionFactory.java
@SuppressWarnings("unchecked")
public <T> T getExtension(Class<T> type, String name) {
    for (ApplicationContext context : contexts) {
        if (context.containsBean(name)) {
            Object bean = context.getBean(name);
            if (type.isInstance(bean)) {
                return (T) bean;
            }
        }
    }
    return null;
}
 
源代码11 项目: nextreports-server   文件: NextServerApplication.java
public Object getSpringBean(String beanName) {
	ApplicationContext applicationContext = WebApplicationContextUtils
			.getWebApplicationContext(getServletContext());
	if (!applicationContext.containsBean(beanName)) {
		return null;
	}

	return applicationContext.getBean(beanName);
}
 
源代码12 项目: dubbo3   文件: SpringExtensionFactory.java
@SuppressWarnings("unchecked")
public <T> T getExtension(Class<T> type, String name) {
    for (ApplicationContext context : contexts) {
        if (context.containsBean(name)) {
            Object bean = context.getBean(name);
            if (type.isInstance(bean)) {
                return (T) bean;
            }
        }
    }
    return null;
}
 
源代码13 项目: dubbox   文件: SpringExtensionFactory.java
@SuppressWarnings("unchecked")
public <T> T getExtension(Class<T> type, String name) {
    for (ApplicationContext context : contexts) {
        if (context.containsBean(name)) {
            Object bean = context.getBean(name);
            if (type.isInstance(bean)) {
                return (T) bean;
            }
        }
    }
    return null;
}
 
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(applicationContext);
    String dsName = ConnectionSource.DEFAULT.equals(dataSourceName) ? "dataSource" : "dataSource_" + dataSourceName;
    Properties properties = getProperties();

    if(applicationContext.containsBean(dsName)) {
        properties.put(Environment.DATASOURCE, applicationContext.getBean(dsName));
    }
    properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, currentSessionContext.getName());
    properties.put(AvailableSettings.CLASSLOADERS, applicationContext.getClassLoader());
}
 
源代码15 项目: cuba   文件: LogMdc.java
public static void setup(SecurityContext securityContext) {
    String userProp = AppContext.getProperty("cuba.logUserName");
    if (userProp == null || Boolean.valueOf(userProp)) {
        if (securityContext != null) {
            String user = securityContext.getUser();
            if (user == null) {
                UserSession session = securityContext.getSession();
                if (session != null)
                    user = session.getUser().getLogin();
                else if (securityContext.getSessionId() != null) {
                    ApplicationContext applicationContext = AppContext.getApplicationContext();
                    if (applicationContext.containsBean("cuba_UserSessions")) {
                        UserSessionFinder sessionFinder = (UserSessionFinder) applicationContext.getBean("cuba_UserSessions");
                        session = sessionFinder.get(securityContext.getSessionId());
                        if (session != null) {
                            user = session.getUser().getLogin();
                        }
                    }
                }
            }
            if (user != null) {
                MDC.put(USER, "/" + user);
            }
        } else {
            MDC.remove(USER);
        }
    }

    String applicationProp = AppContext.getProperty("cuba.logAppName");
    if (applicationProp == null || Boolean.valueOf(applicationProp)) {
        if (securityContext != null) {
            MDC.put(APPLICATION, "/" + AppContext.getProperty("cuba.webContextName"));
        } else {
            MDC.remove(APPLICATION);
        }
    }
}
 
源代码16 项目: onetwo   文件: SocketBeanConfigurator.java
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
	T endpoint = null;
	ApplicationContext appContext = Springs.getInstance().getAppContext();
	Component ann = AnnotationUtils.findAnnotation(endpointClass, Component.class);
	if (ann != null && appContext.containsBean(ann.value())) {
		endpoint = appContext.getBean(ann.value(), endpointClass);
		return endpoint;
	}
	
	Map<String, T> socketEndpointMap = Springs.getInstance().getBeansMap(endpointClass);
	if(LangUtils.isEmpty(socketEndpointMap)){
		endpoint = ReflectUtils.newInstance(endpointClass);
		SpringUtils.injectAndInitialize(appContext, ReflectUtils.newInstance(endpointClass));
		return endpoint;
	}
	if(socketEndpointMap.size()==1){
		endpoint = LangUtils.getFirst(socketEndpointMap);
	}else{
		String beanName = ClassUtils.getShortNameAsProperty(endpointClass);
		if (socketEndpointMap.containsKey(beanName)) {
			endpoint = socketEndpointMap.get(beanName);
			return endpoint;
		}
	}
	return endpoint;
}
 
源代码17 项目: cxf   文件: BusWiringBeanFactoryPostProcessor.java
public static Bus addDefaultBus(ApplicationContext ctx) {
    if (!ctx.containsBean(Bus.DEFAULT_BUS_ID)) {
        Bus b = getBusForName(Bus.DEFAULT_BUS_ID, ctx, true);
        if (ctx instanceof ConfigurableApplicationContext) {
            ConfigurableApplicationContext cctx = (ConfigurableApplicationContext)ctx;
            new BusWiringBeanFactoryPostProcessor(b).postProcessBeanFactory(cctx.getBeanFactory());
        }
    }
    return Bus.class.cast(ctx.getBean(Bus.DEFAULT_BUS_ID, Bus.class));
}
 
源代码18 项目: cxf   文件: ConfigurerImpl.java
public synchronized void configureBean(String bn, Object beanInstance, boolean checkWildcards) {

        if (null == appContexts) {
            return;
        }

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

        if (checkWildcards) {
            configureWithWildCard(bn, beanInstance);
        }

        final String beanName = bn;
        setBeanWiringInfoResolver(new BeanWiringInfoResolver() {
            public BeanWiringInfo resolveWiringInfo(Object instance) {
                if (!beanName.isEmpty()) {
                    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.isLoggable(Level.FINE)) {
                LOG.fine("Successfully performed injection.");
            }
        } 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.isLoggable(Level.FINE)) {
                LOG.log(Level.FINE, "NO_MATCHING_BEAN_MSG", beanName);
            }
        }
    }
 
源代码19 项目: olat   文件: CoreSpringFactory.java
/**
 * @param beanName
 *            The bean name to check for
 * @return true if such a bean does exist, false otherwhise. But if such a bean definition exists it will get created! Use the containsSingleton to check for lazy
 *         init beans whether they are instantiated or not.
 */
public static boolean containsBean(String beanName) {
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(CoreSpringFactory.servletContext);
    return context.containsBean(beanName);
}
 
源代码20 项目: olat   文件: CoreSpringFactory.java
/**
 * @param beanName
 *            The bean name to check for
 * @return true if such a bean does exist, false otherwhise. But if such a bean definition exists it will get created! Use the containsSingleton to check for lazy
 *         init beans whether they are instantiated or not.
 */
public static boolean containsBean(String beanName) {
    ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(CoreSpringFactory.servletContext);
    return context.containsBean(beanName);
}