In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
小编给大家分享一下Java怎么正确地使用依赖注入,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一、C++的诟病
C++最遭人诟病的地方就是定义一个类需要写两个文件,一个.h文件和一个.cpp文件。例如定义一个CMainFrame类,mainframe.h内容如下:
class CMainFrame : public CFrameWndEx{protected: CMainFrame();public: virtual ~CMainFrame();};
mainframe.cpp内容如下:
CMainFrame::CMainFrame(){} CMainFrame::~CMainFrame(){}
当需要给这个类添加一个方法时,需要同时修改.h文件和.cpp文件。例如新增一个DefWindowProc函数。需要在.h文件中增加该函数的声明。
class CMainFrame : public CFrameWndEx{protected: CMainFrame();public: virtual ~CMainFrame(); protected: virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);};
mainframe.cpp中增加DefWindowProc的定义:
LRESULT CMainFrame::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam){ if(message==WM_NCPAINT ) { if(bShow){ ShowWindow(SW_SHOW); } else { ShowWindow(SW_HIDE); } } return CFrameWndEx::DefWindowProc(message, wParam, lParam);}
可以看出C++的类定义代码中,一次变化需要修改两个文件,其维护的繁琐令人诟病。
二、Java的改进
然而Java的出现彻底解决了这个问题,一个类就对应一个.java文件(包括后来其他面向对象语言也秉承了这个思路,比如C#)。
比如一个LogService类用于对日志进行维护,起初只包含日志的增删功能,LogService.java代码如下。
public class LogService{ public ServiceResult addLog (SysLogInfo logInfo) { ...... } public ServiceResult delLog (String id) { ...... }}
当需要增加一个updateLog方法时,仅需对LogService.java进行修改。
public class LogService{ public ServiceResult addLog (SysLogInfo logInfo) { ...... } public ServiceResult delLog(String id) { ...... } public ServiceResult updateLog (SysLogInfo logInfo) { ...... } }
一切变得方便了很多。
三、误用导致的退步
但是最近在看一些基于Spring(SpringBoot、SpringMVC)框架写的代码时,发现很多类的代码又回到了C++的形式。例如在使用一个LogService时,开发人员首先定义了一个interface,在LogService.java中:
public interface LogService { ServiceResult addLog(SysLogInfo logInfo); ServiceResult delLog(String id);}
然后定义了一个该接口的实现类,在LogServiceImpl.java中:
public class LogServiceImpl implements LogService{ @Override public ServiceResult addLog(SysLogInfo logInfo) { ...... } @Override public ServiceResult delLog(String id) { ...... }}
在需要实例化这个类的地方用了一个@Autowired注解注入。
public class LogController { @Autowired private LogService logservice;}
在问及开发人员为什么要象这样做时,其给了一个自信的回答:这是面向接口编程!
注意:这个设计中LogService.java类似于C++中的.h文件,LogServiceImpl.java类似于C++中的.cpp文件,这两个文件共同定义了一个LogService类。当需要给这个类添加一个updateLog方法时,LogService.java和LogServiceImpl.java都需要被修改,又走回了C++的老路。这显然是对面向接口编程的曲解。如果这样都能算面向接口编程的话,那么C++就成了一门天然的面向接口编程的语言,还何必去学那些复杂的设计模式。
不过这样写代码有什么问题吗?其实也没有太大问题,只是代码繁琐一点而已(C++就是这样的)。只不过既然你选择了Java语言,却又写成了C++的样子,就好像在开一辆自动挡的汽车,却一直拨到手动模式驾驶一样。
四、正确理解面向接口编程
那么什么才是面向接口编程呢,其要点在于:接口是基于变化的抽象。在有可能变化的地方才需要接口。假设上面的例子中,写日志的动作同时存在3种不同的实现:
1.写到日志文件。
2.写到数据库。
3.写到本地的一个日志服务的UDP端口。
那么可以基于这个接口写3个不同的实现类:
public class LogServiceFile implements LogService{}public class LogServiceDB implements LogService{}public class LogServiceUdp implements LogService{}
当然此时如果还是使用下面的代码会报错,因为Autowired只能装配对应接口的唯一一个派生类的Bean,而此时存在3个派生类。
public class LogController { @Autowired private LogService logservice;}
需要改进成类似下面这个样子,根据实际情况使用对应的派生类对象:
public class LogController { private LogService logservice; void writeLog(SysLogInfo logInfo){ logservice = GetLogServiceInst(); logservice.addLog(logInfo); }}
如果你的接口只有一个实现类,而且在可以遇见的将来也不会有其他实现类,那么还是建议你能简化一点,采用最基本的类定义方式,减少代码的复杂性。
以上是"Java怎么正确地使用依赖注入"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.