Questions tagged [inject]

Design pattern to reduce coupling between components, by dynamically injecting into a software component dependencies that it needs to function.

Synonym for Dependency Injection

Dependency injection (DI) is a design pattern for object-oriented programming involving dynamically injecting (inserting) into a software component dependencies (service components) that it needs to function, without needing the dependent component to hard-code a dependency on the service. This reduces coupling between the dependent consumer and the service.

Resources / background questions

If you need a general introduction to DI you should refer to this question: What is dependency injection?

If you need a non-technical introduction you can refer to this question: How to explain Dependency Injection to a 5-year old.

If you would like to understand the relationship between DI and Inversion of Control (IoC), see Inversion of Control < Dependency Injection.

For book recommendations see Dependency Injection book recommendation(s)(dead link).

For general recommendations for writing DI-friendly code without a DI Container, see Dependency Inject (DI) “friendly” library.

If you are wondering why you should use a DI Container instead of Poor Man's DI, see Why do I need an IoC container as opposed to straightforward DI code?

If you are wondering what the Composition Root is, see What is a composition root in the context of Dependency Injection.

For potential downsides of using DI, see What are the downsides to using dependency injection?

Dependency injection and Inversion of Control are closely related. The difference between them is discussed at where-exactly-is-the-difference-between-ioc-and-di.

Also can read basic here : Dependency Injection For Beginner - 1

Related Patterns

1000 questions
6
votes
2 answers

Spring Bean implementing multiple interfaces

I have a bean which implements two interfaces. The barebones code is as follows: interface InterfaceA { ... } interface InterfaceB { ... } public class ClassC implements InterfaceA, InterfaceB { ... } In my AppConfig I am specifying the…
341008
  • 9,862
  • 11
  • 52
  • 84
6
votes
3 answers

How to inject in a parent class in Aurelia?

I have a parent class where I want to inject some modules, then I have some derived classes where I would like to use these injected modules. However in the derived class you have to call super() without parameters, so injected modules in parent…
Alex Garcia
  • 607
  • 6
  • 10
6
votes
1 answer

How to use injected services in static methods

I want to use angularjs and typescript together. I'm trying to create Orm factory with typescript and stacked with some problem. I defined my factory class as: class OrmModel implements IOrmModel { static $inject = ['$http', '$q', 'config']; …
Ivan Burnaev
  • 2,690
  • 18
  • 27
6
votes
3 answers

Guice inject based on annotation value

I would like to use goolge/guice inject a value based on a class i provide with the annotation. AutoConfig annotation @BindingAnnotation @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.PARAMETER, ElementType.FIELD }) public @interface…
ST-DDT
  • 2,615
  • 3
  • 30
  • 51
6
votes
1 answer

Two custom injection annotations in Jersey 2

How should I do the ValueFactoryProvider binding in order to have two custom injection annotations coexist in Jersey 2? Below I have included an example of my current approach and as you can see the Hello annotation injection "hides" the SmallTalk…
Stine
  • 1,605
  • 5
  • 23
  • 44
6
votes
1 answer

using inject to count elements in array

I'm trying to count the occurrences of elements in an array and save it in a hash. I'd like to use the inject function. I have this code: a = ["the","the","a", "it", "it", "it"] a.inject(Hash.new(0)) {|hash,word| hash[word] += 1} I don't…
Selah
  • 7,728
  • 9
  • 48
  • 60
6
votes
1 answer

JSF Deploy Exception: Unsatisfied dependencies for type EntityManager

I just followed the ticket-monster tutorial(http://www.jboss.org/jdf/examples/ticket-monster/tutorial/Introduction/) and added a rest-service class to my solution. package projectFoo.rest; import java.util.List; import…
Lama
  • 2,886
  • 6
  • 43
  • 59
6
votes
1 answer

inject script inside iframe of different domain

i was trying to inject script inside an iframe element trying to implement this way, child and parent does not belong to same domain (i know XSS is prevented in latest browsers) is there any way to inject script to child element of button click on…
Bhargav Krishna
  • 227
  • 4
  • 12
6
votes
2 answers

Calling a function in an injected DLL?

Using C++, I have an application which creates a remote process and injects a DLL into it. Is there a way to get the remote application to execute a function exported from the DLL, from the application which created it? And is it possible to send…
RectangleEquals
  • 1,825
  • 2
  • 25
  • 44
6
votes
5 answers

Can someone explain a real-world, plain-language use for inject in Ruby?

I'm working on learning Ruby, and came across inject. I am on the cusp of understanding it, but when I'm the type of person who needs real world examples to learn something. The most common examples I come across are people using inject to add up…
ckbrumb
  • 191
  • 2
  • 11
5
votes
2 answers

Injecting into a Jersey Resource class

I did try going through the following links How to wire in a collaborator into a Jersey resource? and Access external objects in Jersey Resource class But still i am unable to find a working sample which shows how to inject into a Resource class. I…
su_
  • 55
  • 1
  • 1
  • 5
5
votes
2 answers

Proxy Security Service for Web Service requiring Uname/Password in the Request

We have a vendor supplied solution that requires a username and password to utilize their APIs exposed as a web service. They are to be included in the actual xml of the call. We obviously don't like that. Does anyone know of any product, system,…
Bill Rosmus
  • 2,941
  • 7
  • 40
  • 61
5
votes
1 answer

Mootools: inject vs adopt

I want to dynamically add some preconfigured HTML-Elements in use of a 'click'-event with mootools. So I can make it work with my basic knowledge, although it isn´t very nifty. I coded this so far... This is my preconfigured element, with some text,…
Micha
  • 53
  • 1
  • 3
5
votes
1 answer

Sling @Inject NullPointerError, When @Reference Successful

When trying to @Inject (javax.inject.Inject) to inject MyConfigurationService within @SlingServlet MyServlet leads to a NullPointerError anytime any operations are attempted on myConfigurationService within an AEM OSGi container using Maven…
Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
5
votes
1 answer

What is the difference between AspectJ And ASM?

As I understand, the 2 frameworks are both static that injects monitor codes into class codes. So, what is the difference?
Viyu
  • 51
  • 4