springMVC不掃描controller中的方法問題
springMVC不掃描controller
最近把之前的一個Maven項目在一個新的電腦環(huán)境上導(dǎo)入Eclipse,啟動時卻發(fā)現(xiàn)不掃描 controller 中的方法
下面是正確的 spring-mvc.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven />
<mvc:resources location="/image/" mapping="/image/**" />
<mvc:resources location="/Content/" mapping="/Content/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!-- 自動掃描該包,使SpringMVC認為包下用了@controller注解的類是控制器 -->
<context:component-scan base-package="com.aven.weixiao.controller" />
<!--避免IE執(zhí)行AJAX時,返回JSON出現(xiàn)下載文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 啟動SpringMVC的注解功能,完成請求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉(zhuǎn)換器 -->
</list>
</property>
</bean>
<!-- 定義跳轉(zhuǎn)的文件的前后綴 ,視圖模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 這里的配置我的理解是自動給后面action的方法return的字符串加上前綴和后綴,變成一個 可用的url地址 -->
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置文件上傳,如果沒有使用文件上傳可以不用配置,當然如果不配,那么配置文件中也不必引入上傳組件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默認編碼 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 內(nèi)存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>那我遇到這個問題的原因是什么呢?
是因為新配置的環(huán)境,缺少很多 jar 包,所以項目導(dǎo)入Eclipse之后, 這個文件就報 “<mvc:annotation-driven />” 這一句有錯了,
有錯,我也沒多想就先把它給刪除了。
好吧,問題就這樣產(chǎn)生了。
小結(jié)一下:在導(dǎo)入一個項目之后,可能會提示有很多錯誤,但針對一些配置文件,解決的方式不應(yīng)該是刪除或修改文件中的內(nèi)容,
而應(yīng)該先解決依賴等問題,不然像我這種之前沒有問題的項目,就因為換了環(huán)境就產(chǎn)生怪問題。
springMVC包掃描問題
為什么@COntroller要放在springMVC中?
@Controller注解的bean必須由DispatcherServlet初始化的children webApplicationContext來管理,在DispatcherServlet初始化的context中會掃描當前容器所有的bean實例,根據(jù)類級別以及方法級別的映射信息注解組裝成對應(yīng)的HandleMappering信息,但是ContextLoaderListener是不具備這個功能的。
一句話spring中沒有辦法掃描controller的bean,所以spring中可以掃描所有的,但是對于@controller不會騎作用,所以必須在springMVC中再加一次對controller的掃描。
PS:之前遇到一個事物的,一開始把@Transactional放在COntroller怎么都不起作用,原因是只在spring中配置了
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
而Controller沒配,原因<tx:annoation-driven/>只會查找和它在相同的應(yīng)用上下文件中定義的bean上面的@Transactional注解,如果你把它放在Dispatcher的應(yīng)用上下文中,它只檢查控制器(Controller)上的@Transactional注解,而不是你services上的@Transactional注解。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決JPA?save()方法null值覆蓋掉mysql預(yù)設(shè)的默認值問題
這篇文章主要介紹了解決JPA?save()方法null值覆蓋掉mysql預(yù)設(shè)的默認值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot執(zhí)行定時任務(wù)@Scheduled的方法
這篇文章主要介紹了SpringBoot執(zhí)行定時任務(wù)@Scheduled的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
解決IDEA Maven下載依賴時報錯ERROR - #org.jetbrains.ide
這篇文章主要介紹了解決IDEA Maven下載依賴時報錯ERROR - #org.jetbrains.idea.maven - Cannot reconnect.問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
Java數(shù)據(jù)庫連接_jdbc-odbc橋連接方式(詳解)
下面小編就為大家?guī)硪黄狫ava數(shù)據(jù)庫連接_jdbc-odbc橋連接方式(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
關(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)
下面小編就為大家?guī)硪黄P(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

