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
4
votes
3 answers

Google Guice desktop application - how to make it work?

I have used Guice in my web app without problems and I wanted to use it in desktop app. I am certainly missing one thing - some way to tell my app how to bind everything and know what is what. In web app, I had declaration for that in Application…
Paul
  • 389
  • 3
  • 11
4
votes
1 answer

spring-security writing a custom PermissionEvaluator - how to inject a DAO service?

I'm working with Spring-Security and I need to implement my own PermissionEvaluator (following the answer to my other question. However looking at the standard implementation AclPermissionEvaluator here I notice, that the DAO is set via the…
Pete
  • 10,720
  • 25
  • 94
  • 139
4
votes
4 answers

properly understanding inject in angular14 - inject() must be called from an injection context

I'm trying to learn the changes in angular 14, especially the inject() feature where i'm able to inject modules to functions and i don't need to create special services for that.. but i think i got something wrong. I'm trying to create some static…
ufk
  • 30,912
  • 70
  • 235
  • 386
4
votes
1 answer

Use Tailwind in Content Script of Browser Extension

I'm trying to write a browser extension which injects some buttons and custom components into a website, therefore I wanted to use Tailwind (and DaisyUI) to style them. I found this discussion and configured my project according to the solution and…
Nils
  • 134
  • 2
  • 9
4
votes
0 answers

Apache httpd: Inject announcement banner into every page including reverse proxy

I want to inject a banner announcing planned maintenance a few days in advance into every web page served. The apache has a few local pages, but mostly acts as reverse proxy of several web applications. This question has two parts: How do I…
sneumann
  • 91
  • 1
4
votes
2 answers

How do i send keys using a global keyboard hook?

I'm trying to send keys to an application which does not respond to any of the API's that I have so far used (SendInput(), PostMessage(), SendMessage() and others). However, i tested Windows' On-Screen Keyboard Utility and pressed the keys i needed…
fireup
  • 61
  • 1
  • 3
4
votes
5 answers

Can you inject code/an exe into a process with python?

I've seen a few sites talking about injecting DLL's (such as http://www.codeproject.com/KB/DLL/DLL_Injection_tutorial.aspx), but I'm struggling with how to get an EXE to work. any help/tips would be appreciated. The best way I know how to explain it…
user785255
  • 61
  • 1
  • 1
  • 5
4
votes
1 answer

JAX-RS failed to inject @EJB or @Inject

I'm trying to test a minimal JAX-RS + EJB/CDI injection mechanism and currently hitting a road-block due to GlassFish unable to inject for whatever reason. (Using @Inject will throw NPE because GlassFish cannot inject the POJO). My beans.xml…
xandross
  • 911
  • 2
  • 11
  • 17
4
votes
1 answer

Computing nested sum using Ruby inject

I'm trying to use Ruby's inject to sum an array representing a finite continued fraction, where [a, b, c, d, e, ... , x] = a + 1/(b + 1/(c + 1/(d + 1/(e + ... 1/x)...))) I can't figure out how to get the proper nested evaluation to return the…
regulus
  • 939
  • 3
  • 13
  • 21
4
votes
1 answer

Difference between bindAsContract and bind in HK2

I am working to implement a dependency injection of a constructor in a J2EE jersey project. I am using HK2. I create a class class MyServiceImpl implements MyService{ @Inject public MyServiceImpl(String test){ // do something } } Now, my…
4
votes
1 answer

Angular: Injecting router in constructor giving error

I am trying to create a global error handling component and for this, I am providing custom ErrorHandler class. I want to inject Router through the constructor in error.service.ts so that I can navigate to the error component but it fails with below…
4
votes
0 answers

Spring property injection with interface default method

Is there a way to return an injected property in an interface default method? I want any class that implements MyInterface to get a method getBar() that returns a property value, without having to reimplement it in every class. I'm essentially…
Egor
  • 1,622
  • 12
  • 26
4
votes
1 answer

Spring Boot 2.0.0 cycle bean dependency EntityManger and SessionFactory

I have problem with new Spring Boot version 2.0.0. I need to create SessionFactory bean, for that I need Spring to inject EntityManager. package cz.moravec; import cz.moravec.provisioning.Provisioner; import org.hibernate.Session; import…
Jan Moravec
  • 496
  • 6
  • 9
4
votes
1 answer

Optional Scoped injection in Guice

I need to inject a field only if it is available in the current scope, and null otherwise. For example: public class Thinger implements Provider { public @Inject(optional=true) HttpServletRequest request; public SomeSuch…
Steve Skrla
  • 1,620
  • 5
  • 16
  • 24
4
votes
0 answers

Inject custom class dialog with dagger2

I have a problem with dagger, I am trying to understand how it work. I have a custom class that extend of dialog class, So I would like to inject this class in all activitys or fragment for just to show o hide my dialog. This is my custom…
user1153174
  • 133
  • 1
  • 10