使用Spring框架构建库或API是否合适?

时间:2021-05-14 15:48:38

I am currently building a library or API which will be used by another Spring REST application, below are the advantages I thought of using Spring.

我目前正在构建一个将由另一个Spring REST应用程序使用的库或API,下面是我想到使用Spring的优点。

  1. Spring flow(Context initialization->Post construct)
  2. Spring流程(Context initialization-> Post构造)

  3. Easy way of reading properties file
  4. 轻松读取属性文件的方法

  5. Autowiring and bean management
  6. 自动装配和bean管理

  7. Rapid development

Below are the disadvantage

以下是缺点

  1. Tightly coupled, the main application which is going to use the library/API has to import or refer the configuration class of library in order to initialize context
  2. 紧密耦合,将要使用库/ API的主应用程序必须导入或引用库的配置类以初始化上下文

  3. Main application has to use Spring(considering use in other projects)
  4. 主要应用必须使用Spring(考虑在其他项目中使用)

Is there anything I am missing, can Spring framework be used to develop API or library?

有什么我缺少的,Spring框架可以用来开发API或库吗?

Note: The number of library to build is 3-5 and will grow in future.

注意:要构建的库的数量是3-5,并且将来会增长。

1 个解决方案

#1


1  

The only reason to use spring you are giving, is to benefit from the building of the application runtime.

使用spring的唯一原因就是从构建应用程序运行时中受益。

I do not see it as a great advantage from spring for a library : it is only replacing a few new and set statements, that could be easily explained in a doc, with even some sample bean declaration for the users of your api, if he wishes to init your lib in a spring context.

我不认为它是Spring的一个很大的优势:它只是替换了一些新的和set语句,可以在doc中轻松解释,甚至为api的用户提供一些示例bean声明,如果他希望在spring环境中初始化你的lib。

One of the drawback you are not giving is the support of multiple versions of spring by your library : what will happen in one / two years, when an old application developped using your library will need small enhancements, but still be on spring v4, but new application will import spring v6 (with no resource to migrate old app on spring v4 to latest version) ?

您没有给出的缺点之一是您的库支持多个版本的spring:在一年/两年内会发生什么,当使用您的库开发旧应用程序时需要很小的增强功能,但仍然是在v4春季,但是新的应用程序将导入spring v6(没有资源将Spring v4上的旧应用程序迁移到最新版本)?

So i would use spring in a library only if you need to participate in database transaction of the main application (and other tasks like this where it is really convenient to use spring), but otherwise try to depend as little as possible on spring.

因此,只有当你需要参与主应用程序的数据库事务(以及其他像春天才真正方便使用的任务)时,我才会在库中使用spring,但是否则试图尽可能少地依赖spring。

EDIT: Even when minimising the use of spring in the runtime of your library, you can use it extensively to test your library.

编辑:即使在库的运行时最小化spring的使用,您也可以广泛地使用它来测试您的库。

The spring context instead of being included in the resource of your lib is only a test resource, which you can use to bootstrap spring-test, for example.

弹簧上下文而不是包含在lib的资源中只是一个测试资源,例如,您可以使用它来引导spring-test。

#1


1  

The only reason to use spring you are giving, is to benefit from the building of the application runtime.

使用spring的唯一原因就是从构建应用程序运行时中受益。

I do not see it as a great advantage from spring for a library : it is only replacing a few new and set statements, that could be easily explained in a doc, with even some sample bean declaration for the users of your api, if he wishes to init your lib in a spring context.

我不认为它是Spring的一个很大的优势:它只是替换了一些新的和set语句,可以在doc中轻松解释,甚至为api的用户提供一些示例bean声明,如果他希望在spring环境中初始化你的lib。

One of the drawback you are not giving is the support of multiple versions of spring by your library : what will happen in one / two years, when an old application developped using your library will need small enhancements, but still be on spring v4, but new application will import spring v6 (with no resource to migrate old app on spring v4 to latest version) ?

您没有给出的缺点之一是您的库支持多个版本的spring:在一年/两年内会发生什么,当使用您的库开发旧应用程序时需要很小的增强功能,但仍然是在v4春季,但是新的应用程序将导入spring v6(没有资源将Spring v4上的旧应用程序迁移到最新版本)?

So i would use spring in a library only if you need to participate in database transaction of the main application (and other tasks like this where it is really convenient to use spring), but otherwise try to depend as little as possible on spring.

因此,只有当你需要参与主应用程序的数据库事务(以及其他像春天才真正方便使用的任务)时,我才会在库中使用spring,但是否则试图尽可能少地依赖spring。

EDIT: Even when minimising the use of spring in the runtime of your library, you can use it extensively to test your library.

编辑:即使在库的运行时最小化spring的使用,您也可以广泛地使用它来测试您的库。

The spring context instead of being included in the resource of your lib is only a test resource, which you can use to bootstrap spring-test, for example.

弹簧上下文而不是包含在lib的资源中只是一个测试资源,例如,您可以使用它来引导spring-test。