搭建spring项目

作者: 阿拉丁节能灯 | 来源:发表于2018-07-26 13:28 被阅读22次

    知识准备阶段

    现在传统的项目应该还是用的spring框架(当然struts已经不用好多年了),spring仍属当前最热门的框架了。

    spring的模块主要有(666)

    • web
    • core
    • context
    • beans
    • aop
    • webmvc

    每个模块的作用,这里就不说了,我说也说不明白呀。大概就是
    web :web项目用到的,用一些http*的类,但是没有httpSession(这就尴尬了)
    webmvc:这个应该和web作用一样吧
    core:spring的核心包
    beans:都知道spring的ioc,那么beans这个就是ioc实现的核心,定义各种bean
    context:spring的上下文
    aop:和面向切面有关
    搭建spring项目再多也就这几个包了

    maven引入如下(maven是个好东西呀,一定要学会用呀)

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>   
    

    现在开始讲解搭建web项目的结构

    1. web项目怎么可能没有web.xml(当然在spring3.0版本之后可以用java类配置代替web.xml,但是太高深了,本少表示也不会呀)

        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
        <display-name>spring</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
        <!-- 应用程序Spring上下文配置 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:applicationContext.xml
        </param-value>
        </context-param>
    
        <!-- spring上下文加载监听器 -->
        <listener>
            <listener-class>
                org.springframework.web.context.ContextLoaderListener
            </listener-class>
        </listener>
    
        <!-- DispatcherServlet 配置 -->
        <servlet>  
            <servlet-name>spring</servlet-name>  
        <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    
        <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml  --> 
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath*:springMVC.xml</param-value>
        </init-param>  
        <load-on-startup>1</load-on-startup>  
        </servlet>
    
        <servlet-mapping>  
            <servlet-name>spring</servlet-name>  
            <url-pattern>*.do</url-pattern>  
        </servlet-mapping> 
    
        </web-app>  
    

    web.xml中其他的配置项就不说了,只说有关spring的配置

    1.1 spring上下文配置

    <!-- 应用程序Spring上下文配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:applicationContext.xml
        </param-value>
    </context-param>
    
    
    <!-- spring上下文加载监听器 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    

    曾经有面试官问我,配置spring上下文时用的标签是什么,就是

    <context-param></context-param>

    context-param上下文有两个子标签,其中 param-name 的值是唯一的,只能是 contextConfigLocation

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

    param-value指定的是设定上下文的xml文件所在位置。这里classpath和classpath*都是web项目的根目录,具体区别可以去百度。
    配置上下文得有spring监听,即

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

    1.2 前置servlet

    不管是原生的servlet还是现在的spring,都会用到dispatcherServlet,分发请求用的。配置如下:

    <!-- DispatcherServlet 配置 -->
        <servlet>  
            <servlet-name>spring</servlet-name>  
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <!-- 可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml  --> 
            <init-param>  
                <param-name>contextConfigLocation</param-name>  
                <param-value>classpath*:springMVC.xml</param-value>
            </init-param>  
            <load-on-startup>1</load-on-startup>  
        </servlet>  
        <servlet-mapping>  
            <servlet-name>spring</servlet-name>  
            <url-pattern>*.do</url-pattern>  
        </servlet-mapping>   
    

    很奇怪,和spring上下文配置一样,这个配置也有两部分组成,servlet和servlet-mapping,首先说下servlet-mapping:

    <url-pattern>*.do</url-pattern>

    表示带只有带.do的请求才能通过dispatcherServlet去找controller。
    其次servlet配置,要注意的就是xml放置的地方

    <param-value>classpath*:springMVC.xml</param-value>

    其他的就正常复制粘贴就可以了。

    2. spring上下文的配置文件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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.springframework.org/schema/context 
             http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    <bean id="cacheConf" class="com.wk.conf.CacheConf" init-method="init">
        <property name="dataUtils" ref="dataUtils"></property>
        <property name="map">
            <map></map>
        </property>
    </bean>
    <bean id="dataUtils" class="com.wk.utils.DataUtils"/>
    
    </beans>
    

    这。。。我不想讲了,就是各种bean的定义。哈哈哈

    3. dispatcherServlet配置文件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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/mvc    
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
    
     <!-- 加载properties配置文件 -->
     <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:prop/config.properties</value>
            </list>
        </property>
    </bean> 
    
    <!-- 扫描基本包 -->
    <context:component-scan base-package="com.wk"/>  
    
    <!-- 开启springMVC驱动 -->
    <mvc:annotation-driven/>
    
    <!-- 加载静态资源 -->
    <mvc:resources location="classpath:/WEB-INF/h/" mapping="/style/**"/> 
    <mvc:resources location="classpath:/WEB-INF/script/" mapping="/script/**"/> 
    <mvc:resources location="classpath:/WEB-INF/html/" mapping="/html/**"/> 
    </beans>  
    

    这个文件最重要的就是

    <mvc:annotation-driven/>

    <context:component-scan base-package="com.wk"/>

    其中<mvc:annotation-driven/> 开启扫描启动,<context:component-scan base-package="com.wk"/>中的base-package="com.wk"表示在com.wk及其所有子包下的spring注解都会被扫描,加到spring容器中。 也就是说,你在类上加一个@Controller注解,那这个类就是controller类了。
    这其中当然还有properties文件的加载,静态资源的加载,前后缀配置等,不多讲了。到此就可以启动spring项目了。

    总结

    spring项目缕清思路,掌握重点:
    引包666, 再弄xml, web是第一 , 配置上下文 ,前置不能少


    自古大神多寂寥,舞刀弄月笑乾坤;人丑别嫌镜子歪,我是小白我怕谁。本文仅限那些弄了好几十遍才明白一个超级简单的常识小白阅读,当然大神,超神什么的也可以看后指点一二,给些建议。本文可能说的并不全面或是准确,因为我都不知道自己说的对不对。但是我希望此文可以告诉那些处于迷茫,怀疑自己的人,不要放弃,坚持下去,就有希望。未少不姓韦,有啥尽管怼。

    相关文章

      网友评论

        本文标题:搭建spring项目

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