What is the difference between dependency injection and dependency?

What is addiction? Can someone clarify these two concepts.

+5
source share
3 answers

A dependency search is when the object itself tries to find a dependency, for example:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml"); MyBean bean = applicationContext.getBean("myBean") 

Here, the class itself initializes the ApplicationContext via XML, and it searches in context for the bean called myBean in ApplicationContext

Enabling dependencies is when a property is automatically bound when istance is initialized. For instance:

in application-context.xml , we have one line that initializes the bean and another to initialize the object, say MyClass :

 <bean id="myBean" class="org.mypackage.MyBean"/> <bean id="myClass" class="org.mypackage.MyClass"/> 

Then in MyClass you have something like:

 @Component public class MyClass{ @AutoWire MyBean myBean; 

In this case, you indicated that two of the two beans are initialized. And myClass bean has a property myBean, which is already initialized due to injection

+4
source

Since @Michael Zucchetta explained what the difference is in the context of Spring, I will try to give you a more general explanation. The main difference between the two approaches is "who is responsible for getting the dependencies."

Usually in DI (dependency injection) your component does not know about the DI container, and the dependencies automatically appear “automatically” (for example, you simply declare some parameters of setters / constructors, and the DI container fills them for you).

In, DL (dependency search) you must specifically ask what you need. In practice, this means that you depend on the context (in the spring context of the application) and extract everything you need from it.

You can take a look at M. Fowler's ServiceLocator vs DependencyInjection for a better explanation, but I will give you a quote:

The key difference is that with the Locator of the service, each user of the service has a locator dependency. The locator may hide dependencies on other implementations, but you need to see the locator. Therefore, the decision between the locator and the injector depends on whether this dependence is a problem.

Using dependency injection can help simplify the viewing of component dependencies. With the dependency injector, you can just look at the injection mechanism, such as the constructor, and see the dependencies. Using a service locator, you should look for the source code for locator calls. Modern IDEs with a link lookup feature facilitate this, but it's still not as simple as looking at the constructor or customization methods.

Hope this helps.

+3
source

Dependency search is a more traditional approach:

  • Component
  • must request a dependency link from the JNDI registry

We can achieve this in two ways:

1. Dependence

  ApplicationContext ctx = new ClassPathXmlApplicationContext ("META-INF/spring/app-context.xml"); SimpleBean mr = ctx.getBean("renderer", SimpleBean.class); 

2. Contextualized dependency search (the search is retrieved directly from the container, not from the registry). A component must implement a specific interface in order to be able to search

 public interface Container { Object dependencyLookup(String key);} public class CDL implements Container { private Dependency dependency; @Override public void performLookup(Container container) { this.dependency = (Dependency) container.dependencyLookup("myDependency"); } 

}

Dependency inclusion is more controversial (but more flexible, scalable):

  • Dependencies
  • are introduced into the component

There are two ways to do this:

  • Constructor

  • Setter dependency injection.

0
source

Source: https://habr.com/ru/post/1211481/


All Articles