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

Where are scripts in injectScripts injected in TestCafé tests?

I am setting up TestCafé tests programmatically and I use the injectScripts config on the Runner class to inject functions. According to the documentation, these scripts are added to the header of the tested page. Is it possible to invoke the…
Sven
  • 892
  • 14
  • 29
3
votes
1 answer

how can i use a chrome extension content script to inject html into a page at startup

i want to create an extension to inject html into every page as soon as it loads up. I am familiar with teh manifest.json rules for this as well as how to run a content script. The problem I'm currently having is the content script injects the html…
lubac
  • 31
  • 1
  • 3
3
votes
2 answers

Can't @Inject a @ManagedBean in another @ManagedBean

Ok here is my session bean. I can always retrieve the currentUser from any Servlet or Filter. That's not the problem The problem is the fileList, and currentFile. I've tested with simple int's and Strings and its' the same effect. If I set a…
Drew H
  • 4,699
  • 9
  • 57
  • 90
3
votes
2 answers

ClassCastException: com.sun.proxy.$ProxyX cannot be cast to a class

I have a Spring bean (ChildBean extends Parent) which is extending an abstract class (Parent implements Runnable). public abstract class Parent implements Runnable { public final void run() { // some code } public int…
sys463
  • 337
  • 2
  • 5
  • 18
3
votes
1 answer

CDI in JUnit tests with Jersey Test Framework

We are using the Jersey Test Frameworks for API testing. In test-mode, we use an h2 database, mysql in production. Everything is fine to this point. Now i want to write tests for our repositories to check if the data is written properly to the…
3
votes
0 answers

Vuejs inject to functional component from parent provider component

I'am trying to inject to an functional component in vue from an parent "ThemeProvider" component, but the functional component dont't get the provided theme object. It only works if the provider is inside the View.vue file... Is there a way to do…
Walraz
  • 155
  • 1
  • 1
  • 7
3
votes
0 answers

Do child injectors in Guice ever get created automatically?

I've found references to child injectors inside the Guice documentation, but other than them being used explicitly I can't find any reference to how they're created implicitly. Are they created automatically for dependents of dependents? Are they…
GMac
  • 31
  • 1
3
votes
2 answers

TypeScript compiler can't find injected property in Vue Object Component

I'm writing a couple of examples for work, and one that's hanging me up is injecting a service that's provided during Vue's bootstrapping. This "works" (I can access it, it compiles, and runs), and there are no problems or complaints with my…
user358089
3
votes
1 answer

Create Bookmarklet to open a page and add some scripts into it

I need to make a link to open a page and add some scripts into it. That's why i thought creating bookmarklet is the best way. For example, i want to redirect user to a page and alert user when he/she tries to close the tab. The code i want to…
Can
  • 659
  • 1
  • 7
  • 24
3
votes
3 answers

Inject variables into JSON config file

We have an AWS Lambda function in place that dynamically builds test environments for our engineers to test their code through GitHub pull requests. This Lambda function is called using GitHub webhooks, whereby Github POSTs across all the…
JSmith
  • 143
  • 1
  • 8
3
votes
1 answer

Guice DI binding without adding Guice annotations

I have a usecase where I am using an external jar that is based on Spring, while my code is on Google guice. I am trying to inject dependencies in this class of my dependency jar, by writing modules. External class: public class PTRS { @Inject …
3
votes
1 answer

Google Guice configuration error (No implementation was bound)

I have a problem with the Google Guice framework. I'm trying to create a simple application that injects a list of objects. Unfortunately, when trying to run an application, I get the following error. No implementation for java.util.List was…
Rafal
  • 31
  • 1
  • 2
3
votes
1 answer

Inject in interactor android mvp

I need to inject in my interactor my prefrerences interfaces from a dagger module, So my question if I need to instance component in my interactor for get my preferences? ,each process need to do for any class that I need to inject? this is my…
user1153174
  • 133
  • 1
  • 10
3
votes
1 answer

@Inject delivers null in Weld 3 using Java SE

I am using CDI 2.0 with Weld 3.0.0 final (complete weld-se-shaded.jar in the classpath) in a plain Java SE 8 program, as shown below. What is wrong with it or am I missing something, since @Inject does nothing, i.e., the references stay null?…
chris21k
  • 241
  • 1
  • 7
3
votes
1 answer

Spring managed bean injection in class implementing RowMapper

I have a class BusinessRowMapper that implements RowMapper to convert PostGres JSONB object to Java object. BusinessRowMapper implements RowMapper it overrides mapRow public class BusinessRowMapper implements…
user842122
  • 295
  • 2
  • 4
  • 18