何时使用“可选"选项?依赖关系以及何时使用“提供的"范围? [英] When to use "optional" dependencies and when to use "provided" scope?

查看:54
本文介绍了何时使用“可选"选项?依赖关系以及何时使用“提供的"范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< optional> true</optional> " scope>提供的</scope> 修饰的依赖关系在传递依赖时将被忽略.我已阅读解决方案

除注释外,还有语义更重要的区别:已提供"依赖项应该由容器提供,因此,如果容器为您提供了休眠状态,则应将休眠状态标记为已提供.

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

  1. 该库会提取大量的传递依赖项,而您实际上只需要很少的传递依赖项,以便您无故炸毁项目.

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

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?

解决方案

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.

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 they actually use. If you don't do, you might get two types of problems:

  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.

  2. 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.

这篇关于何时使用“可选"选项?依赖关系以及何时使用“提供的"范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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