• dubbo(4):spring和springmvc快速整合


    前言

    spring和springmvc 如何快速整合?

    本章代码已分享至:https://gitee.com/lengcz/dubbo01.git

    第一节 父项目

    1. 新建一个空项目dubbo01
      在这里插入图片描述
      2.

    第二节 新建子模块dubbo-service(maven)

    1. 新建模块dubbo-service
      在这里插入图片描述

    在这里插入图片描述

    1. 导入依赖
      <properties>
            <spring.version>5.1.9.RELEASEspring.version>
            <dubbo.version>2.7.4.1dubbo.version>
            <zookeeper.version>4.0.0zookeeper.version>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.1.0version>
                <scope>providedscope>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${spring.version}version>
            dependency>
    
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-apiartifactId>
                <version>1.7.33version>
            dependency>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
                <version>1.7.33version>
            dependency>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubboartifactId>
                <version>${dubbo.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-frameworkartifactId>
                <version>${zookeeper.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-recipesartifactId>
                <version>${zookeeper.version}version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    1. 新建service和实现类
      在这里插入图片描述
    package com.it2.service;
    
    public interface UserService {
    
         String hello();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.it2.service.impl;
    
    import com.it2.service.UserService;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImpl implements UserService {
        public String hello() {
            return "hello dubbo";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 在resources目录下创建spring文件夹,并在spring文件夹下创建applicationContext.xml
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" 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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        
        <context:component-scan base-package="com.it2.service"/>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 在resources目录配置log4.properties
    ### 设置###
    log4j.rootLogger = debug,stdout,Deb,Err
    
    ### 输出信息到控制抬 ###
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target = System.out
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
    
    ### 输出DEBUG 级别以上的日志到=../logs/error.log ###
    log4j.appender.Deb = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.Deb.File = ../logs/log.log
    log4j.appender.Deb.Append = true
    log4j.appender.Deb.Threshold = DEBUG 
    log4j.appender.Deb.layout = org.apache.log4j.PatternLayout
    log4j.appender.Deb.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
    
    ### 输出ERROR 级别以上的日志到=../logs/error.log ###
    log4j.appender.Err = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.Err.File =../logs/error.log 
    log4j.appender.Err.Append = true
    log4j.appender.Err.Threshold = ERROR 
    log4j.appender.Err.layout = org.apache.log4j.PatternLayout
    log4j.appender.Err.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    第三节 新建子模块dubbo-web(maven)

    1. 新建项目
      在这里插入图片描述

    在这里插入图片描述

    1. 导入依赖并配置tomcat启动插件
     <properties>
            <spring.version>5.1.9.RELEASEspring.version>
            <dubbo.version>2.7.4.1dubbo.version>
            <zookeeper.version>4.0.0zookeeper.version>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>javax.servletgroupId>
                <artifactId>javax.servlet-apiartifactId>
                <version>3.1.0version>
                <scope>providedscope>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${spring.version}version>
            dependency>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-apiartifactId>
                <version>1.7.33version>
            dependency>
            <dependency>
                <groupId>org.slf4jgroupId>
                <artifactId>slf4j-log4j12artifactId>
                <version>1.7.33version>
            dependency>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubboartifactId>
                <version>${dubbo.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-frameworkartifactId>
                <version>${zookeeper.version}version>
            dependency>
            <dependency>
                <groupId>org.apache.curatorgroupId>
                <artifactId>curator-recipesartifactId>
                <version>${zookeeper.version}version>
            dependency>
    
            <dependency>
                <groupId>com.it2groupId>
                <artifactId>dubbo-serviceartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
    
        dependencies>
    
        <build>
            <plugins>
                
                <plugin>
                    <groupId>org.apache.tomcat.mavengroupId>
                    <artifactId>tomcat7-maven-pluginartifactId>
                    <version>2.1version>
                    <configuration>
                        <port>8000port>
                        <path>/path>
                    configuration>
                plugin>
            plugins>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    1. 项目改成war形式
      
        <packaging>warpackaging>
    
    • 1
    • 2

    在这里插入图片描述
    4. 在resources目录下创建spring文件夹,并新建springmvc.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    		http://www.springframework.org/schema/mvc
    		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    		http://dubbo.apache.org/schema/dubbo
    		http://dubbo.apache.org/schema/dubbo/dubbo.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-4.0.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    		http://www.springframework.org/schema/tx
    		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">
        <mvc:annotation-driven/>
        <context:component-scan base-package="com.it2.controller"/>
    
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 在main目录下创建webapp文件夹,并在webapp下创建WEB-INF文件夹,在WEB-INF下配置web.xml文件
      在这里插入图片描述
    
    <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_4_0.xsd"
             version="4.0">
    
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath*:spring/applicationContext*.xmlparam-value>
        context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
    
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:spring/springmvc.xmlparam-value>
            init-param>
        servlet>
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>*.dourl-pattern>
        servlet-mapping>
    
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    1. 在resources目录配置log4.properties
    ### 设置###
    log4j.rootLogger = debug,stdout,Deb,Err
    
    ### 输出信息到控制抬 ###
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target = System.out
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
    
    ### 输出DEBUG 级别以上的日志到=../logs/error.log ###
    log4j.appender.Deb = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.Deb.File = ../logs/log.log
    log4j.appender.Deb.Append = true
    log4j.appender.Deb.Threshold = DEBUG 
    log4j.appender.Deb.layout = org.apache.log4j.PatternLayout
    log4j.appender.Deb.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
    
    ### 输出ERROR 级别以上的日志到=../logs/error.log ###
    log4j.appender.Err = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.Err.File =../logs/error.log 
    log4j.appender.Err.Append = true
    log4j.appender.Err.Threshold = ERROR 
    log4j.appender.Err.layout = org.apache.log4j.PatternLayout
    log4j.appender.Err.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 编写controller
      在这里插入图片描述
    package com.it2.controller;
    
    
    import com.it2.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
    
        @Autowired
        private UserService userService;//注入service
    
        @RequestMapping("/sayhello")
        public String sayhello(){
            return userService.hello();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    1. 启动项目

    由于dubbo-web依赖dubbo-service,所以需要先让dubbo-service执行一下install,如果已执行install可以忽略

    在这里插入图片描述

    在这里插入图片描述

    1. 打开浏览器访问http://localhost:8000/user/sayhello.do
      在这里插入图片描述

    自此,spring和springmvc快速就完成了。

  • 相关阅读:
    图像哈希:DCT篇
    【CSS】设置文字(文本)的渐变色
    第二章 C++的输出
    Node.js安装环境环境配置,以及yarn安装以及踩坑之路
    go的堆内存结构分析
    知识库网站如何搭建?需要注意这五个要点!
    SAP S4 FI后台详细配置教程- PART3 (财务凭证相关配置篇)
    【C++ 程序设计入门基础】- Chapter One
    java88-Charactor包装类
    Kali Linux 2024.1
  • 原文地址:https://blog.csdn.net/u011628753/article/details/126977332