Spring MVC通過(guò)注解完成運(yùn)行配置,原理你都會(huì)嗎?
環(huán)境:Spring5.3.26
SpringMVC使用相信大家都會(huì)使用,別人項(xiàng)目工程搭建后,你只需負(fù)責(zé)寫(xiě)Controller即可,那你是否想過(guò)自己能否把環(huán)境搭建出來(lái)呢?而且還不借助網(wǎng)絡(luò);本篇教大家如何通過(guò)注解快速搭建SpringMVC運(yùn)行環(huán)境。
傳統(tǒng)SpringMVC配置
本節(jié):回顧傳統(tǒng)SpringMVC的基本配置原理。
DispatcherServlet需要一個(gè)WebApplicationContext(一個(gè)普通ApplicationContext的擴(kuò)展)用于它自己的配置。WebApplicationContext有一個(gè)鏈接到ServletContext和它所關(guān)聯(lián)的Servlet。它還綁定到ServletContext,這樣應(yīng)用程序就可以在需要訪問(wèn)WebApplicationContext時(shí)使用RequestContextUtils上的靜態(tài)方法來(lái)查找它。
對(duì)于許多應(yīng)用程序來(lái)說(shuō),只有一個(gè)WebApplicationContext就足夠簡(jiǎn)單了。也可以有一個(gè)上下文層次結(jié)構(gòu),其中一個(gè)根WebApplicationContext在多個(gè)DispatcherServlet(或其他Servlet)實(shí)例之間共享,每個(gè)實(shí)例都有自己的子WebApplicationContext配置。有關(guān)上下文層次結(jié)構(gòu)特性的更多信息,請(qǐng)參閱ApplicationContext的附加功能。
根WebApplicationContext通常包含基礎(chǔ)設(shè)施bean,例如需要跨多個(gè)Servlet實(shí)例共享的數(shù)據(jù)存儲(chǔ)庫(kù)和業(yè)務(wù)服務(wù)。這些bean被有效地繼承,并且可以在特定于Servlet的子WebApplicationContext中被重寫(xiě)(即重新聲明),該子WebApplicationContext通常包含給定Servlet的本地bean。下圖顯示了這種關(guān)系:
web.xml中配置:
<web-app>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>app1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app1-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app1</servlet-name>
<url-pattern>/app1/*</url-pattern>
</servlet-mapping>
</web-app>
ContextLoaderListener:該監(jiān)聽(tīng)器用來(lái)創(chuàng)建Root 容器,該容器就是用來(lái)配置基礎(chǔ)的Bean,如DAO,Service等。
DispatcherServlet:對(duì)應(yīng)一個(gè)web 容器,也就是子容器。該容器用來(lái)配置Controller。在Controller中會(huì)應(yīng)用到Service,那么該子容器就會(huì)從父容器中查找相應(yīng)的Bean。如下父子關(guān)系配置:
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
protected WebApplicationContext initWebApplicationContext() {
// 獲取父容器,該父容器是在ContextLoaderListener監(jiān)聽(tīng)器中創(chuàng)建并保存到ServletContext中
WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
if (!cwac.isActive()) {
if (cwac.getParent() == null) {
cwac.setParent(rootContext);
}
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// 創(chuàng)建子容器并設(shè)置父容器
wac = createWebApplicationContext(rootContext);
}
return wac;
}
}
以上就是SpringMVC的基本配置。
Servlet注冊(cè)
既然是基于注解的方式配置SpringMVC,那么我們需要先了解Servlet的注冊(cè)方式有哪些。
方式1:
web.xml中注冊(cè)
<servlet>
<servlet-name>DemoServlet</servlet-name>
<servlet-class>com.pack.servlet.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
方式2:
基于注解方式
@WebServlet(name = "demoServlet", urlPatterns = "/demo")
@WebServlet(value = {"/demo","/demo1"})
@WebServlet(value = "/demo")
@WebServlet("/demo")
public class DemoServlet extends HttpServlet {
// ...
}
方式3:
通過(guò)SPI技術(shù),這也是今天要使用的方式
Servlet3.0以上的版本開(kāi)始,可以通過(guò)SPI方式注冊(cè)Servlet,F(xiàn)ilter,Listener三大組件。
第一步:在項(xiàng)目中建立如下文件
META-INF/service/javax.servlet.ServletContainerInitializer
文件名:javax.servlet.ServletContainerInitializer
第二步:自定義類(lèi)實(shí)現(xiàn)ServletContainerInitializer
@HandlesTypes({CustomHandler.class})
public class CustomContainerInitializer implements ServletContainerInitializer {
// 這里的set集合就是當(dāng)前環(huán)境中所有CustomHandler的子類(lèi)
@Override
public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
if (set!=null&&set.size()>0){
set.stream().forEach(cls->{
try {
CustomHandler o = (CustomHandler)cls.newInstance();
o.onStartup();
} catch (Exception e) {
e.printStackTrace();
}
});
}
//注入Servlet
ServletRegistration.Dynamic userServlet = servletContext.addServlet("DemoServlet", DemoServlet.class);
userServlet.addMapping("/demo");
}
}
SpringMVC注解配置
接下來(lái)就是要使用上面介紹的Servlet注冊(cè)方式的第三種方式來(lái)實(shí)現(xiàn)SpringMVC的注冊(cè)。
在Spring中已經(jīng)提供了相應(yīng)的實(shí)現(xiàn):
在spring-web包中:
內(nèi)容:
org.springframework.web.SpringServletContainerInitializer
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
}
這里我們只需要實(shí)現(xiàn)WebApplicationInitializer接口即可,不過(guò)Spring已經(jīng)為我們定義好了該接口的抽象模版,我們只需繼承該抽象類(lèi)即可:
public class SpringMVCConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {RootConfig.class} ;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {WebConfig.class} ;
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"} ;
}
}
RootConfig.java
@Configuration
public class RootConfig {
}
WebConfig.java
@Configuration
@ComponentScan(basePackages = {"com.pack.controller"})
public class WebConfig {
}
測(cè)試controller
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping("")
public Object index() {
Map<String, Object> result = new HashMap<>() ;
result.put("code", 0) ;
result.put("data", "你好") ;
return result ;
}
}
測(cè)試:
只是通過(guò)如上配置,SpringMVC環(huán)境基本上是可以使用了,但是我們看上面Controller接口,是基于REST full,所以當(dāng)你訪問(wèn)該接口時(shí)會(huì)出現(xiàn)如下錯(cuò)誤:
這是因?yàn)槟J(rèn)情況下RequestMappingHandlerAdapter無(wú)法處理,服務(wù)器端無(wú)法提供與 Accept-Charset 以及 Accept-Language 消息頭指定的值相匹配的響應(yīng)。
這時(shí)候就需要為其配置相應(yīng)的消息轉(zhuǎn)換器:
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter() ;
adapter.getMessageConverters().add(new MappingJackson2HttpMessageConverter()) ;
return adapter ;
}
再次方法正常:
完畢?。。?/p>