Java知識分享網 - 輕松學習從此開始!????

Java知識分享網

Java1234官方群25:java1234官方群25
Java1234官方群25:838462530
     

GIT視頻教程(結合github,碼云)免費領取

BAT一線大廠筆試面試打包下載

java1234.vip企業級站點全新上線(可以領取站點源碼)

SpringBoot打造企業級進銷存

Java1234 VIP課程

領取微信掃碼登錄Java實現視頻教程

Java1234至尊VIP(特價活動)

spring框架外文文獻 PDF 下載


分享到:
時間:2020-07-10 14:17來源:http://www.luygg.com 作者:小鋒  侵權舉報
spring框架外文文獻 PDF 下載
失效鏈接處理
spring框架外文文獻 PDF 下載

本站整理下載:
提取碼:qjri 
 
相關截圖:
 
主要內容:

 
Dependency Injection
The concept of dependency injection (DI), objects are given their dependencies at construction time, is one of the foundations of the Spring Framework. You have also probably heard of Inversion of Control (IoC). IoC is a broader, more general concept that can be addressed in different ways. IoC lets developers decouple and focus on what is important for a given part of an enterprise application, but without having to think about what other parts of the system do. Programming to interfaces is one way to think about decoupling.
Almost every enterprise application consists of multiple components that need to work together. In the early days of Java enterprise development, we simply put all the logic of constructing those objects (and the objects those objects needed) in the constructor (see Listing 2-1). At first sight, there is nothing wrong with that approach; however, as time progressed, object construction became slow, and objects had a lot of knowledge they shouldn’t have had (see the Single Responsibility Principle). Those classes became hard to maintain, and they were also quite hard to unit and/or integration test.
 
The class from Listing 2-1 programs to interfaces, but it still needs to know about the concrete implementation of an interface simply to do object construction. Applying IoC by decoupling the construction logic (collaborating objects) makes the application easier to maintain and increases testability. There are seven ways to decouple this dependency construction logic:
1. Factory pattern
2. Service locator pattern
3. Dependency injection
a. Constructor based
b. Setter based
c. Interface based
d. Annotation driven
4. Contextualized lookup
When using the factory pattern, service locator pattern, or contextualized lookup, the class that needs the dependencies still has some knowledge about how to obtain the dependencies. This can make things easier to maintain, but it can still be hard to test. Listing 2-2 shows a contextualized lookup from JNDI (Java Naming and Directory Interface). The constructor code would need to know how to do the lookup and handle exceptions.
 
The immediately preceding code isn’t particularly clean; for example, imagine if there were multiple dependencies from different contexts. The code would quickly become messy and increasingly hard, if not impossible, to unit test (see Chapter 9 for more information on testing).

 
 
------分隔線----------------------------
鋒哥公眾號


鋒哥微信號


關注公眾號
【Java資料站】
回復 666
獲取 
66套java
從菜雞到大神
項目實戰課程
日本色在线