美文网首页
配置文件

配置文件

作者: luckee | 来源:发表于2019-02-18 22:14 被阅读0次

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC_01</display-name>
  <!-- 配置前端控制器DispatcherServlet -->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--springmvc.xml 是自己创建的SpringMVC全局配置文件,用contextConfigLocation作为参数名来加载
        如果不配置 contextConfigLocation,那么默认加载的是/WEB-INF/servlet名称-servlet.xml,在这里也就是 springmvc-servlet.xml
      -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--第一种配置:*.do,还可以写*.action等等,表示以.do结尾的或者以.action结尾的URL都由前端控制器DispatcherServlet来解析
        第二种配置:/,所有访问的 URL 都由DispatcherServlet来解析,但是这里最好配置静态文件不由DispatcherServlet来解析
        错误配置:/*,注意这里是不能这样配置的,应为如果这样写,最后转发到 jsp 页面的时候,仍然会由DispatcherServlet进行解析,
                    而这时候会找不到对应的Handler,从而报错!!!
      -->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

spring.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--注解处理器映射器  -->   
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
     
    <!--注解处理器适配器  -->   
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>  
 
    <!--使用mvc:annotation-driven可以代替上面的映射器和适配器
        这里面会默认加载很多参数绑定方法,比如json转换解析器就默认加载,所以优先使用下面的配置
      -->
    <!-- <mvc:annotation-driven></mvc:annotation-driven> -->
 
 
    <!--单个配置Handler  -->
    <!-- <bean class="com.ys.controller.HelloController"></bean> -->
     
    <!--批量配置Handler,指定扫描的包全称  -->
    <context:component-scan base-package="com.ys.controller"></context:component-scan>
     
 
    <!--配置视图解析器  -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 返回视图页面的前缀 -->
        <property name="prefix" value="/WEB-INF/view/"></property>
        <!-- 返回页面的后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

context-param和init-param区别

  • context-param:是应用范围内的参数,存放在ServletContext中,当服务器启动时,服务器会为每一个WEB应用创建一个唯一的ServletContext对象代表WEB应用。每一个web应用中的servlet共享一个ServletContext,所以Servlet之间就可以通过该对象来实现数据通信。ServletConfig,Servlet,Session等对象都可以获取到ServletContext对象
  • init-context:是Servlet范围内的参数,只能在Servlet的init()的方法中取得

spring配置文件的命名空间写法

spring配置文件的命名空间写法

整合Spring时Service层为什么不做全局包扫描详解

整合Spring时Service层为什么不做全局包扫描详解
使用ssm框架的时候,其实是有2个容器,spring容器和springmvc容器,而spring是springmvc的父容器,dao,service还有一些其他的东西是注入在spring容器中的,controller是注入在springmvc容器中的,子容器可以访问父容器中的东西,反之则不行

<!-- 加载spring容器 -->
  <!-- 初始化加载application.xml的各种配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/application-*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 配置springmvc前端控制器 -->
  <servlet>
    <servlet-name>taotao-manager</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,
     springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

所以如果我们使用批量扫描来将java bean注册到spring容器中的话(配置文件的位置定义在context-param,一般是spring-*.xml的形式),不应该扫描controller,controller应该交给springmvc,配置文件在DispacherServlet的init-param中指定(一般是springmvc.xml

ref="dataSource"提示无法解析

出现这样的问题,一般是spring配置文件有多个的情况(spring-dao,spring-service,spring-transaction),使用<import resource="classpath:">导入dataSource所在的配置文件即可

相关文章

  • 配置文件使用实例

    从配置文件读取内容进行实例化 读取配置文件的类 配置文件 使用配置文件

  • springboot(2)配置文件与整合其他技术

    4.配置文件: 4.1 SpringBoot配置文件类型:4.2 yml配置文件简介与语法: 4.3配置文件与...

  • URXVT

    配置文件 生成简明的配置文件 生成有详细说明的配置文件 [1] 示例配置文件 .Xresource / .X...

  • Spring Boot

    配置文件 默认配置文件:application.properties 推荐配置文件:application.yml...

  • Nginx + Uwsgi的基础配置

    Nginx 配置文件: Uwsgi配置文件:

  • linux rabbitmq 修改端口号

    查找配置文件位置: 拷贝配置文件到 /etc/rabbitmq 目录下: 修改配置文件:

  • python配置文件读取

    1、ConfigParser读取配置文件模块 配置文件 [people]为section 2、读取配置文件

  • Hexo+Next的摸索

    区分站点配置文件和主题配置文件 站点配置文件路径:blog/_config.yml主题配置文件路径:blog/th...

  • 环境变量

    环境变量配置文件 查看环境变量 set source 配置文件 等同于 . 配置文件重新读取配置文件/etc...

  • Kandroid代码篇 (2) 从assets读取配置

    创建 assets 配置文件使用 AssetManager 读取配置文件内容使用 fastjson 解析配置文件内...

网友评论

      本文标题:配置文件

      本文链接:https://www.haomeiwen.com/subject/czftyqtx.html