SpringMvc学习心得(五)控制器产生与构建
![](http://static.blog.csdn.net/images/category_icon.jpg)
版权声明:本文为博主原创文章,未经博主允许不得转载。
在springmvc中,控制器(controller)是一个很重要的概念。在实际项目中,我们一般在控制器里完成具体的业务逻辑。控制器是非常重要,因此讨论控制器的产生和构建就变得很有意义(PS:我们在这里主要讨论基于注解的配置方式)。
在讨论控制器的相关问题之前,需要考虑的第一个问题是:ApplicationContext的类型是如何确定的?ApplicationContext是spring的IOC机制实现的一个核心,spring的很多功能都是通过ApplicationContext对外输出的。而springmvc的ApplicationContext则是“写死”在FrameworkServlet这个类的field中。
FrameworkServlet.Java
- public abstract class FrameworkServlet extends HttpServletBean {
- /**
- * Suffix for WebApplicationContext namespaces. If a servlet of this class is
- * given the name "test" in a context, the namespace used by the servlet will
- * resolve to "test-servlet".
- */
- public static final String DEFAULT_NAMESPACE_SUFFIX = "-servlet";
- /**
- * Default context class for FrameworkServlet.
- * @see org.springframework.web.context.support.XmlWebApplicationContext
- */
- public static final Class<?> DEFAULT_CONTEXT_CLASS = XmlWebApplicationContext.class;
- //some other code
- beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
- public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
- Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
- this.beanPostProcessors.remove(beanPostProcessor);
- this.beanPostProcessors.add(beanPostProcessor);
- if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
- this.hasInstantiationAwareBeanPostProcessors = true;
- }
- if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
- this.hasDestructionAwareBeanPostProcessors = true;
- }
- }
控制器的产生:打开@Controller这一注解的源代码,你会发现该注解会被@Component这一注解所修饰。因此,所有被@Controller所修饰的类都会默认被@Component所修饰。同时这也意味着搭配<context:component-scan base-package="xxx.xxxx.xxxx" />这一标签,所有被@Controller所修饰的类都会被注册成为JavaBean。这个JavaBean与其它自定义的JavaBean没有什么区别。而重要的区别则在于被@Controller所修饰的类,能够被之前注册的BeanPostProcessor所扫描并进行处理。
控制器的构建:springmvc将构建控制器的这一工作交给了BeanPostProcessor进行处理。在<mvc:annotation-driven/>这一标签里,springmvc创建了一个JavaBean,这个bean是RequestMappingHandlerMapping。当这个JavaBean被反射出来但是还没有被初始化的时候,BeanPostProcessor的postProcessBeforeInitialization会发挥作用。当然,由于会首先对JavaBean进行过滤。具体代码如下:
- if (bean instanceof ApplicationContextAware) {
- ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
- }
RequestMappingHandlerMapping扫描JavaBean并处理的方法:
- protected void initHandlerMethods() {
- if (logger.isDebugEnabled()) {
- logger.debug("Looking for request mappings in application context: " + getApplicationContext());
- }
- String[] beanNames = (this.detectHandlerMethodsInAncestorContexts ?
- BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
- getApplicationContext().getBeanNamesForType(Object.class));
- for (String beanName : beanNames) {
- if (isHandler(getApplicationContext().getType(beanName))){
- detectHandlerMethods(beanName);
- }
- }
- handlerMethodsInitialized(getHandlerMethods());
- }
- protected boolean isHandler(Class<?> beanType) {
- return AnnotationUtils.findAnnotation(beanType, Controller.class) != null;
- }
RequestMappingHandlerMapping中一个很重要的函数是detectHandlerMethods。该函数具体代码如下:
- protected void detectHandlerMethods(final Object handler) {
- Class<?> handlerType = (handler instanceof String) ?
- getApplicationContext().getType((String) handler) : handler.getClass();
- final Class<?> userType = ClassUtils.getUserClass(handlerType);
- Set<Method> methods = HandlerMethodSelector.selectMethods(userType, new MethodFilter() {
- public boolean matches(Method method) {
- return getMappingForMethod(method, userType) != null;
- }
- });
- for (Method method : methods) {
- T mapping = getMappingForMethod(method, userType);
- registerHandlerMethod(handler, method, mapping);
- }
- }
- protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
- RequestMappingInfo info = null;
- RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
- if (methodAnnotation != null) {
- RequestCondition<?> methodCondition = getCustomMethodCondition(method);
- info = createRequestMappingInfo(methodAnnotation, methodCondition);
- RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
- if (typeAnnotation != null) {
- RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType);
- info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
- }
- }
- return info;
- }
既然是以mapping为可以,那么必然会涉及equals函数和hashcode函数,下面是RequestMappingInfode 的equals函数以及hashcode函数。
- public int hashCode() {
- int result = hash;
- if (result == 0) {
- result = patternsCondition.hashCode();
- result = 31 * result + methodsCondition.hashCode();
- result = 31 * result + paramsCondition.hashCode();
- result = 31 * result + headersCondition.hashCode();
- result = 31 * result + consumesCondition.hashCode();
- result = 31 * result + producesCondition.hashCode();
- result = 31 * result + customConditionHolder.hashCode();
- hash = result;
- }
- return result;
- }
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj != null && obj instanceof RequestMappingInfo) {
- RequestMappingInfo other = (RequestMappingInfo) obj;
- return (this.patternsCondition.equals(other.patternsCondition) &&
- this.methodsCondition.equals(other.methodsCondition) &&
- this.paramsCondition.equals(other.paramsCondition) &&
- this.headersCondition.equals(other.headersCondition) &&
- this.consumesCondition.equals(other.consumesCondition) &&
- this.producesCondition.equals(other.producesCondition) &&
- this.customConditionHolder.equals(other.customConditionHolder));
- }
- return false;
- }
- 顶
- 0
- 踩