何时使用< optional> true</optional>以及何时使用< scope>提供的</scope> [英] When to use <optional>true</optional> and when to use <scope>provided</scope>

查看:86
本文介绍了何时使用< optional> true</optional>以及何时使用< scope>提供的</scope>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<optional>true</optional><scope>provided</scope>装饰的依赖项在传递性依赖项时将被忽略.我已阅读

the dependencies decorated by <optional>true</optional> or <scope>provided</scope> will be ignored when they are dependent transitively. I have read this, my understanding is like the difference between @Component and @Service in Spring, they only vary semantically. Is it right?

推荐答案

除注释外,还有一个更重要的 semantic 区别:容器应提供提供的"依赖项,因此,如果您的容器让您处于休眠状态,则应将休眠状态标记为已提供.

In addition to the comment, there is more important semantic difference: "Provided" dependencies are expected to be supplied by the container, so if your container gives you hibernate, you should mark hibernate as provided.

可选依赖项主要用于减轻某些库的传递负担.例如:如果您可以使用具有5种不同数据库类型的库,但是通常只需要一种,则可以将依赖于库的依赖项标记为可选,以便用户可以提供他实际使用的库.如果不这样做,则可能会遇到两种类型的问题:

Optional dependencies are mainly used to reduce the transitive burden of some libraries. For example: If you can use a library with 5 different database types, but you usually only require one, you can mark the library-dependent dependencies as optional, so that the user can supply the one he actually uses. If you don't do, you might get two types of problems:

  1. 该库会提取大量的传递依赖关系,而实际上您几乎不需要这些传递依赖关系,因此您可以无故炸毁项目.

  1. The library pulls a huge load of transitive dependencies of which you actually need very few so that you blow up your project without reason.

更危险的是:您可能会拉取两个具有重叠类的库,以使类加载器无法同时加载它们.这可能会导致您的库出现意外行为.

More dangerously: You might pull two libraries with overlapping classes, so that the class loader cannot load both of them. This might lead to unexpected behaviour of your library.

这篇关于何时使用&lt; optional&gt; true&lt;/optional&gt;以及何时使用&lt; scope&gt;提供的&lt;/scope&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆