Spring常用配置及解析类说明

springMVC配置用法的文章很多,但具体描述清楚的不多,这里主要介绍下常用的配置项的用法,以及它的解析类,springMVC处理内容有两种方式,一种是converter,另一种是ViewResolver,两种都能处理json,xml以及form内容格式。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns:c="http://www.springframework.org/schema/c"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 

<!-- 如果controller里要用到配置,才需要加载配置,因为一般这个配置由DispatchServlet来加载,和spring监听类不在一个上下文里,想要知道原因请看
http://blog.csdn.net/strivezxq/article/details/43795081 这篇文章详细解析了spring初始化过程 --> 

   <context:property-placeholder location="classpath:app.properties" />
<!--Scans the classpath for annotated components @Component, @Repository, @Service, and @Controller
通过use-default-filters="false",可以设置只扫描哪些注释,一般springMVC配置只加载下面两种注释
-->
<context:component-scan base-package="your.base.package" use-default-filters="false"> 

<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- <context:component-scan annotation-config = "true">已经包含了context:annotation-configr的功能,所以这个配置基本没必要配置,激活在bean类中被检测到的各种注释:Spring's @Required and
   @Autowired, as well as JSR 250's @PostConstruct, @PreDestroy and @Resource (if available),
   JAX-WS's @WebServiceRef (if available), EJB3's @EJB (if available), and JPA's
   @PersistenceContext and @PersistenceUnit (if available) --> 

<context:annotation-config />
<!--会在Spring MVC上下文中定义一个 org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler, 它会像一个检查员,对进入DispatcherServlet的URL进行筛查,如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的 Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理。
一般Web应用服务器默认的Servlet名称是"default",因此DefaultServletHttpRequestHandler可以 找到它。如果你所有的Web应用服务器的默认Servlet名称不是"default",则需要通过default-servlet-name属性显示指 定:
<mvc:default-servlet-handler default-servlet-name="所使用的Web服务器默认使用的Servlet名称" />
Tomcat, Jetty, JBoss, and GlassFish默认名称为default, eg: web.xml中 

  1. <servlet-mapping>
  2.   <servlet-name>default</servlet-name>
  3.   <url-pattern>*.jpg</url-pattern>
  4. </servlet-mapping>
  5. <servlet-mapping>
  6.   <servlet-name>default</servlet-name>
  7.   <url-pattern>*.js</url-pattern>
  8. </servlet-mapping>
  9. <servlet-mapping>
  10.   <servlet-name>default</servlet-name>
  11.   <url-pattern>*.css</url-pattern>
  12. </servlet-mapping>   

如果不配置springdefault-servlet-name 默认会设置,已经支持常用的web服务器
-->
<mvc:default-servlet-handler /> 

<!-- 允许静态资源放在任何地方 ,处理类org.springframework.web.servlet.resource.ResourceHttpRequestHandler
<bean id="resourceHttpRequestHandler" class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
  <property name="locations" value="classpath:/META-INF/resources/"></property>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/resources/**">resourceHttpRequestHandler</prop>
    </props>
  </property>
</bean>
下面标签实现
-->
<mvc:resources mapping="/resources/**" location="/resources/"></mvc:resources>
<!--
register "global" interceptor beans to apply to all registered HandlerMappings .
Each inteceptor must implement the org.springframework.web.servlet.HandlerInterceptor or
org.springframework.web.context.request.WebRequestInterceptor interface
-->
<mvc:interceptors>
  <mvc:interceptor>
    <mvc:mapping path="/**" />
    <mvc:exclude-mapping path="/css/**" />
    <mvc:exclude-mapping path="/js/**" />
    <mvc:exclude-mapping path="/images/**" />
    <bean class="com.fpx.common.auth.mgt.framework.interceptor.ContextInterceptor" />
  </mvc:interceptor>
</mvc:interceptors> 

<!-- Turns on support for mapping requests to Spring MVC @Controller methods
Also registers default Formatters and Validators for use across all @Controllers
配置解析类:org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
配置content-negotiation-anager可以在url里设置内容类型参数,可以设置默认内容类型
<bean id="contentNegotiationManagerFactoryBean" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"
p:favorPathExtension="false" p:favorParameter="true" p:parameterName="format" p:ignoreAcceptHeader="true"
p:defaultContentType="application/json">
  <property name="mediaTypes">
  <props>
  <prop key="json">application/json</prop>
  <prop key="xml">application/xml</prop>
   </props>
  </property>
</bean>
-->
<mvc:annotation-driven content-negotiation-anager="contentNegotiationManagerFactoryBean">
  <mvc:message-converters>
    <ref bean="stringHttpMessageConverter" />
    <ref bean="jsonHttpMessageConverter" />
    <ref bean="marshallingHttpMessageConverter" />
  </mvc:message-converters>
</mvc:annotation-driven> 

<!-- 内容管理工厂 -->
     <bean
        class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"
        p:favorPathExtension="false" p:favorParameter="true"
        p:parameterName="format" p:ignoreAcceptHeader="true"
        p:defaultContentType="application/json">
        <property name="mediaTypes">
          <props>
            <prop key="json">application/json</prop>
            <prop key="xml">application/xml</prop>
          </props>
        </property>
      </bean> 

<!-- 内容解析器 ,可以p:parameterName="format"来配置返回参数类型 ,通过p:defaultContentType配置默认请求内容类型,
c:qualityValue="0.5" 可以设置内容类型的优先级, 如果使用了mvc:annotation-driven 和注解方式(@RequestBody), 下面配置是不生效的--> 

  <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
     <property name="contentNegotiationManager"  ref= "contentNegotiationManagerFactoryBean"> 

     </property>
     <property name="defaultViews">
        <list>
          <bean
             class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
             <property name="modelKey" value="resultVo" />
             <property name="extractValueFromSingleKeyModel" value="true" />
          </bean>
          <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
             <constructor-arg ref="jaxb2Marshaller" />
             <property name="contentType" value="application/xml" />
          </bean>
        </list>
     </property>
     <!-- <property name="ignoreAcceptHeader" value="true" /> -->
   </bean> 

   <!-- XML view using a JAXB marshaller -->
   <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
     <property name="marshallerProperties">
        <map>
          <entry key="jaxb.formatted.output">
             <value type="boolean">true</value>
          </entry>
          <entry key="jaxb.encoding" value="UTF-8" />
        </map>
     </property>
     <property name="packagesToScan">
        <list>
          <value>com.api.domain</value>
          <value>com.api.web.controller.vo</value>
        </list>
     </property>
   </bean> 

   <bean id="jstlViewResolver"
     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="order" value="2" />
     <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
     <property name="prefix" value="/views/" />
     <property name="suffix" value="" />
     <property name="requestContextAttribute" value="rc" />
   </bean> 

<!-- c:qualityValue="0.5" 可以设置内容类型的优先级,默认是1.0,越大优先级越高 -->
   <bean id="stringHttpMessageConverter"
     class="org.springframework.http.converter.StringHttpMessageConverter">
     <property name="supportedMediaTypes">
        <list>
          <value>text/plain;charset=UTF-8</value>
          <value>text/html;charset=UTF-8</value>
        </list>
     </property>
   </bean> 

   <bean id="jsonHttpMessageConverter"
     class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
   <bean id="marshallingHttpMessageConverter"
     class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
     <constructor-arg ref="jaxb2Marshaller" />
     <!-- <property name="supportedMediaTypes" value="application/xml"></property> -->
     <property name="supportedMediaTypes">
        <util:list>
          <bean class="org.springframework.http.MediaType" c:type="application" c:subtype="xml" c:qualityValue="0.5"/>
        </util:list>
     </property>
   </bean> 

SpringMVC返回json配置步骤如下:

1、添加jackson.jar包

2、在applicationContext.xml配制文件中添加如下代码

<!--解析返回JSON -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> -->
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
 <property name="messageConverters">
  <list >
  <ref bean="mappingJacksonHttpMessageConverter" />
  </list>
 </property>
 </bean>
 <bean id="mappingJacksonHttpMessageConverter"
 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
 <property name="supportedMediaTypes">
  <list>
  <value>text/html;charset=UTF-8</value>
  </list>
 </property>
 </bean> 

3、在controller中添加如下代码

@RequestMapping(value="/chinese/listTree", method = RequestMethod.POST)
@ResponseBody
 public List getlistChinese(Model model){
 List<User> list = (List<ChineseCategory>) commonMgr.find("from User");
  return list;
 }

返回值可以为list也可以为Map类型

总结

以上就是本文关于Spring常用配置及解析类说明的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

Spring中利用配置文件和@value注入属性值代码详解

spring配置扫描多个包问题解析

Spring配置使用之Bean生命周期详解

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

(0)

相关推荐

  • 详解Spring Cloud Zuul中路由配置细节

    上篇文章我们介绍了API网关的基本构建方式以及请求过滤,小伙伴们对Zuul的作用应该已经有了一个基本的认识,但是对于路由的配置我们只是做了一个简单的介绍,本文我们就来看看路由配置的其他一些细节. 首先我们来回忆一下上篇文章我们配置路由规则的那两行代码: zuul.routes.api-a.path=/api-a/** zuul.routes.api-a.serviceId=feign-consumer 我们说当我的访问地址符合/api-a/**规则的时候,会被自动定位到feign-consume

  • Spring装配Bean之用Java代码安装配置bean详解

    前言 本文主要给大家介绍了关于Spring之利用Java代码安装配置bean的相关内容,尽管通过组件扫描和自动装配实现Spring的自动化配置很方便也推荐,但是有时候自动配置的方式实现不了,就需要明确显示的配置Spring.比如说,想要将第三方库中的组件装配到自己的应用中,这样的情况下,是没办法在它的类上添加 @Compnent和 @Autowired注解的. 在这种情况下,需要使用显示装配的方式,可以分别通过Java和XML实现,推荐使用Java的方式,因为更加强大,类型安全并且重构友好,因为

  • spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法

    串行的定时任务 @Component public class ScheduledTimer { private Logger logger = Logger.getLogger(this.getClass()); /** * 定时任务,1分钟执行1次,更新潜在客户超时客户共享状态 */ @Scheduled(cron="0 0/1 8-20 * * ?") public void executeUpdateCuTask() { Thread current = Thread.curr

  • Spring中利用配置文件和@value注入属性值代码详解

    1 简单属性值注入 package com.xy.test1; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service // 需要被注入属性值的类需要被Spring管理 public class PropertiesService1 { // 利用@Value注解,即使没有该属性或者属性文件也不会报错 // @Value输入

  • Spring boot多线程配置方法

    本文实例为大家分享了Spring boot多线程配置的具体代码,供大家参考,具体内容如下 1.配置线程配置类 package test; import java.util.concurrent.Executor; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.ComponentScan; import o

  • Spring常用配置及解析类说明

    springMVC配置用法的文章很多,但具体描述清楚的不多,这里主要介绍下常用的配置项的用法,以及它的解析类,springMVC处理内容有两种方式,一种是converter,另一种是ViewResolver,两种都能处理json,xml以及form内容格式. <?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.s

  • Spring深入了解常用配置应用

    目录 常用配置 一.别名 二.bean 的配置 三.import 存在问题 总结 常用配置 现在这里简单了解一下spring 配置文件中的一些常用配置,在后面我们还会遇到更多的配置,在后文继续进行介绍了. spring中的配置一共也就这几个 description描述不太重要, bean在之前已经见识过了, alias给bean起别名, import在当前xml文件中导入其他xml文件 一.别名 在spring中别名主要是给bean的id起一个别名,同样也有好几种方式. 1.alias 配置 <

  • Spring Boot2配置服务器访问日志过程解析

    这篇文章主要介绍了Spring Boot2配置服务器访问日志过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Tomcat控制台中看到的日志是服务器的日志,而服务器访问日志则是记录服务处理的请求信息. 开发环境:IntelliJ IDEA 2019.2.2 Spring Boot版本:2.1.8 1.新建一个名称为demo的Spring Boot项目. 2.application.yml 添加配置 server: tomcat: base

  • Spring Bean生命周期之Bean元信息的配置与解析阶段详解

    目录 BeanDefinitionReader体系 BeanDefinitionReader接口定义 元信息配置与解析方式 XmlBeanDefinitionReader元信息解析源码分析 AnnotatedBeanDefinitionReader元信息解析源码分析 总结 写在前面 注:本文章使用的 SpringBoot 版本为 2.2.4.RELEASE,其 Spring 版本为 5.2.3.RELEASE 虽然Bean的创建可以采用BeanDefinition API 也可以直接采用注解方式

  • Java之Spring注解配置bean实例代码解析

    前面几篇均是使用xml配置bean,如果有上百个bean,这是不可想象的.故而,请使用注解配置bean !!! [1]注解类别 @Component : 基本注解, 标识了一个受 Spring(点击这里可以下载<Spring应用开发完全手册>) 管理的组件 @Repository : 标识持久层组件 @Service : 标识服务层(业务层)组件 @Controller : 标识表现层组件 Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. 对于扫描到的组

  • web容器中实例化spring相关配置解析

    文章主要探究了web容器中实例化spring的相关配置简单介绍,接下来我们看看具体内容. web容器中实例化spring相关配置说明: 要想在web容器实例化时加载spring容器,web.xml文件中配置如下: <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </cont

  • spring boot springMVC扩展配置实现解析

    摘要: 在spring boot中 MVC这部分也有默认自动配置,也就是说我们不用做任何配置,那么也是OK的,这个配置类就是 WebMvcAutoConfiguration,但是也时候我们想设置自己的springMvc配置怎么办呢 . 我们也可以写个自己的配置类,继承 WebMvcConfigurer 重写需要的配置方法 .在spring boot 早期是继承WebMvcConfigurerAdapter ,但是高版已标上注解@Deprecated,注意:在配置类中不要标注:@EnableWeb

  • Spring AOP AspectJ使用及配置过程解析

    这篇文章主要介绍了Spring AOP AspectJ使用及配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持.因为Spring1.0的时候Aspectj还未出现; AspectJ1.5中新增了对注解的支持,允许直接在Bean类中定义切面.新版本的Spring框架建 议我们都使用AspectJ方式来开发AOP,并提供了非常灵活且

  • Spring Security自定义登录页面认证过程常用配置

    目录 一.自定义登录页面 1.编写登录页面 2.修改配置类 3.编写控制器 二. 认证过程其他常用配置 1.失败跳转 1.1编写页面 1.2修改表单配置 1.3添加控制器方法 1.4设置fail.html不需要认证 2.设置请求账户和密码的参数名 2.1源码简介 2.2修改配置 2.3修改页面 3.自定义登录成功处理器 3.1源码分析 3.2代码实现 4.自定义登录失败处理器 4.1源码分析 4.2代码实现 一.自定义登录页面 虽然Spring Security给我们提供了登录页面,但是对于实际

  • 浅谈spring 常用注解

    我们不妨先将spring常用的注解按照功能进行分类 1 .将普通类加入容器形成Bean的注解 日常开发中主要使用到的定义Bean的注解包括(XML方式配置bean暂不讨论): @Component.@Repository.@Service.@Controller.@Bean 其中@Component.@Repository.@Service.@Controller实质上属于同一类注解,用法相同,功能相同,区别在于标识组件的类型.当一个组件代表数据访问层(Dao)时,你可以给它加上@Reposit

随机推荐