eclipse 跨多个项目划分 Spring 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5092133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:51:21  来源:igfitidea点击:

Divide Spring configuration across multiple projects

javaeclipsespringmaven-2

提问by Bart Vangeneugden

we have quite a few projects that use the same codebase (backend code). Just the frontend tends to be different. We decided that the best approach would be to seperate backend and frontend into different projects: Engineand Project_name

我们有很多项目使用相同的代码库(后端代码)。只是前端往往不同。我们决定最好的方法是将后端和前端分离到不同的项目中:EngineProject_name

Now these are Spring-projects. So it would only seem logical if we divide the Spring configurations aswell: Database.xml, Services.xmlwould belong to the project Engine. And a specific Frontend.xmlwould belong to Project_Name. To link these up, I would need a generic SpringBeans.xmlthat imports all of these XML's.

现在这些是 Spring 项目。因此,如果我们也划分 Spring 配置,这似乎是合乎逻辑的: Database.xmlServices.xml将属于项目Engine。一个特定的Frontend.xml将属于Project_Name。为了将这些链接起来,我需要一个通用的SpringBeans.xml来导入所有这些 XML。

I tried following directory structure:

我尝试了以下目录结构:

Engine Project

发动机项目

  • Config
    • Spring
      • Database.xml
      • Services.xml
  • 配置
    • 春天
      • 数据库.xml
      • 服务.xml

Project_Name Project

Project_Name 项目

  • Config
    • SpringBeans.xml
    • spring
      • Frontend.xml
  • 配置
    • SpringBeans.xml
    • 春天
      • 前端.xml

The contents of SpringBeans.xml are simply:

SpringBeans.xml 的内容很简单:

    <import resource="spring/Database.xml"/>
    <import resource="spring/Services.xml"/>
    <import resource="spring/Frontend.xml"/> 

I set up Eclipse so the Project_Name project references the Engine project. When I start it, SpringBeans.xml gets found, however the XML files in the Engine project aren't found (FileNotFoundException). I'll also note that before splitting up the Engine and Frontend code into different projects, the principle of importing other XML files worked like a charm.

我设置了 Eclipse,因此 Project_Name 项目引用了 Engine 项目。当我启动它时,找到了 SpringBeans.xml,但是没有找到 Engine 项目中的 XML 文件(FileNotFoundException)。我还将注意到,在将引擎和前端代码拆分为不同的项目之前,导入其他 XML 文件的原则非常有用。

So my question to you: Is it possible to make different Spring configrations in different projects play together nicely?

所以我的问题是:是否可以让不同项目中的不同 Spring 配置很好地协同工作?

回答by Jan

Since the spring bean configuration files are in the classpath, you need to add the prefix claspath to the resource location:

由于spring bean的配置文件在classpath中,所以需要在资源位置加上前缀claspath:

<import resource="classpath:spring/Database.xml"/>
<import resource="classpath:spring/Services.xml"/>
<import resource="classpath:spring/Frontend.xml"/>
<import resource="classpath:spring/Database.xml"/>
<import resource="classpath:spring/Services.xml"/>
<import resource="classpath:spring/Frontend.xml"/>