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
5
votes
0 answers

Inject a service on the console in angular2

How do I inject a service in angular2 in the console? In angular1 just wrote: angular.element(document).injector().get('MyService')
JV3
  • 872
  • 2
  • 9
  • 21
5
votes
3 answers

Inject a list of classes that extends an abstract class with Spring

Hy, I have the next code: public abstract class MyClass{ protected abstract void method1(); } Classes that extend the first one: @Component public class MyClass1 extends MyClass{ ..... } @Component public class MyClass2 extends MyClass{ …
fernando1979
  • 1,727
  • 2
  • 20
  • 26
5
votes
2 answers

How to pass parameter to injected class from another class in CDI?

I am new to CDI, tried to find solution for this question, but, couln't found any. Question is Suppose I have one class which is being injected(A) from, where some value(toPass) is getting injected, now I want to pass this same value(toPass) to…
Krishna Kumar
  • 511
  • 1
  • 9
  • 21
5
votes
3 answers

Map with accumulator on an array

I'm looking to create a method for Enumerable that does map and inject at the same time. For example, calling it map_with_accumulator, [1,2,3,4].map_with_accumulator(:+) # => [1, 3, 6, 10] or for strings ['a','b','c','d'].map_with_accumulator…
veksev
  • 90
  • 7
5
votes
6 answers

How to insert html in iframe

Hallo all: I need to insert a html string in a iframe as shown below: .... var html = "Titolo

body

" jQuery('#popolaIframe').click(function() { …
Massimo Ugues
  • 4,373
  • 8
  • 43
  • 56
5
votes
2 answers

How to inject a ViewStub with ButterKnife?

I want to use a ViewStub with ButterKnife, This is what I've done: public class ExampleFragment extends Fragment { @InjectView ( R.id.stub ) ViewStub mStub; /* A TextView in the ViewStub */ @InjectView ( R.id.text ) @Optional …
RockerFlower
  • 727
  • 2
  • 10
  • 28
5
votes
2 answers

Order of creating @Inject objects

Look at following class (please note, that it is not a singleton): public MyClass() { @Inject private A a; @Inject private B b; } What object will be created first a or b? Is there a possibility of determine the order of creating…
jtomaszk
  • 9,223
  • 2
  • 28
  • 40
5
votes
1 answer

Lazy init an injected bean dependency (Spring 3)

How to lazy init a dependency that is @Inject? public class ClassA { @Inject ClassB classB; } @Configuration public class Config { @Bean public ClassA classA() { return new ClassA(); } @Bean @Lazy public ClassB…
Ana
  • 312
  • 2
  • 9
5
votes
1 answer

Java EE 6 @Inject lazy?

I am doing some refactoring and reviewing of the application that we are currently developing. While doing this I found that more beans are injected and I think making they loading in an lazy manner would be better suited for their purpose. I am…
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
5
votes
1 answer

Injecting a stateless EJB into Servlet

I'm trying to inject a stateless EJB into servlet. But it is not working. Did I understand something wrong? If I do this in a @WebService annotated class, I can use the injected EJB without problems. My EJB: @Stateless public class…
blaine
  • 155
  • 1
  • 10
5
votes
2 answers

Editing string with it's starting address in memory in C++

I really hope this hasn't been asked before, but my googling came up with nothing so I figured I would ask. I have a small script I am working on where I inject a DLL into a game and edit a particular string that gets displayed when a particular…
haze
  • 239
  • 2
  • 11
5
votes
1 answer

How do I stop a Python process instantly from a Tkinter window?

I have a Python GUI that I use to test various aspects of my work. Currently I have a "stop" button which kills the process at the end of each test (there can be multiple tests set up to run at once). However, some tests take a long time to run and…
Brad Conyers
  • 1,161
  • 4
  • 15
  • 24
5
votes
1 answer

Understanding the behaviour of inject used with a lambda in Ruby

I often plug pre-configured lambdas into enumerable methods like 'map', 'select' etc. but the behavior of 'inject' seems to be different. e.g. with mult4 = lambda {|item| item * 4 } then (5..10).map &mult4 gives me [20, 24, 28, 32, 36,…
Mike Berrow
  • 2,546
  • 1
  • 20
  • 16
4
votes
2 answers

Inject byte[] exported from MethodInfo.GetMethodBody()

I was wondering, if it's possible to inject byte[] code that was previously exported from an method via GetMethodBody() back, meaning, that it can be runnable code again. If someone could write pretty simple example or explanation I'd…
deadmau5
  • 95
  • 2
  • 7
4
votes
2 answers

Need to instrument Javascript: function calls / args - ANTLR?

I need to take a load of Javascript and instrument it automatically: specifically I want to log every call to a function, and provide a list of arguments that the function was invoked with. I have a half-baked way of doing this with Python : using…
monojohnny
  • 5,894
  • 16
  • 59
  • 83