• maven打包项目,然后给其他项目引用


    A项目(这个项目需要被打包,作为被引入的项目),不需要启动类,因为作为公共模块被B项目引入:

    package com.yunya.mvndependontest.rest;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/teacher")
    public class TeacherRest {
    
        @RequestMapping("/say")
        public String say() {
            String word = "好好学习,天天向上!";
            System.out.println(word);
            return word;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    Teaching Service类,包含math 和 chinese方法:
    
    package com.yunya.mvndependontest.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class Teaching {
    
        public String math() {
            return "教数学啦";
        }
    
        public String chinese() {
            return "教中文啦";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    B项目:引入打包后的A项目(gav格式引入),启动端口是:8082

    MvnDependOnParentTestApplication 启动类文件:
    注意务必要使用@ComponentScan注解将A项目的restcontroller路径也引入,否则访问A项目restful接口路径(/teacher/say)会提示找不到地址。
    package com.yunya.mvndependonparenttest;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    
    @SpringBootApplication
    @ComponentScan("com.yunya")
    public class MvnDependOnParentTestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MvnDependOnParentTestApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    Student RestController类,该类中调用A项目的Teaching Service类的方法math:
    
    package com.yunya.mvndependonparenttest.rest;
    
    import com.yunya.mvndependontest.service.Teaching;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/student")
    public class Student {
    
        @Autowired
        Teaching teaching;
    
        @RequestMapping("/study")
        public String study() {
            String studyContent = "学生学到了一首诗:床前明月光,疑是地上霜。";
            System.out.println(studyContent);
    
            // 调用引入的A项目的teaching service类中的math方法
            String mathContent = teaching.math();
            System.out.println(mathContent);
    
            return studyContent;
        }
    }
    
    • 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

    访问效果:
    访问B项目地址:localhost:8082/teacher/say,如下效果:
    在这里插入图片描述
    访问B项目地址:localhost:8082/student/study,如下效果:
    在这里插入图片描述

    引用:
    把一个项目打成jar包并引入其他项目中

    springboot引入其他项目jar包并实现对数据库的操作

  • 相关阅读:
    VSCode远程连接
    java处理高并发高负载类网站的优化方法
    jmh测试实践(针对不同准备数据测试)
    HTML和CSS基础(一)
    Application UI
    Python并发编程:多线程与多进程实战
    强大的Nginx配置生成器
    CSS盒子模型
    3-Python基础编程之入门
    正在等待操作系统重新启动。 请重新启动计算机以安装autocad 2024。
  • 原文地址:https://blog.csdn.net/yunyala/article/details/134543283