maven多模块项目SpringMVC简单实例

划分多模块,也就是方便多人开发,自己开发自己的那块没有多大冲突

项目结构

整个项目目录是这样的:

GitHub地址 :https://github.com/thecattle/maven_model

maven多模块项目SpringMVC简单实例_第1张图片

—- app-parent
|– pom.xml (pom)
|
|– app-dao
| |– pom.xml (jar)
|
|– app-service
| |– pom.xml (jar)
|
|– app-web
|– pom.xml (war)

注意他们的packaging类型,有jar,war和pom三种

dao–作为jar导入–>service–作为jar导入–>web–build成war项目–>运行

项目搭建

父模块仅仅是帮助聚合其他模块构建的工具,它本身并无实际的内容。
父模块与其他模块的目录结构并非一定是父子关系,还可以是平行关系。更改下modules路径即可。

  • 首先new一个maven项目作为parent项目。
    pom文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.sungroupId>
    <artifactId>maven_testartifactId>
    <version>1.0-SNAPSHOTversion>

    
    <packaging>pompackaging>

    
    <modules>
        <module>maven_servicemodule>
        <module>maven_webmodule>
        <module>maven_daomodule>
    modules>

    
    <properties>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
    properties>

    <dependencies>

        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>

        

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.1.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>4.1.4.RELEASEversion>
        dependency>

    dependencies>
project>
  • 再new一个maven项目作为dao模块。pom文件如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    
    <parent>
        <artifactId>maven_testartifactId>
        <groupId>com.sungroupId>
        <version>1.0-SNAPSHOTversion>
    parent>

    
    <packaging>jarpackaging>

    <modelVersion>4.0.0modelVersion>

    <artifactId>maven_daoartifactId>
project>
  • 再new一个maven项目作为service模块,pom如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    
    <parent>
        <artifactId>maven_testartifactId>
        <groupId>com.sungroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    
    <packaging>jarpackaging>

    <artifactId>maven_serviceartifactId>

    <dependencies>

        
        <dependency>
            <groupId>com.sungroupId>
            <artifactId>maven_daoartifactId>
            
            <version>${project.version}version>
        dependency>

    dependencies>
project>
  • 再new一个maven项目作为web模块,pom如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    
    <parent>
        <artifactId>maven_testartifactId>
        <groupId>com.sungroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    
    <packaging>warpackaging>

    <artifactId>maven_webartifactId>

    <dependencies>

        
        <dependency>
            <groupId>com.sungroupId>
            <artifactId>maven_serviceartifactId>
            <version>${project.version}version>
        dependency>

    dependencies>

project>

现在这三个模块就已经关联起来了

配置SpringMVC

在web模块中,web.xml文件如下:


<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_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
        <welcome-file>index.htmlwelcome-file>
    welcome-file-list>

    
    <servlet>
        <servlet-name>spring-mvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup> 
    servlet>

    <servlet-mapping>
        <servlet-name>spring-mvcservlet-name>
        <url-pattern>/url-pattern> 
    servlet-mapping>

web-app>

spring-mvc.xml:


<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

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

beans>

你可能感兴趣的:(spring)