标签:ati return spring配置 line 快速 web应用 enable 可见性 事务
Spring和SpringMVC作为Bean管理容器和MVC层的默认框架,已被众多WEB应用采用,而实际使用时,由于有了强大的注解功能,很多基于XML的配置方式已经被替代,但是在实际项目中,同时配置Spring和SpringMVC时会出现一些奇怪的异常,比如Bean被多次加载,多次实例化,或者依赖注入时,Bean不能被自动注入,但是明明你已经将该Bean注册了的。找原因还是要看问题的根源,我们从容器说起。
1
|
<context:component-scan base- package =“com.test" /> |
HandlerMapping,是SpringMVC中用来处理Request请求URL到具体Controller的,其自身也分成很多种类;
HandlerAdapter,是SpringMVC中用来处理具体请求映射到具体方法的,其自身也分很多种类;
@RequestMapping这个注解的主要目的就是对具体的Controller和方法进行注册,以方便HandlerMapping用来处理请求的映射。但是@RequestMapping需要结合<mvc:annotation-driven />使用才能生效。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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()); } |
1
2
3
|
protected boolean isHandler(Class<?> beanType) { return AnnotationUtils.findAnnotation(beanType, Controller. class ) != null ; } |
1
2
3
4
5
|
<bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" > <property name= "detectHandlerMethodsInAncestorContexts" > <value> true </value> </property> </bean> |
Spring容器配置,排除所有@controller的Bean
1
2
3
|
<context:component-scan base- package = "com.fsnip.open" > <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Controller" /> </context:component-scan> |
SpringMVC容器配置,让其只包括@controller的Bean
1
2
3
|
<context:component-scan base- package = "com.fsnip.open" use- default -filters= "false" > <context:include-filter type= "annotation" expression= "org.springframework.stereotype.Controller" /> </context:component-scan> |
标签:ati return spring配置 line 快速 web应用 enable 可见性 事务
原文地址:http://www.cnblogs.com/shuo1208/p/6812522.html