简体   繁体   English

减少项目之间的依赖

[英]reduce the dependencies between projects

I wanted create a project which should minimise the dependencies between the projects. 我想创建一个项目,该项目应最大程度地减少项目之间的依赖性。 My project structure is below. 我的项目结构如下。

com.example.persistence which will contains all the entities and one BaseDao which will contain a save method for saving the entities. com.example.persistence将包含所有实体,而一个BaseDao将包含用于保存实体的保存方法。

package com.example.pl;

public class BaseDao {

    public  void save(Object obj){
        System.out.println("Saved the object"+obj);
    }

}

com.example.dao will contain the method for saving the entities into the database.The save method substantially calls the BaseDao.save method which is in the persitence layer. com.example.dao将包含用于将实体保存到数据库中的方法BaseDao.save方法实质上调用位于BaseDao.save层中的BaseDao.save方法。

package com.example.dao;

import com.example.pl.BaseDao;

public class EmployeeDao extends BaseDao{

    @Override
    public void save(Object obj) {
        // TODO Auto-generated method stub
        super.save(obj);
    }

}

com.example.service which is a service layer which will access the Dao.Save() method. com.example.service,它是将访问Dao.Save()方法的服务层。

Now I wrote a main method in service layer itself to test the dependencies.when I run the main method it gives a below comilation error 现在我在服务层本身中编写了一个main方法来测试依赖项。当我运行main方法时,它给出了以下编译错误

package com.example.sl;

import com.example.dao.EmployeeDao;

public class EmployeeService {

    public static void main(String[] args) {
        EmployeeDao dao =new EmployeeDao();
        dao.save(null);
    }

}



The type com.example.pl.BaseDao cannot be resolved. It is indirectly referenced from required .class files

I know I am missing the jar the from the persistence layer in service layer. 我知道我在服务层的持久层中缺少jar。 so Is there any way without adding the dependency of persistence layer in service layer? 那么有没有办法在服务层中不添加持久层的依赖关系呢?

It's difficult to answer without knowing how you are organizing the project. 不知道您如何组织项目就很难回答。 So, I will take the liberty to assume that you are using Maven. 因此,我将自由地假设您正在使用Maven。 I will assume that you defined three projects/modules(persitence, dao, service) which will result in persistence.jar, dao.jar, and service.jar. 我将假设您定义了三个项目/模块(persitence,dao,service),这将导致persistence.jar,dao.jar和service.jar。

So, the service module will depend on dao module and dao module will depend on persistence module. 因此,服务模块将取决于dao模块,而dao模块将取决于持久性模块。 The beauty of maven like tool is that it will bring the dependencies transitively. Maven之类的工具的优点在于,它将传递依赖关系。 So, the pom.xml(maven artifact - defines project definition) will only declare dependency on dao. 因此,pom.xml(maven构件-定义项目定义)将仅声明对dao的依赖。

However, just directly answering your Question, you are missing persistence's lib jar from your classpath. 但是,仅直接回答您的问题,您就会从类路径中丢失持久性的lib jar。 Again, it is difficult to answer w/o knowing your project structure. 同样,很难不知道您的项目结构就回答。

In some form or fashion, you will have to add the persistence dependency. 以某种形式或方式,您将必须添加持久性依赖性。 If you are using a maven multi-module project, this error can sometimes occur, if you attempt to run a maven command in a sub-project, since it will be unable to locate the jars from the other modules. 如果您正在使用maven多模块项目,则尝试在子项目中运行maven命令时,有时会发生此错误,因为它将无法从其他模块中找到jar。 If it's a normal maven project and your dao.jar depends on the persistence.jar, it should be pulling in the persistence.jar into your project for you via the transitive dependency. 如果这是一个普通的Maven项目,而您的dao.jar依赖于persistence.jar,则它应该通过传递依赖项将persistence.jar拖入您的项目中。 (Net result, you are getting it in there some way, even if you don't specifically reference it.) (最终结果是,即使您没有专门引用它,也可以通过某种方式获得它。)

If you aren't using maven or some other dependency management tool, you will need to put the persistence.jar into your classpath/project. 如果您不使用maven或其他依赖管理工具,则需要将persistence.jar放入类路径/项目中。 The dependency management tool is just doing it for you, if you have setup the dependencies properly and didn't try to have the service one exclude the persistence from the transitive dependencies coming in from the DAO layer. 如果您已经正确地设置了依赖性并且没有试图让服务从DAO层传入的传递性依赖性中排除持久性,那么依赖性管理工具就是为您完成的工作。 You have objects in your DAO, which are compiled on top of objects in the persistence.jar. 您的DAO中有对象,这些对象是在persistence.jar中的对象之上编译的。 It has to have access to those to do the compilation of your project. 它必须具有访问权限才能进行项目的编译。

From what it sounds like, the way you have it built out... 从听起来上看,您的构建方式...

  • dao depends on persistence , and dao取决于persistence ,并且
  • service depends on dao . service取决于dao

That should just work. 应该工作。 You're not depending directly on persistence in your service layer, but you use a component that does . 您不是直接依赖于service层的persistence ,而是使用的组件可以做到一点 Barring anything that may have been overlooked by either of us, your dependency chain would not introduce a direct dependency on persistence . 除非我们中任何一个都可能被忽略,否则您的依赖链不会引入对persistence的直接依赖。

Depending on how you're doing this (Maven, Gradle, Ivy, etc), it may be of benefit to separate these into their own separate modules; 根据您的操作方式(Maven,Gradle,Ivy等),将它们分成各自独立的模块可能会有好处。 that way, the hierarchy is clear and the likelihood of introducing unintended dependencies is very slim. 这样,层次结构清晰,引入意外依赖的可能性很小。

With that hierarchy, you would ensure that dao has a depdendency on persistence , and that service has a dependency on dao . 使用该层次结构,您将确保daopersistence性上具有依赖性,并且servicedao具有依赖性。

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

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