Java Code Examples for org.springframework.context.ApplicationContext#getType()

The following examples show how to use org.springframework.context.ApplicationContext#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractMethodMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 2
Source File: AbstractMethodMessageHandler.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 3
Source File: AbstractMethodMessageHandler.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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));
	}
}
 
Example 4
Source File: AbstractMethodMessageHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
			}
		}
	}
}
 
Example 5
Source File: AbstractMethodMessageHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
			}
		}
	}
}
 
Example 6
Source File: SpringRedisCacheManager.java    From Mykit with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: DefaultAnnotationHandlerMapping.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
		}
	}
}
 
Example 8
Source File: UrlBeanNameUiMapping.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: DefaultAnnotationHandlerMapping.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
	}
}
 
Example 10
Source File: DefaultAnnotationHandlerMapping.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
	}
}