java - 我们应该将每个对象都暴露为 spring bean 吗?
问题描述
class TibcoPasswordRetriever {
private TibcoPasswordUtil tibcoPasswordUtil;
public TibcoPasswordRetriever(TibcoPasswordUtil tibcoPasswordUtil) {
this.tibcoPasswordUtil = tibcoPasswordUtil;
}
}
class TibcoPasswordRetriever {
private TibcoPasswordUtil tibcoPasswordUtil;
public TibcoPasswordRetriever() {
this.tibcoPasswordUtil = new TibcoPasswordUtil();
}
}
这是 TibcoPasswordRetriever 的两个定义。
问题:我只在 TibcoPasswordRetriever 类中使用 TibcoPasswordUtil。依赖注入仍然是个好主意吗?
进一步的问题:我们是否应该将每个可能的对象都创建为spring bean(只是因为可以这样做)
解决方案
Question : I am using TibcoPasswordUtil in TibcoPasswordRetriever class only. Is dependency injection still a good idea?
Probably dependency injection is good idea if TibcoPasswordUtil
is a Singletone
. You did not provide TibcoPasswordUtil
class in the question. Addind Util
to name does not mean many here. Consider writing more about architecture and context.
Further question: Should we create expose every possible object as spring bean (just because it is possible to do that)
No it is not, cause better solution would be not to be dependent so much on framework. Ask yourself questions like:
If you don't have to, then why is the advantage of it?
Why is the benefit of not doing so?
Like i wrote above less coupled code is better. In the future it would be easier to for example change framework.
推荐阅读
- amazon-web-services - 在 AWS 实例中运行的弹性搜索不可访问
- c# - ListView 内的 Xamarin CarouselView
- amazon-web-services - 如何从无服务器应用程序安全地连接到 AWS DynamoDB 或其他 aws 数据库
- c# - 如何记录来自电报机器人的所有消息?
- if-statement - 从两个数据 sas 创建变量
- android - 我想在 AlerDilog 中将 relativelayout 背景设置为透明
- ios - Swift - 如何使 JSONDecoder 正确解析类层次结构中的日期数组
- angular - Angular - 如何制作文件上传服务?
- scala - 对 multipart/form-data 进行 POST 调用时,Gatling 测试返回 504
- r - Shiny DT - 使用按钮在选定行之后选择行