SpringMVC整合,出現(xiàn)注解沒(méi)有起作用的情況處理
SpringMVC整合注解沒(méi)有起作用
在spring的applicationContext.xml中配置問(wèn)題
正確的配置:
<?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:context="http://www.springframework.org/schema/context" ? ? ? ?xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ? ? ? ? http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> ? ? <!--啟動(dòng)注解--> ? ? <context:annotation-config /> ? ? <!-- ?base-package 注解所在的包根據(jù)自己的需要?jiǎng)澐肿⒔忸?lèi)使用的范圍 ?--> ? ? <context:component-scan base-package="main.com.talkweb"> ? ? ? ? <!--不再管理Controller 注解 ?因?yàn)樗鼏为?dú)給mvc-servler.xml去管理--> ? ? ? ? <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> ? ? </context:component-scan> </beans>
明明有啟動(dòng)注解,但有些注解還是沒(méi)起作用
<!--啟動(dòng)注解--> ? ? <context:annotation-config />
后來(lái)發(fā)現(xiàn)原因是:下面這句作與springMVC的controller注解的排除關(guān)注時(shí),把掃描注解的返回局限了
原來(lái) base-package=”main.com.talkweb.Controller”:
!-- ?base-package 注解所在的包根據(jù)自己的需要?jiǎng)澐肿⒔忸?lèi)使用的范圍 ?--> ? ? <context:component-scan base-package="main.com.talkweb.Controller"> ? ? ? ? <!--不再管理Controller 注解 ?因?yàn)樗鼏为?dú)給mvc-servler.xml去管理--> ? ? ? ? <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> ? ? </context:component-scan>
現(xiàn)在的包范圍 base-package=“main.com.talkweb”:
!-- ?base-package 注解所在的包根據(jù)自己的需要?jiǎng)澐肿⒔忸?lèi)使用的范圍 ?--> ? ? <context:component-scan base-package="main.com.talkweb"> ? ? ? ? <!--不再管理Controller 注解 ?因?yàn)樗鼏为?dú)給mvc-servler.xml去管理--> ? ? ? ? <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> ? ? </context:component-scan>
注解方式整合SpringMVC
整合SpringMVC分析
web容器在啟動(dòng)的時(shí)候,會(huì)掃描每個(gè)jar包下的META-INF/services/javax.servlet.ServletContainerInitializer文件,加載這個(gè)文件里面指定的類(lèi)SpringServletContainerInitializer

spring的應(yīng)用一啟動(dòng)就會(huì)加載WebApplicationInitializer接口下的所有組件并創(chuàng)建對(duì)象

WebApplicationInitializer接口下有三個(gè)抽象類(lèi)

AbstractContextLoaderInitializer抽象類(lèi):
調(diào)用createRootApplicationContext()方法來(lái)創(chuàng)建根容器

AbstractDispatcherServletInitializer抽象類(lèi)
調(diào)用createServletApplicationContext()方法創(chuàng)建一個(gè)web的IOC容器
調(diào)用createDispatcherServlet()方法創(chuàng)建了DispatchServlet
將創(chuàng)建的DispatchServlet添加到ServletContext中

AbstractAnnotationConfigDispatcherServletInitializer抽象類(lèi)
使用注解的方式配置的DispatcherServlet初始化器
該類(lèi)重寫(xiě)AbstractContextLoaderInitializer父類(lèi)的createRootApplicationContext()方法來(lái)創(chuàng)建根容器。調(diào)用getRootConfigClasses()方法傳入一個(gè)配置類(lèi)

該類(lèi)還重寫(xiě)了AbstractDispatcherServletInitializer父類(lèi)的createServletApplicationContext()方法來(lái)創(chuàng)建web的IOC容器。獲取配置類(lèi)注冊(cè)到IOC容器中。

以注解方式來(lái)啟動(dòng)SpringMVC,需要繼承AbstractAnnotationConfigDispatcherServletInitializer,然后實(shí)現(xiàn)抽象方法指定DispatcherServlet的配置信息
注解方式整合SpringMVC
自定義容器初始化器
/*web容器啟動(dòng)的時(shí)候創(chuàng)建對(duì)象,調(diào)用方法來(lái)初始化容器以及前端控制器*/
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
/*獲取根容器的配置類(lèi),相當(dāng)于Spring的配置文件,用來(lái)創(chuàng)建父容器*/
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
/*獲取web容器的配置類(lèi),相當(dāng)于SpringMVC的配置文件,用來(lái)創(chuàng)建子容器*/
protected Class<?>[] getServletConfigClasses() {
return new Class[]{AppConfig.class};
}
/*獲取DispatcherServlet的映射信息*/
protected String[] getServletMappings() {
return new String[]{"/"};
}
}子容器配置類(lèi)
//SpringMVC只掃描Controller,子容器
@ComponentScan(value = "springMVC", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
})
public class AppConfig {
}父容器配置類(lèi)
//Spring的容器不掃描Controller,必須禁用默認(rèn)的過(guò)濾規(guī)則才會(huì)生效,父容器
@ComponentScan(value = "springMVC", excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)
public class RootConfig {
}Service層
@Service
public class HelloService {
public String sayHello(String name){
return "hello " + name;
}
}Controller層
@Controller
public class HelloController {
@Autowired
HelloService helloService;
@ResponseBody
@RequestMapping("/hello")
public String hello(){
String hello = helloService.sayHello("張三");
return hello;
}
}定制SpringMVC
使用@EnableWebMvc開(kāi)啟SpringMVC定制配置功能,相當(dāng)于在xml文件中使用<mvc:annotation-driven/>標(biāo)簽
@ComponentScan(value = {"controller", "service", "config", "springMVC"}, includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)
@EnableWebMvc
public class AppConfig {
}配置組件,包括視圖解析器、視圖映射、靜態(tài)資源映射、攔截器等等
@ComponentScan(value = {"controller", "service", "config", "springMVC"}, includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
}, useDefaultFilters = false)
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter{
//定制視圖解析器
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
//定制靜態(tài)資源訪問(wèn)
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
/*相當(dāng)于在xml配置文件中使用<mvc:default-servlet-handler>標(biāo)簽,
* 將SpringMVC處理不了的請(qǐng)求交給Tomcat,這樣靜態(tài)資源就可以訪問(wèn)了*/
configurer.enable();
}
//定制攔截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 如何使用Idea搭建全注解式開(kāi)發(fā)的SpringMVC項(xiàng)目
- SpringMVC?@RequestMapping注解屬性詳細(xì)介紹
- SpringMVC中RequestBody注解的List參數(shù)傳遞方式
- SpringMVC @GetMapping注解路徑?jīng)_突問(wèn)題解決
- SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)
- 解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問(wèn)題
- SpringMVC注解@RequestParam方法原理解析
- springMVC?@RestControllerAdvice注解使用方式
相關(guān)文章
Java實(shí)現(xiàn)Linux下雙守護(hù)進(jìn)程
這篇文章主要介紹了Java實(shí)現(xiàn)Linux下雙守護(hù)進(jìn)程的思路、原理以及具體實(shí)現(xiàn)方式,非常的詳細(xì),希望對(duì)大家有所幫助2014-10-10
SpringBoot獲取配置文件內(nèi)容的幾種方式總結(jié)
大家都知道SpringBoot獲取配置文件的方法有很多,下面這篇文章主要給大家介紹了關(guān)于SpringBoot獲取配置文件內(nèi)容的幾種方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Java中判斷對(duì)象是否相等的equals()方法使用教程
與==運(yùn)算符響應(yīng),equals()方法也是Java中對(duì)對(duì)象進(jìn)行比較的一大方式,要特別注意二者的不同點(diǎn),這個(gè)我們?cè)谙挛闹屑磳⒅v到,接下來(lái)我們就來(lái)看一下Java中判斷對(duì)象是否相等的equals()方法使用教程2016-05-05
Java的WeakHashMap源碼解析及使用場(chǎng)景詳解
這篇文章主要介紹了Java的WeakHashMap源碼解析及使用場(chǎng)景詳解,Map本身生命周期很長(zhǎng),需要長(zhǎng)期貯留內(nèi)存中,但Map中的Entry可以刪除,使用時(shí)可以從其它地方再次取得,需要的朋友可以參考下2023-09-09
SpringBoot靜態(tài)資源與首頁(yè)配置實(shí)現(xiàn)原理深入分析
最近在做SpringBoot項(xiàng)目的時(shí)候遇到了“白頁(yè)”問(wèn)題,通過(guò)查資料對(duì)SpringBoot訪問(wèn)靜態(tài)資源做了總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10
Mybatis插件之自動(dòng)生成不使用默認(rèn)的駝峰式操作
這篇文章主要介紹了Mybatis插件之自動(dòng)生成不使用默認(rèn)的駝峰式操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11

