In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "DI dependency injection sample Analysis of Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Java's DI dependency injection sample Analysis".
Dependency injection (DI) is a technique that allows classes to receive their dependencies externally. If class A uses class B, class A depends on class B, and B is a dependency of A.
The following example shows what the dependencies and DI are in Java. In the first example, class A depends on class B because B is a member of A. An and B are tightly coupled. Whenever B changes, A must change. This situation is called hard dependency.
/ / hard dependencyclass A {private B b; public A () {this.b = new B ();}.}
In the second example, A still depends on B, but the dependency is not hard-coded. It is decoupled by using parameters in the constructor. If A requires a different implementation of B, A can use a different implementation of B to construct an instance. This leads to a key feature of DI: the injected class should be an abstract interface so that different implementations can be injected into A. If there is only one implementation of B, there is no need for DI.
/ / dependency injection through constructorclass A {private B b; public A (BB) {this.b = b;}.} benefits of using dependency injection
An example use of DI is data access objects (DAO). Classes that perform CRUD operations usually need to access the database. Use DI to inject DAO into the application to decouple the application layer from the data persistence layer. If the underlying database changes, the application class can be changed to a different DAO as long as the DAO implements the same interface. Another benefit is to make unit testing easier. Unit tests can use fake (hard-coded or in-memory) DAO to test application logic without worrying about underlying database access.
DI is a key technology used in popular Java frameworks such as Spring and Hibernate. Instead of manually creating B objects and passing them to A's constructor, the framework uses reflection to create dependent objects and inject them into the appropriate location based on configuration.
A simple example of dependency injection
Here is a simple example to illustrate what DI looks like when using a framework and the two benefits of using DI. I use the Guice framework, but other frameworks work in the same way behind the scenes.
Suppose we have a computer that has many parts that work together, such as CPU, memory, and so on. There are two ways to do this in CPU.
Public interface CPU {public void start (); public int getUsage ();}
CPU can be Intel.
Public class Intel implements CPU {@ Override public void start () {System.out.println ("Intel is started.");} @ Override public int getUsage () {return new Random () .nextInt;}}
Or AMD.
Public class Amd implements CPU {@ Override public void start () {System.out.print ("Amd is started");} @ Override public int getUsage () {return new Random () .nextInt;}}
In Guice, injecting dependencies through a constructor is as simple as adding a @ Inject annotation.
Public class Computer {private CPU cpu; @ Inject Computer (CPU cpu) {this.cpu = cpu;} public void start () {cpu.start (); / / start other parts} public boolean isStatusOk () {/ / assuming this random if (cpu.getUsage () > 50) {return false;} / / check other things, such as memory, hard drives. Return true;} public static void main (String [] args) {Injector injector = Guice.createInjector (new BasicModule ()); Computer computer = injector.getInstance (Computer.class); computer.start (); System.out.println ("Status:" + (computer.isStatusOk ()? "OK": "Not OK");}}
Guice uses modules to configure injection. In this example, when a CPU is requested, the module binds the specific Intel to the CPU.
Public class BasicModule extends AbstractModule {@ Override protected void configure () {bind (CPU.class) .to (Intel.class);}}
The benefits of this are obvious. Computers have the flexibility to use other types of CPU when needed. In addition, if CPU depends on another class, such as Cache or Clock, we can inject dependencies in the same way without coupling those classes.
For the second benefit of making unit testing easier, we can do a simple unit test to test the isStatusOk () method. In practice, CPU usage can be a random number based on actual usage. If we want to focus our testing on other parts of the method, we can simulate CPU usage, assume that CPU usage is fine, and then test the rest.
Public class ComputerTest {@ Test public void testIsStatusOk () {CPU cpu = mock (CPU.class); / / mock cpu usage, so we can focus on testing other part when (cpu.getUsage ()) .thenReturn (10); assertTrue (new Computer (cpu). IsStatusOk ());}}
In short, DI is to separate the concerns of object creation and usage. DI decouples class dependencies and makes unit testing easier.
At this point, I believe you have a deeper understanding of "Java's DI dependency injection sample analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.