maven多模块下的spring配置

1、首先是父子模块之间的依赖配置
创建父子依赖关系maven工程
maven工程dependencyManagement与dependencies区别

2、在总工程里添加spring依赖

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

    <groupId>com.tortuousroad</groupId>
    <artifactId>groupon</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <name>${project.artifactId}</name>

    <modules>
        <module>Admin</module>
        <module>Service</module>
        <module>Site</module>
    </modules>
    <properties>
        <servlet_version>3.1.0</servlet_version>
        <javassist_version>3.20.0-GA</javassist_version>
        <freemarker_version>2.3.22</freemarker_version>
        <spring_version>4.3.2.RELEASE</spring_version>

        <maven_jar_plugin_version>2.3.2</maven_jar_plugin_version>
        <maven_war_plugin_version>2.1.1</maven_war_plugin_version>
        <maven_install_plugin_version>2.3.1</maven_install_plugin_version>
        <maven_deploy_plugin_version>2.7</maven_deploy_plugin_version>
        <maven_compiler_plugin_version>3.2</maven_compiler_plugin_version>
        <maven_clean_plugin_version>2.4.1</maven_clean_plugin_version>
        <java_source_version>8</java_source_version>
        <java_target_version>8</java_target_version>
        <file_encoding>UTF-8</file_encoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.tortuousroad</groupId>
                <artifactId>groupon-service</artifactId>
                <version>1.0</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-webmvc</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</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>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </dependency>
    </dependencies>

</project>

3、在service模块中再声明依赖

因为项目整体是:Site(网站前台模块)、Admin(后台管理模块)、Service(前后台公用模块),即Site与Admin同时依赖Service,因此在Service中声明spring系列依赖即可
Service->pom.xml

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

    <parent>
        <groupId>com.tortuousroad</groupId>
        <artifactId>groupon</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>com.tortuousroad</groupId>
    <artifactId>groupon-service</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>${project.artifactId}</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
    </dependencies>

</project>

4、配置Site模块

Site中包含Controller模块与Web模块,而Web模块又依赖Controller模块

Site->pom.xml

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

    <parent>
        <groupId>com.tortuousroad</groupId>
        <artifactId>groupon</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>com.tortuousroad</groupId>
    <artifactId>groupon-site</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <name>${project.artifactId}</name>

    <modules>
        <module>Controller</module>
        <module>Web</module>
    </modules>

</project>

Site->Controller->pom.xml

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

    <parent>
        <groupId>com.tortuousroad</groupId>
        <artifactId>groupon-site</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>com.tortuousroad</groupId>
    <artifactId>groupon-site-controller</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>${project.artifactId}</name>
    <description>Controller.</description>

    <dependencies>
        <dependency>
            <groupId>com.tortuousroad</groupId>
            <artifactId>groupon-service</artifactId>
        </dependency>
    </dependencies>

</project>

Site->Web->pom.xml

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

    <parent>
        <groupId>com.tortuousroad</groupId>
        <artifactId>groupon</artifactId>
        <version>1.0</version>
    </parent>

    <groupId>com.tortuousroad</groupId>
    <artifactId>groupon-site</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <name>${project.artifactId}</name>

    <modules>
        <module>Controller</module>
        <module>Web</module>
    </modules>

</project>

5、配置Web模块

在Web/src/main/webapp/WEB-INF下
创建:
web.xml
applicationContext.xml
spring-servlet.xml

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

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

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  • 注意,servlet和servlet-mapping是必须的,而
<param-name>contextConfigLocation</param-name>

是可以更改默认选项(默认是去/WEB-INF/下加载applicationContext.xml)
同时,listener也并非必须

Only if you have two config xml files. One with Services / DAOs and another with Controller. If you have configured everything in one spring config file you don’t need the ContextLoaderListener, just the dispatcher servlet is sufficient.
It is recommended to split the config into two and use the ContextLoaderListener to create the root application context and the dispatcher servlet to create the web layer application context.
——stackoverflow

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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" 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/tx
          http://www.springframework.org/schema/tx/spring-tx.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.xsd">
    <context:component-scan base-package="com.tortuousroad">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

spring-servlet.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
       default-lazy-init="true">
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:resources mapping="/style/**" location="/style/"/>
    <mvc:resources mapping="*.html" location="/"/>
    <mvc:resources mapping="*.txt" location="/"/>

    <context:component-scan base-package="com.tortuousroad">
        <context:include-filter type="regex" expression="com.tortuousroad..controller"/>
    </context:component-scan>

    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" id="viewResolver">
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="order" value="2"/>
                <property name="prefix" value="/WEB-INF/views/"/>
                <property name="suffix" value=".jsp"/>
             </bean>
        </list>
    </property>
    </bean>

</beans>

6、编写测试,并部署
这里写图片描述

选择war
这里写图片描述

7、项目目前结构

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值