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
3
votes
1 answer

Factory pattern with Unity Buildup trouble

My demo code is very simple using Microsoft.Practices.Unity; using System; public interface IDecorator { string GetA(); } public class Decorations:IDecorator { public string GetA() { return "temp"; } } public class…
allencharp
  • 1,101
  • 3
  • 14
  • 31
3
votes
1 answer

Karma Jasmine testing inject not working

I am new to karma unit testing and I have encountered this problem to a simple Ionic Application. This is my controller: angular.module('starter').controller('AccountCtrl', function ($scope) { $scope.settings = { enableFriends: true …
3
votes
3 answers

Equivalent for @Conditional in CDI

I have two classes with post construct initialization, and i need one of them to be injected based on a vm argument. I have done this kind of conditional injection in spring using @Conditional annotation, however i could not find any equivalent in…
vamsi
  • 325
  • 5
  • 15
3
votes
1 answer

Is null check of field annotated with javax.inject.Inject required

I have a class where I have injected a field using javax.inject.Inject annotation import javax.inject.Inject; public class Foo extends BaseFoo { @Inject private Bar bar; ... public void execute(){ if (bar == null) { //log failure…
Learner
  • 1,503
  • 6
  • 23
  • 44
3
votes
3 answers

Injecting Constructor Parameter value using Google Guice

I have a simple requirement - This is the service where I want to inject the object of CalledService. public class CallingService { private CalledService service; @Inject public CallingService(CalledService svc) { service =…
Rito
  • 3,092
  • 2
  • 27
  • 40
3
votes
3 answers

How does using Array#reduce in this way work?

I have been learning ruby for the last few weeks and I came across something like: array = [10, 20, 20]; array.reduce(:^) # => 10 which evaluated to 10. The purpose of the code was to find an element with an odd number of occurrences within a…
hellothere1
  • 85
  • 1
  • 4
3
votes
1 answer

Guice named inject in eager singleton

There is Java class which should have int MY_LISTENER_PORT with injected my.listener.port value from a properties file: @Singleton public class MyListener { @Inject @Named("my.listener.port") private int MY_LISTENER_PORT; public…
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
3
votes
1 answer

Inject html in polymer including data binding

I have a trusted html file with data-bindings, which I want to include to a web component.I tried multiple ways to include the html file, but the data-binding doesn't work. I know polymer won't stamp the html, because it becomes a vulnerability for…
aphex
  • 3,372
  • 2
  • 28
  • 56
3
votes
1 answer

Java injection inside @Asynchronous bean

I have 2 beans that use Injection to "pass" UserData info that is extracted from HttpRequest. If I remove @Asynchronous from WorkerBean then its all working and WorkerBean can access UserInfo thats injected down. However if I use @Asynchronous on…
Andrei V
  • 1,468
  • 3
  • 17
  • 32
3
votes
1 answer

why doesn't my refactored ruby using inject work?

I tried to do some refactoring to convert an each block into an inject, but it didn't work and I don't understand why. Here's the code that works before refactoring: class String # Build the word profile for the given word. The word profile is an…
Stewart Johnson
  • 14,281
  • 7
  • 61
  • 70
3
votes
1 answer

what is diff in @Injectable() and @Inject

I am working on angular2 I have created service and inject those service using @Inject in component. I am confuse with the use of @Injectable() in service itself and what diff it makes.
Rhushikesh
  • 3,630
  • 8
  • 45
  • 82
3
votes
4 answers

Spring Boot : Use @Autowired within a Quartz Job

I managed to configure and schedule a Quartz job using JobStoreTX persistent store in Spring Boot ( version 4.2.5 ). Here is how I schedule the job. First : public class MyJob implements Job{ @Autowired IService service; @Override …
Daniel
  • 584
  • 3
  • 8
  • 20
3
votes
1 answer

Dependency injection into controller using $inject

I am trying to inject a number of dependencies into a controller as part of a code refactoring process inline with John Papa's Style Guide. Right now, our controller looks like this: .controller('alerting-settings.mainController', [ '$scope', …
Marco V
  • 2,553
  • 8
  • 36
  • 58
3
votes
1 answer

Is importing text via CSS really permitted?

There is a html page that I have no control over, ditto goes for any javascript. I can however style it, and one of the thing's I've done is injected a slab of text via content in a CSS pseudo-element. However this slab is multiple lines and with…
Hashbrown
  • 12,091
  • 8
  • 72
  • 95
3
votes
1 answer

Setting multiple instances for Verticles using vertx embedded and Guice

I am using Vertx cluster on the same machine(dev mode). I am using guice to Inject some pojo's as well. However when I try to increase Verticle instances I get: java.lang.IllegalArgumentException: Can't specify > 1 instances for already created…
rayman
  • 20,786
  • 45
  • 148
  • 246