【Spring MVC】之一 环境搭建

一、Spring的主要功能:

IOC 依赖注入(控制反转)和AOP (面向切面的编程)
正常情况下,有A对象需要B对象时,我们直接New B对象。这样A、B两对象间耦合度比较高。Spring提供一种实现,对象的创建由第三方统一、集中管理,A对象如需B对象时,直接申请即可得到,无需自已NEW 对象,以实现解耦。也就是A对象需B对象,B对象由第三方创建,并注入给A对象。这种思想称为依赖注入。这个第三方也称为IOC容量,ApplcationContext。

Spring中提供统一管理的容器是ApplicationContext,也称为应用上下文。

二、ApplicationContext的配置方式

ApplicationContext是Bean的生产工厂,是Bean的容器。容量根据配置信息进行生产和管理Bean。那如何定义或表示Bean及Bean之间的关系。
Spring提供了三种方式:1、XML;2、注解;3、Java配置

先介绍XML方式,如建立ApplicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="login" class="com.stu.users.login"></bean>
</beans>

其中<bean>中的ID和Class属性也定义bean

Spring通过装载配置文件来实例化ApplicationContext,实例化之化,通过Application
ApplicationContext的getBean来获得bean实例。

根据配置文件的不同格式和存放位置,Spring提供了多种ApplicationContext的实现。

三、ApplicationContext如何启动

那ApplicationContext 本身是如何实例化的。在WEB中,可以通过Listener方式来启动spring容器。ApplicationContext的缺省配置文件是存放在WEB-INF目录下的ApplicationContext.XML。当然,你也可以定义。在WEB.XML中配置

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/Root-Context.xml</param-value>

</context-param>
<listener>

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

</listener>

四、通过IDEA建立Spring 项目

1、File->New Project->选左边的Maven,选择Create from archetype中的org.apache.archetypes.maven-archetype-webapp

Paste_Image.png

2、修改POM.XML, 增加Spring的类库

<properties>
      <spring.servison>4.3.7.RELEASE</spring.servison>
  </properties>

  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.servison}</version>
      </dependency>

3、修改WEB.XML
修改为Servlet 3.1格式

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">

      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

增加ApplicationContext的配置文件和Listener

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

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

五、建立SpringMVC 项目

1、在POM.XML 增加Spring MVC的类库。

      <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.servison}</version>
        </dependency>

2、在WEB.XML中增加DispatcherServlet

<servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

3、创建springmvc.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
">

    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/Viwe/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

4、创建Controler

package com.edu.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by User on 2017/3/31.
 */
@Controller
public class Index {
    @RequestMapping(value="/",method = RequestMethod.GET )
    public String hello(){
        return "index";
    }
    @RequestMapping(value="/world",method = RequestMethod.GET    )
    public String helloworld(){
        return "world";
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容