今天小编给大家分享一下springMVC不扫描controller中的方法怎么解决的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

springMVC不扫描controller

最近把之前的一个Maven项目在一个新的电脑环境上导入Eclipse,启动时却发现不扫描 controller 中的方法

下面是正确的 spring-mvc.xml 文件

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><mvc:annotation-driven/><mvc:resourceslocation="/image/"mapping="/image/**"/><mvc:resourceslocation="/Content/"mapping="/Content/**"/><mvc:resourceslocation="/js/"mapping="/js/**"/><!--自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器--><context:component-scanbase-package="com.aven.weixiao.controller"/><!--避免IE执行AJAX时,返回JSON出现下载文件--><beanid="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><propertyname="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value></list></property></bean><!--启动SpringMVC的注解功能,完成请求和注解POJO的映射--><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><propertyname="messageConverters"><list><refbean="mappingJacksonHttpMessageConverter"/><!--JSON转换器--></list></property></bean><!--定义跳转的文件的前后缀,视图模式配置--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个可用的url地址--><propertyname="prefix"value="/"/><propertyname="suffix"value=".jsp"/></bean><!--配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包--><beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!--默认编码--><propertyname="defaultEncoding"value="utf-8"/><!--文件大小最大值--><propertyname="maxUploadSize"value="10485760000"/><!--内存中的最大值--><propertyname="maxInMemorySize"value="40960"/></bean></beans>那我遇到这个问题的原因是什么呢?

是因为新配置的环境,缺少很多 jar 包,所以项目导入Eclipse之后, 这个文件就报 “<mvc:annotation-driven />” 这一句有错了,

有错,我也没多想就先把它给删除了。

好吧,问题就这样产生了。

小结一下:在导入一个项目之后,可能会提示有很多错误,但针对一些配置文件,解决的方式不应该是删除或修改文件中的内容,

而应该先解决依赖等问题,不然像我这种之前没有问题的项目,就因为换了环境就产生怪问题。

springMVC包扫描问题为什么@COntroller要放在springMVC中?

@Controller注解的bean必须由DispatcherServlet初始化的children webApplicationContext来管理,在DispatcherServlet初始化的context中会扫描当前容器所有的bean实例,根据类级别以及方法级别的映射信息注解组装成对应的HandleMappering信息,但是ContextLoaderListener是不具备这个功能的。

一句话spring中没有办法扫描controller的bean,所以spring中可以扫描所有的,但是对于@controller不会骑作用,所以必须在springMVC中再加一次对controller的扫描。

PS:之前遇到一个事物的,一开始把@Transactional放在COntroller怎么都不起作用,原因是只在spring中配置了

<tx:annotation-driventransaction-manager="transactionManager"proxy-target-class="true"/>

而Controller没配,原因<tx:annoation-driven/>只会查找和它在相同的应用上下文件中定义的bean上面的@Transactional注解,如果你把它放在Dispatcher的应用上下文中,它只检查控制器(Controller)上的@Transactional注解,而不是你services上的@Transactional注解。

以上就是“springMVC不扫描controller中的方法怎么解决”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。