Spring Core 和 Spring MVC中的配置

前言

最早接触的是spring 中的xml配置,感觉不是问题。后来Spring 文档中使用注解配置越来越多,经常被弄的头晕。需要整理一下。

 

@Configuration和@Bean

Spring Core提供的注解。非常简单,与xml配置中声明一个bean类型。

@Configuration
public class MyConfig {
    @Bean
    public MyService myService() {
        return new MyService();
    }
}

Dispatcherservlet

https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/web.html#mvc-servlet

Spring MVC设计中,叫做“central Servlet”。

 

WebApplicationContext和Context Hierarchy

https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/web.html#mvc-servlet-context-hierarchy

每个DispatcherServlert都有自己的context,类型是WebapplictionContext。

由于应用中可以存在多个DispatherServlet,就有多个WebapplicationContext。

为了解决Bean的复用问题,DispatcherServlet  的WebaplicationContext可以有一个父WebapplicationContext。

java代码配置DispatcherServlet的Webapplicationcontext和父WebapplicationContext:

https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/web.html#mvc-container-config
 

另外还可以在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/app-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

</web-app>

下面这两段是配置父webapplicationContext.

ContextLoaderListener监听器保证在servlet初始化之前,创建父Webapplicationcontext,并存储在application attribute里。

 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-context.xml</param-value>
    </context-param>

 

Special Bean Types

https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/web.html#mvc-servlet-special-bean-types

Spring MVC的主要特性都由这些bean提供,比如下面3个

HandlerMapping找到合适的Interceptor
HandlerAdapter找到合适的Dispatchservlet和处理方法
HandlerExceptionResolver处理异常

按照官网的说明,使用如下xml配置能自动往context中注册以上3种类型的默认bean(不过我在5.1版本没有这个配置依然在visula VM的实例中找到了以上3种类型的bean)

<mvc:annotation-driven/>

 mvc:annotation-driven 等同于注解

@EnableWebMvc

 <mvc:annotation-driven/>

此配置能满足大部分的web应用,即使不满足,也很容易扩展。

它除了注册上面说的3个bean,还会:

 举例,如果classpath里有fastxml-jackson,则此配置会自动将jackson作为JSON的格式的消息转化器(配合@ResponsBody使用),

不然,在Controller方法中返回对象会抛出异常:

org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.ArrayList
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值