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)澐肿⒔忸愂褂玫姆秶??--> ? ? <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)澐肿⒔忸愂褂玫姆秶??--> ? ? <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)澐肿⒔忸愂褂玫姆秶??--> ? ? <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è)文件里面指定的類SpringServletContainerInitializer

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

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

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

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

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

該類還重寫了AbstractDispatcherServletInitializer父類的createServletApplicationContext()方法來(lái)創(chuàng)建web的IOC容器。獲取配置類注冊(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 {
/*獲取根容器的配置類,相當(dāng)于Spring的配置文件,用來(lái)創(chuàng)建父容器*/
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
/*獲取web容器的配置類,相當(dāng)于SpringMVC的配置文件,用來(lái)創(chuàng)建子容器*/
protected Class<?>[] getServletConfigClasses() {
return new Class[]{AppConfig.class};
}
/*獲取DispatcherServlet的映射信息*/
protected String[] getServletMappings() {
return new String[]{"/"};
}
}子容器配置類
//SpringMVC只掃描Controller,子容器
@ComponentScan(value = "springMVC", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
})
public class AppConfig {
}父容器配置類
//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)文章
spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)操作
這篇文章主要介紹了spring-AOP 及 AOP獲取request各項(xiàng)參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java?8?的異步編程利器?CompletableFuture的實(shí)例詳解
這篇文章主要介紹了Java?8?的異步編程利器?CompletableFuture?詳解,本文通過(guò)一個(gè)例子給大家介紹下Java?8??CompletableFuture異步編程的相關(guān)知識(shí),需要的朋友可以參考下2022-03-03
spring security中的csrf防御原理(跨域請(qǐng)求偽造)
這篇文章主要介紹了spring security中的csrf防御機(jī)制原理解析(跨域請(qǐng)求偽造),本文通過(guò)實(shí)例代碼詳解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

