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

C# injecting C++ DLL

I am trying to inject a C++ detour dll using a C# application using Syringe. Here is what I have so far: -A dll to inject which hook both recv/send from wsock32.dll and log the communication (http://pastebin.com/JvnmYuz0) -A C++ loader which start…
Feeh
  • 33
  • 1
  • 5
3
votes
1 answer

How to cast from super class to derived class in field injected by CDI?

I'm using JSF 2.1 with CDI and JBoss 7.1.1 Is it possible to inject with CDI in a super class variable principal and cast to derived class ? In example MyUserPrincipal is derived class. If I write @Inject Principal principal I know from debugging…
Tony
  • 2,266
  • 4
  • 33
  • 54
3
votes
3 answers

Spring configuration in GWT Project?

I am developing a GWT-Spring-Hibernate project and I want to use Spring Autowired annotation in GWT Service Servlet but my autowired annotated service is not injected. it is null. Is there a configuration detail that I missed? I…
firstthumb
  • 4,627
  • 6
  • 35
  • 45
3
votes
1 answer

Symbol vs. Proc in `Enumerable#inject`

Given that Enumerable#inject can take either a symbol or a block as the method to be used in the iteration, as explained in an answer to this question, is there any reason to use the & in conjunction with Symbol#to_proc within Enumerable#inject? The…
sawa
  • 165,429
  • 45
  • 277
  • 381
3
votes
2 answers

Spring inject values

I am very new to spring and started using. I have a requirement where i have something like properties like regions..US,UK Regions ------- US UK And when i read US it shud have values something…
3
votes
1 answer

Using a decorator to inject a function in Python

import functools import logging def logDeprecated( msg=None, *args, **kwargs ): """Log a message at level 'WARNING' Args: msg=None : (str) The message you wish to log. If no msg is passed, default to 'This…
Stephen Lu
  • 314
  • 1
  • 9
3
votes
1 answer

Incorporating External Html into a jQuery Mobile page

I am attempting to dynamically incorporate an external source of html into a jQuery mobile page. I am able to successfully incorporate the external html but it looks like regular HTML (i.e. not jQuery mobile affected Html). Can any one suggest what…
Ben Pearce
  • 6,884
  • 18
  • 70
  • 127
3
votes
2 answers

@Inject + @Qualifier vs @Inject implementation

What's the different between using a qualifier and just specifying the exact implementation class you want? This question is copied from: http://docs.jboss.org/weld/reference/latest/en-US/html/injection.html
3
votes
2 answers

Inject Keystroke to different process using Bash

I have a process that runs indefinitely until a key is pressed. I would like to use bash to inject a keystroke into this process to have it terminate. Based on this post, linux - write commands from one terminal to another I have tried to use echo…
Max
  • 31
  • 1
  • 2
3
votes
2 answers

Dynamic Spring bean creation

I'm using an API that exposes services in the form of XXXLocalServiceUtil classes which are static wrappers of singleton objects. Instead of using the static XXXLocalServiceUtil methods I want to inject the XXXLocalService objects themselves to use…
p.mesotten
  • 1,402
  • 1
  • 14
  • 26
3
votes
1 answer

Spring 3 receive servletContext in custom bean

my problem is that I can't get servletcontext in my bean. I created custom bean "FileRepository" and I need to get ServletContext there. here is the code package com.pc.webstore.utils; import java.io.File; import java.nio.file.Files; import…
yaroslav prokipchyn
  • 472
  • 2
  • 8
  • 17
3
votes
1 answer

inject static method with *args, receiving type as the first argument

I have several class where I need to inject a static method; this static method should be called with type (not instance) as the first argument, and pass all remaining args to the implementation (the example at ideone): # function which takes class…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
2
votes
2 answers

Capturing, forging and injecting packets

In order to build a measurement tool, I need to capture packets, forge them and re-inject them into the network. What are some tools or libraries that I could look into? Thanks.
Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185
2
votes
1 answer

Inject mock object using jMock

I'm trying to use jMock to create a mock object. The mock object looks fine - but the thing is it is not injected to the tested class. Here is my code: @RunWith(JMock.class) public class FeederFilterTest { private TestedClass…
Noam
  • 3,049
  • 10
  • 34
  • 52
2
votes
3 answers

Chrome Extension: How to detect if an extension is installed using Content Scripts

I am asking this question after looking at several related questions on stackoverflow. I started with how to detect if an extension is installed. I opted for the method where I add a div to body using content scripts on some pages. Here is how I did…
vikmalhotra
  • 9,981
  • 20
  • 97
  • 137