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

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

/**
 * Detect if the given handler has any methods that can handle messages and if
 * so register it with the extracted mapping information.
 * <p><strong>Note:</strong> This method is protected and can be invoked by
 * sub-classes, but this should be done on startup only as documented in
 * {@link #registerHandlerMethod}.
 * @param handler the handler to check, either an instance of a Spring bean name
 */
protected final void detectHandlerMethods(Object handler) {
	Class<?> handlerType;
	if (handler instanceof String) {
		ApplicationContext context = getApplicationContext();
		Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
		handlerType = context.getType((String) handler);
	}
	else {
		handlerType = handler.getClass();
	}
	if (handlerType != null) {
		final Class<?> userType = ClassUtils.getUserClass(handlerType);
		Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
				(MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
		if (logger.isDebugEnabled()) {
			logger.debug(formatMappings(userType, methods));
		}
		methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
	}
}
 
/**
 * Detect if the given handler has any methods that can handle messages and if
 * so register it with the extracted mapping information.
 * @param handler the handler to check, either an instance of a Spring bean name
 */
protected final void detectHandlerMethods(final Object handler) {
	Class<?> handlerType;
	if (handler instanceof String) {
		ApplicationContext context = getApplicationContext();
		Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
		handlerType = context.getType((String) handler);
	}
	else {
		handlerType = handler.getClass();
	}

	if (handlerType != null) {
		final Class<?> userType = ClassUtils.getUserClass(handlerType);
		Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
				(MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
		if (logger.isDebugEnabled()) {
			logger.debug(formatMappings(userType, methods));
		}
		methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
	}
}
 
/**
 * Detect if the given handler has any methods that can handle messages and if
 * so register it with the extracted mapping information.
 * @param handler the handler to check, either an instance of a Spring bean name
 */
protected final void detectHandlerMethods(final Object handler) {
	Class<?> handlerType;
	if (handler instanceof String) {
		ApplicationContext context = getApplicationContext();
		Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
		handlerType = context.getType((String) handler);
	}
	else {
		handlerType = handler.getClass();
	}

	if (handlerType != null) {
		final Class<?> userType = ClassUtils.getUserClass(handlerType);
		Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
				(MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
		if (logger.isDebugEnabled()) {
			logger.debug(methods.size() + " message handler methods found on " + userType + ": " + methods);
		}
		methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
	}
}
 
@Override
public void afterPropertiesSet() {
	if (this.argumentResolvers.getResolvers().isEmpty()) {
		this.argumentResolvers.addResolvers(initArgumentResolvers());
	}

	if (this.returnValueHandlers.getReturnValueHandlers().isEmpty()) {
		this.returnValueHandlers.addHandlers(initReturnValueHandlers());
	}
	Log returnValueLogger = getReturnValueHandlerLogger();
	if (returnValueLogger != null) {
		this.returnValueHandlers.setLogger(returnValueLogger);
	}

	this.handlerMethodLogger = getHandlerMethodLogger();

	ApplicationContext context = getApplicationContext();
	if (context == null) {
		return;
	}
	for (String beanName : context.getBeanNamesForType(Object.class)) {
		if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
			Class<?> beanType = null;
			try {
				beanType = context.getType(beanName);
			}
			catch (Throwable ex) {
				// An unresolvable bean type, probably from a lazy bean - let's ignore it.
				if (logger.isDebugEnabled()) {
					logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
				}
			}
			if (beanType != null && isHandler(beanType)) {
				detectHandlerMethods(beanName);
			}
		}
	}
}
 
@Override
public void afterPropertiesSet() {
	if (this.argumentResolvers.getResolvers().isEmpty()) {
		this.argumentResolvers.addResolvers(initArgumentResolvers());
	}

	if (this.returnValueHandlers.getReturnValueHandlers().isEmpty()) {
		this.returnValueHandlers.addHandlers(initReturnValueHandlers());
	}
	Log returnValueLogger = getReturnValueHandlerLogger();
	if (returnValueLogger != null) {
		this.returnValueHandlers.setLogger(returnValueLogger);
	}

	this.handlerMethodLogger = getHandlerMethodLogger();

	ApplicationContext context = getApplicationContext();
	if (context == null) {
		return;
	}
	for (String beanName : context.getBeanNamesForType(Object.class)) {
		if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
			Class<?> beanType = null;
			try {
				beanType = context.getType(beanName);
			}
			catch (Throwable ex) {
				// An unresolvable bean type, probably from a lazy bean - let's ignore it.
				if (logger.isDebugEnabled()) {
					logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
				}
			}
			if (beanType != null && isHandler(beanType)) {
				detectHandlerMethods(beanName);
			}
		}
	}
}
 
源代码6 项目: Mykit   文件: SpringRedisCacheManager.java
private void parseCacheDuration(ApplicationContext applicationContext) {
    final Map<String, Long> cacheExpires = new HashMap<String, Long>();
    String[] beanNames = applicationContext.getBeanNamesForType(Object.class);
    for (String beanName : beanNames) {
        final Class clazz = applicationContext.getType(beanName);
        Service service = findAnnotation(clazz, Service.class);
        if (null == service) {
            continue;
        }
        addCacheExpires(clazz, cacheExpires);
    }
    logger.info(cacheExpires.toString());
    //设置有效期
    super.setExpires(cacheExpires);
}
 
/**
 * Register all handlers specified in the Portlet mode map for the corresponding modes.
 * @throws org.springframework.beans.BeansException if the handler couldn't be registered
 */
protected void detectHandlers() throws BeansException {
	ApplicationContext context = getApplicationContext();
	String[] beanNames = context.getBeanNamesForType(Object.class);
	for (String beanName : beanNames) {
		Class<?> handlerType = context.getType(beanName);
		RequestMapping mapping = context.findAnnotationOnBean(beanName, RequestMapping.class);
		if (mapping != null) {
			// @RequestMapping found at type level
			String[] modeKeys = mapping.value();
			String[] params = mapping.params();
			boolean registerHandlerType = true;
			if (modeKeys.length == 0 || params.length == 0) {
				registerHandlerType = !detectHandlerMethods(handlerType, beanName, mapping);
			}
			if (registerHandlerType) {
				AbstractParameterMappingPredicate predicate = new TypeLevelMappingPredicate(
						params, mapping.headers(), mapping.method());
				for (String modeKey : modeKeys) {
					registerHandler(new PortletMode(modeKey), beanName, predicate);
				}
			}
		}
		else if (AnnotationUtils.findAnnotation(handlerType, Controller.class) != null) {
			detectHandlerMethods(handlerType, beanName, mapping);
		}
	}
}
 
源代码8 项目: jdal   文件: UrlBeanNameUiMapping.java
/**
 * Lookup UI class from application context.
 * @param request request
 */
@Override
@SuppressWarnings("unchecked")
public Class<?extends UI> getUiClass(VaadinRequest request) {
	ApplicationContext ctx = VaadinUtils.getApplicationContext();
	String beanName = getBeanNameFromRequest(request);
	
	if (beanName != null && ctx.containsBean(beanName))
		return (Class<? extends UI>) ctx.getType(beanName);
	
	return null;
}
 
源代码9 项目: lams   文件: DefaultAnnotationHandlerMapping.java
/**
 * Checks for presence of the {@link org.springframework.web.bind.annotation.RequestMapping}
 * annotation on the handler class and on any of its methods.
 */
@Override
protected String[] determineUrlsForHandler(String beanName) {
	ApplicationContext context = getApplicationContext();
	Class<?> handlerType = context.getType(beanName);
	RequestMapping mapping = context.findAnnotationOnBean(beanName, RequestMapping.class);
	if (mapping != null) {
		// @RequestMapping found at type level
		this.cachedMappings.put(handlerType, mapping);
		Set<String> urls = new LinkedHashSet<String>();
		String[] typeLevelPatterns = mapping.value();
		if (typeLevelPatterns.length > 0) {
			// @RequestMapping specifies paths at type level
			String[] methodLevelPatterns = determineUrlsForHandlerMethods(handlerType, true);
			for (String typeLevelPattern : typeLevelPatterns) {
				if (!typeLevelPattern.startsWith("/")) {
					typeLevelPattern = "/" + typeLevelPattern;
				}
				boolean hasEmptyMethodLevelMappings = false;
				for (String methodLevelPattern : methodLevelPatterns) {
					if (methodLevelPattern == null) {
						hasEmptyMethodLevelMappings = true;
					}
					else {
						String combinedPattern = getPathMatcher().combine(typeLevelPattern, methodLevelPattern);
						addUrlsForPath(urls, combinedPattern);
					}
				}
				if (hasEmptyMethodLevelMappings ||
						org.springframework.web.servlet.mvc.Controller.class.isAssignableFrom(handlerType)) {
					addUrlsForPath(urls, typeLevelPattern);
				}
			}
			return StringUtils.toStringArray(urls);
		}
		else {
			// actual paths specified by @RequestMapping at method level
			return determineUrlsForHandlerMethods(handlerType, false);
		}
	}
	else if (AnnotationUtils.findAnnotation(handlerType, Controller.class) != null) {
		// @RequestMapping to be introspected at method level
		return determineUrlsForHandlerMethods(handlerType, false);
	}
	else {
		return null;
	}
}
 
/**
 * Checks for presence of the {@link org.springframework.web.bind.annotation.RequestMapping}
 * annotation on the handler class and on any of its methods.
 */
@Override
protected String[] determineUrlsForHandler(String beanName) {
	ApplicationContext context = getApplicationContext();
	Class<?> handlerType = context.getType(beanName);
	RequestMapping mapping = context.findAnnotationOnBean(beanName, RequestMapping.class);
	if (mapping != null) {
		// @RequestMapping found at type level
		this.cachedMappings.put(handlerType, mapping);
		Set<String> urls = new LinkedHashSet<String>();
		String[] typeLevelPatterns = mapping.value();
		if (typeLevelPatterns.length > 0) {
			// @RequestMapping specifies paths at type level
			String[] methodLevelPatterns = determineUrlsForHandlerMethods(handlerType, true);
			for (String typeLevelPattern : typeLevelPatterns) {
				if (!typeLevelPattern.startsWith("/")) {
					typeLevelPattern = "/" + typeLevelPattern;
				}
				boolean hasEmptyMethodLevelMappings = false;
				for (String methodLevelPattern : methodLevelPatterns) {
					if (methodLevelPattern == null) {
						hasEmptyMethodLevelMappings = true;
					}
					else {
						String combinedPattern = getPathMatcher().combine(typeLevelPattern, methodLevelPattern);
						addUrlsForPath(urls, combinedPattern);
					}
				}
				if (hasEmptyMethodLevelMappings ||
						org.springframework.web.servlet.mvc.Controller.class.isAssignableFrom(handlerType)) {
					addUrlsForPath(urls, typeLevelPattern);
				}
			}
			return StringUtils.toStringArray(urls);
		}
		else {
			// actual paths specified by @RequestMapping at method level
			return determineUrlsForHandlerMethods(handlerType, false);
		}
	}
	else if (AnnotationUtils.findAnnotation(handlerType, Controller.class) != null) {
		// @RequestMapping to be introspected at method level
		return determineUrlsForHandlerMethods(handlerType, false);
	}
	else {
		return null;
	}
}