简体   繁体   English

springboot 将类从一个项目导入到另一个项目

[英]springboot import classes from one project to another

my scheme is as follows我的方案如下

-project1 (springboot maven Initializr)
  -model
  -service
  -controller
  -properties(mongodb)
-project2 (springboot maven Initializr)
  -controller (import project1 service)

I already added it to dependencies and I call the service of my project1 as follows:我已经将它添加到依赖项中,并按如下方式调用我的 project1 的服务:

import com.example.project1.service.Document_service;

I don't have any typing error in my project2, but when I run project2 I have the error:我的 project2 中没有任何输入错误,但是当我运行 project2 时出现错误:

com/example/project2/controller/project2_controller.java:[11,33] package com.example.project1.service 
does not exist
symbol:   class Document_service
location: class com.example.project2.controller.project2_controller
-> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

I need help to run the service1 project on my project2, how can I do it?我需要帮助才能在我的 project2 上运行 service1 项目,我该怎么做?

Try to clone this project Clef Workflow .尝试克隆这个项目Clef Workflow And see my architecture.看看我的架构。 I do my best to do it.我尽我所能去做。

If I've got your question right, you've made a dependency of one spring boot project on another.如果我的问题是正确的,那么您已经将一个 spring 引导项目依赖于另一个。

This is, in general, a wrong approach and should not be done, because spring boot artifact is not really a JAR, it has an extension "jar" but doesn't have a structure of jar and hence can't be used by java itself in runtime or a build tool as a dependency (its a very special setup with custom classloader developed by spring boot team, with all the internal dependencies baked into BOOT-INF/lib folder). This is, in general, a wrong approach and should not be done, because spring boot artifact is not really a JAR, it has an extension "jar" but doesn't have a structure of jar and hence can't be used by java本身在运行时或作为依赖项的构建工具(它是一个非常特殊的设置,带有由 spring 引导团队开发的自定义类加载器,所有内部依赖项都烘焙到BOOT-INF/lib文件夹中)。

In terms of resolution, consider introducing a module with all the relevant code but so that it won't be a spring boot service by itself - just a regular JAR.在分辨率方面,考虑引入一个包含所有相关代码的模块,但它本身不会是 spring 引导服务 - 只是一个普通的 JAR。

Then project1 and project2 could both add this "common" module as a dependency and this should work.然后project1project2都可以将此“通用”模块添加为依赖项,这应该可以工作。

try adding in project2 the following:尝试在 project2 中添加以下内容:

@SpringBootApplication
@ComponentScan({"com.example.project1", "com.example.project2" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM