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
789
votes
11 answers

What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

I am going through some blogs on SpringSource and in one of the blogs, author is using @Inject and I suppose he can also use @Autowired. Here is the piece of code: @Inject private CustomerOrderService customerOrderService; I am not sure about the…
Rachel
  • 100,387
  • 116
  • 269
  • 365
135
votes
8 answers

Inheritance and dependency injection

I have a set of angular2 components that should all get some service injected. My first thought was that it would be best to create a super class and inject the service there. Any of my components would then extend that superclass but this approach…
maxhb
  • 8,554
  • 9
  • 29
  • 53
119
votes
3 answers

Is inject the same thing as reduce in ruby?

I saw that they were documented together here. Are they the same thing? Why does Ruby have so many aliases (such as map/collect for arrays)? Thanks a lot.
Jacky
  • 8,619
  • 7
  • 36
  • 40
103
votes
20 answers

inject bean reference into a Quartz job in Spring?

I managed to configure and schedule a Quartz job using JobStoreTX persistent store in Spring. I do not use Spring's Quartz jobs, because I need to schedule them dynamically, at run time, and all examples of integrating Spring with Quartz that i…
Marina
  • 3,894
  • 9
  • 34
  • 41
83
votes
7 answers

Is it possible to inject interface with angular2?

i wonder if there is a proper way to inject interfaces in Angular2? (cf. below) I think this is related with the missing @Injectable() decorator on the interface, but it seems this is not allowed. Regards. When CoursesServiceInterface is implemented…
user1568220
  • 1,018
  • 1
  • 8
  • 10
56
votes
7 answers

How to sum properties of the objects within an array in Ruby

I understand that in order to sum array elements in Ruby one can use the inject method, i.e. array = [1,2,3,4,5]; puts array.inject(0, &:+) But how do I sum the properties of objects within an object array e.g.? There's an array of objects and…
Spike Fitsch
  • 747
  • 1
  • 7
  • 12
47
votes
9 answers

Define AngularJS directive using TypeScript and $inject mechanism

Recently I started refactoring one of the Angular projects I am working on with TypeScript. Using TypeScript classes to define controllers is very convenient and works well with minified JavaScript files thanks to static $inject Array
Milko Lorinkov
  • 600
  • 1
  • 5
  • 8
36
votes
3 answers

Difference between javax.inject.Singleton and javax.ejb.Singleton

im little confused. What is the exact difference between javax.inject.Singleton and javax.ejb.Singleton?
Hakan Kiyar
  • 1,199
  • 6
  • 16
  • 26
33
votes
4 answers

Getting Unknown Provider error when injecting a Service into an Angular unit test

I'm fairly new to Angular and have reviewed all the similarly related questions on Stack Overflow but none have helped me. I believe I have everything set up correctly but am still getting an 'Unknown Provider' error when attempting to inject a…
Dud
  • 361
  • 1
  • 3
  • 6
33
votes
3 answers

Superclass has no null constructors but no arguments were given

Im using Spring Social in my…
nilsi
  • 10,351
  • 10
  • 67
  • 79
31
votes
1 answer

http_sub_module / sub_filter of nginx and reverse proxy not working

I am trying to reverse proxy my website and modify the content. To do so, I compiled nginx with sub_filter. It now accepts the sub_filter directive, but it does not work somehow. server { listen 8080; server_name www.xxx.com; …
thms0
  • 494
  • 1
  • 7
  • 15
30
votes
3 answers

Ruby Print Inject Do Syntax

Why is it that the following code runs fine p (1..1000).inject(0) { |sum, i| sum + i } But, the following code gives an error p (1..1000).inject(0) do |sum, i| sum + i end warning: do not use Fixnums as Symbols in `inject': 0 is not a…
Verhogen
  • 27,221
  • 34
  • 90
  • 109
29
votes
1 answer

What is the difference between javax.inject.Inject and com.google.inject.Inject?

I'm initiating myself to Google Guice. I have a simple question : What is the difference between the javax.inject's @Inject annotation and the com.google.inject's @Inject one ? Thanks.
Maoori
  • 293
  • 1
  • 3
  • 4
25
votes
4 answers

How to inject HttpClient in static method or custom class?

I'd like to use angular HttpClient in static method or class (in class it can't be defined as constructor parameter). I tried something like: export class SomeNotInjectableService { static doSomething() { const injector = Injector.create({ …
elzoy
  • 5,237
  • 11
  • 40
  • 65
22
votes
4 answers

Use Spring to inject text file directly to String

So I have this @Value("classpath:choice-test.html") private Resource sampleHtml; private String sampleHtmlData; @Before public void readFile() throws IOException { sampleHtmlData = IOUtils.toString(sampleHtml.getInputStream()); } What I'd like…
rozner
  • 580
  • 2
  • 5
  • 11
1
2 3
66 67