Spring MVC-开发预准备

1、修改web.xml

src>main>WEB-INF>web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>Archetype Created Web Application</display-name>
  <!-- spring mvc的前端控制器,拦截所有请求 实现现在很流行的REST风格的URI-->
  <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一个Servlet,,所以可以配置多个DispatcherServlet-->
  <!--DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。-->
  <servlet>
	<servlet-name>spring</servlet-name>
	<!--在DispatcherServlet的初始化过程中,框架会在web应用的 WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml 的配置文件,生成文件中定义的bean。-->
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--指明了配置文件的文件名,不使用默认配置文件名,而使用dispatcher-servlet.xml配置文件。-->
	<init-param>
	  <param-name>contextConfigLocation</param-name>
	  <param-value>classpath:spring/spring-servlet.xml</param-value>
	</init-param>
	<!--是启动顺序,让这个Servlet随Servletp容器一起启动。-->
	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
	<url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list> 
    <welcome-file>/webapp/static/index.jsp</welcome-file>
  </welcome-file-list> 
  <session-config>
    <!--会话超时配置,单位分钟-->
    <session-timeout>360</session-timeout>
  </session-config>
</web-app>

web.xml文件是Java web项目中的一个配置文件,主要用于配置欢迎页、FilterListenerServlet等,但并不是必须的,一个java web项目没有web.xml文件照样能跑起来。Tomcat容器/conf目录下也有作用于全局web应用web.xml文件,当一个web项目要启动时,Tomcat会首先加载项目中的web.xml文件,然后通过其中的配置来启动项目,只有配置的各项没有错误时,项目才能正常启动。

使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一个Servlet,可以配置多个DispatcherServlet

DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理

指明了配置文件的文件名,不使用默认配置文件名,而使用spring-servlet.xml配置文件。

web.xml文件加载顺序为:(与顺序无关)

ServletContext -> context-param -> listener -> filter -> servlet

2、配置spring-dispatch-servlet.xml

spring-mvc中,在applicationContext.xml dispatch-servlet.xml中都可以进行spring 配置

ApplicationContext.xml  spring 全局配置文件,用来控制spring 特性的

dispatcher-servlet.xml spring mvc里面的,控制器、拦截uri转发view

使用applicationContext.xml文件时是需要在web.xml中添加listener

1) 一个bean如果在两个文件中都被定义了(比如两个文件中都定义了component scan扫描相同的package)spring会在application contextservlet context中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的(见下面描述的问题)。

2) 如果在application context servlet context中都存在同一个 @Service 的实例, controller(在servlet context中) 通过 @Resource引用时, 会优先选择servlet context中的实例。

其实如果直接使用SpringMVC是可以不添加applicationContext.xml文件的

3)

<?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:util="http://www.springframework.org/schema/util" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-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/context 
						http://www.springframework.org/schema/context/spring-context-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">
						
  <context:component-scan base-package="com.zhenling.controller"/>	
  <mvc:annotation-driven/>					
  <!-- 将所需要的bean写到这里,被beans包裹着 -->
  
  <!-- 内部资源视图解析器,前缀 + 逻辑名 + 后缀 -->
  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  
    <property name="prefix">
      <value>/webapp/static/js/</value>
    </property>
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>
</beans>

Spring配置文件

schemaLocation目的是引入约束

Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置。

我们使用Spring框架所做的就是两件事:开发Bean、配置Bean。对于Spring矿建来说,它要做的就是根据配置文件来创建Bean实例,并调用Bean实例的方法完成“依赖注入”。

Id:确定该Bean的唯一标识符,容器对Bean管理、访问、以及该Bean的依赖关系,都通过该属性完成。Beanid属性在Spring容器中是唯一的。    

Class:指定该Bean的具体实现类。注意这里不能使接口。通常情况下,Spring会直接使用new关键字创建该Bean的实例,因此,这里必须提供Bean实现类的类名。

4)Bean的作用域以及Scope的作用域

Spring容器集中管理Bean的实例化,通过 spring 容器我要获得一个对象,Bean实例可以通过BeanFactorygetBean(Stringbeanid)方法得到。BeanFactory是一个工厂,程序只需要获取BeanFactory引用,即可获得Spring容器管理全部实例的引用。程序不需要与具体实例的实现过程耦合。

通过 spring 容器我要获得一个对象,

其中scope总共有以下几个属性:

通用:singleton(默认属性,单例模式)prototype(多例模式)

用于web工程里:requestsessionglobalsession

单例模式和多例模式的区别:在一个方法里创建一个 bean 的对象,如果是单例模式,前后通过 getBean 获得的对象是同一个对象,前后操作互相影响。但如果是多例模式,通过 getBean 获得的对象不是同一个对象,前后操作互不影响。

5)Spring Bean的三种注入方式

注:构造方法为public Student(String name,Integer age){this.name=name;this.age=age}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值