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

Mock service injected into Angular module run block

In my module.run block it is calling a method on a service I have made. When running my tests I want it to reference a mock service instead of the real one which is making http requests. I am currently trying to test a controller, not the actual run…
Heather Roberts
  • 1,978
  • 1
  • 24
  • 33
3
votes
2 answers

How to resolve @Inject that gives NullPointerException?

I have been looking for a possible solution why the @Inject object I'm using is returning null. I looked all over stackoverflow and I also bump into this site Solving @Inject and Null Pointer Exception and Im sure that Ive already taken those into…
bencampbell_14
  • 587
  • 2
  • 10
  • 32
3
votes
1 answer

Laravel 5.1 > use @inject within view to display content

I´m working on a small Web-Application which will be having a private messaging system. While a user is signed in his received or written messages will be displayed in a toolbar area inside a dropdown menu. Nothing new :-) The toolbar will be…
Kristo
  • 547
  • 1
  • 7
  • 20
3
votes
3 answers

Ruby inject until sum exceeds a set value and return the index where this occurs

With the following array and value: v = 50 a = [10, 20, 25, 10, 15] I want to iterate through the array adding up the values until the sum of these values exceeds the variable v. And then I want to be able to return the index in the array where…
treeseal7
  • 739
  • 8
  • 22
3
votes
1 answer

can't inject Providers mock in JerseyTest

I'm getting Providers from context in my filter to get defined ObjectMapper public class Filter implements ContainerRequestFilter, ContainerResponseFilter { @Context private Providers providers; @Context private HttpServletRequest…
Capril Aprilovich
  • 425
  • 1
  • 6
  • 17
3
votes
2 answers

Guice : Inject an ArrayList of Strings

I'm trying to inject an ArrayList of Strings with the help of Guice. I want to show a panel with many RadioButtons (for example) where an user can select some services to activate. Once selected, I would like to get all the names of the selected…
Jacks
  • 587
  • 5
  • 28
3
votes
1 answer

PHP-DI annotation not working

I have installed php-di 4.4 in a new custom project using composer.Im runing a xampp localhost with php 5.6.3 but I set netbeans for php 5.4 on this project. Im new to php-di, I did use annotations in my android projects but cant seem to make it…
MrDrishu
  • 100
  • 8
3
votes
0 answers

GWTP & Inject error

I recive an error when try to call service private void startSendPacket() { Timer timer = new Timer() { @Override public void run() { packetService.sendPacketMSGToServer(str, new…
LeshaRB
  • 1,345
  • 2
  • 23
  • 44
3
votes
2 answers

Spring creating bean but not injecting it

I know theres a ton of questions regarding problems with Spring Autowired, but I havent been able to find anything similar to mine, so sorry if its a dupe... Im having problems with autowiring a bean that is created (debugging shows that the…
OsaSoft
  • 589
  • 5
  • 19
3
votes
0 answers

@Dependent @javax.ejb.Singleton versus @ApplicationScoped @javax.ejb.Singleton?

In essence, what is the difference between these two classes: @ApplicationScoped @Singleton class A {} @Dependent @Singleton class B {} Contextual EJB instances I prefer not to use @Inject when looking for EJB:s, unless the EJB is a @Stateful and…
Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
3
votes
1 answer

Jersey : use @Inject in ResourceConfig

I've a Configuration.java singleton class (with many properties loaded by a file) that I would like to inject in some classes in my Application. So, I bind my injection in the ResourceConfig class with an AbstractBinder. Now, I need to use this…
Guillaume
  • 694
  • 1
  • 6
  • 15
3
votes
1 answer

Is it possible to inject JS to Android WebView before the page is loaded?

I need to add some properties to the window object before a page is loaded and its JS code is executed. I have a couple of reasons to do it, but one is to add support for HTML5 APIs which are not yet supported in Android WebView. If I inject the JS…
3
votes
4 answers

Sorting nested hash in ruby

Provided the following ruby hash: { cat: { 1: 2, 2: 10, 3: 11, 4: 1 }, wings: { 1: 3, 2: 5, 3: 7, 4: 7 }, grimace: { 1: 4, 2: 5, 3: 5, …
Rabbott
  • 4,282
  • 1
  • 30
  • 53
3
votes
1 answer

why @Inject cannot work but @EJB works in JavaEE 7?

I am going through JavaEE7-samples repo in github. I found that the author has mentioned the following in this code snippet: // Cannot be injected using @Inject @EJB Cart bean; The author explicitly mentions that @Inject will not work. But I am…
brain storm
  • 30,124
  • 69
  • 225
  • 393
3
votes
4 answers

Is it possible to "inject" a .NET dll into another .NET application, by way of app.config perhaps?

I have made a .NET class library in C# that initializes some logging, sent to an external tool. The library is altogether separate from any application, but in order to initialize it, I need to make at least one method call to it. Is there a way for…
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825