🌐
DigitalOcean
digitalocean.com › community › tutorials › java-dependency-injection-design-pattern-example-tutorial
Java Dependency Injection: Design Pattern Tutorial | DigitalOcean
August 3, 2022 - Master Dependency Injection in Java. Learn constructor, setter, interface injection with Spring examples and best practices for decoupled code design.
🌐
Vogella
vogella.com › tutorials › DependencyInjection › article.html
Using dependency injection in Java - Introduction - Tutorial
The general concept behind dependency injection is called Inversion of Control (IoC). A Java class has a dependency on another class if it uses an instance of that class. This is known as a class dependency. For example, a class that accesses a logging service has a dependency on the service class.
Discussions

Is dependency injection really a hard topic ? Or am i just dumb ?
Dependency injection as a principal is quite easy. The consumer doesn’t provide/instantiates the dependency itself, but it’s rather passed in. Applying the pattern correctly to write better code or using DI frameworks is quite hard. Especially if you already have a code base. More on reddit.com
🌐 r/androiddev
80
30
July 7, 2023
How to dependency injection

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full - best also formatted as code block

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

More on reddit.com
🌐 r/learnjava
13
13
May 30, 2023
Dependency injection frameworks
I have an example project implemented with a variety of DI frameworks if you want concrete code samples: Avaje Dagger 2 Dirk Feather Guice Glassfish HK2 Inverno Jaywire JBock Spring Weld I have an opinion post in the same repo with shorter descriptions and samples. Anyways my take on the items listed: Dagger 2: Speed is very nice to have, but unless you're running on a toaster, the speed of DI frameworks handling things like constructor injection is not noticeable. Dagger 2 operates off of generated code and thus will be the fastest since there are no reflective lookups or excessive checks needed at runtime. Personally, I find the need to rebuild a project to reference these classes in my IDE a bit annoying. Dirk: Dirk is probably the most feature rich DI framework for its size, and even offers some CDI capabilities if you need them (But it is not fully CDI complaint). Configuring it is almost entirely automatic which is is a plus for lazy developers like me. Another great thing dirk does is the wonderful documentation on its project page README. Its top notch stuff. Feather: If file size is your concern, feather is your guy. Its like Guice if you stripped out all the extra stuff, just keeping barely enough to be JSR-330 compliance. Guice: If you want a bit more configurability on top of what is just required for JSR-330 compliance check out the github wiki page on Guice. It also has examples on how to integrate with some common frameworks like OSGI and struts. Jaywire: Its 100% manual. Don't. Spring: Really only viable if you're building a spring boot application. I've not looked too much in depth to Spring's DI so I can't give a great summary of it. Even though JSR-330 interop is scheduled for v3 you can get support immediately via includeFilters on the @ComponentScan. (Edited comment here) Replies have pointed out some differences in how it handles scope than I originally listed here. Weld: If you need more than just plain injection, namely scoped injection via CDI Weld is great. You don't need a whole SE environment to run a weld instance as they offer a stand-alone artifact. I don't write web-apps so I don't use built in scopes like RequestScoped but instead utilize CDI's ability to create custom scopes. That and extensions/interceptors to handle some automated work on injectable types. Choosing a DI framework is like choosing any other library. You choose the right tool for the job. They all have their pro's and con's. More on reddit.com
🌐 r/java
67
42
February 2, 2023
need help understanding dependency injection with annotations
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
7
2
March 13, 2024
🌐
Oracle
docs.oracle.com › javaee › 7 › tutorial › injection002.htm
4.2 Dependency Injection - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7)
Use the javax.inject.Inject annotation to inject managed beans; for example: public class MyServlet extends HttpServlet { @Inject CurrencyConverter cc; ... } As opposed to resource injection, dependency injection is typesafe because it resolves by type.
🌐
Mend
mend.io › blog › developer › how to use dependency injection in java: tutorial with examples
How To Use Dependency Injection In Java: Tutorial With Examples
October 31, 2024 - Furthermore, these types depend on specific use cases and project requirements, and knowing this helps the developer make informed decisions when implementing dependency injection in their Java projects. Let’s explore them in more detail: Constructor injection involves passing dependencies as parameters to a class’s constructor. This method ensures that the required dependencies are provided during object creation. Consider the following example:
🌐
GeeksforGeeks
geeksforgeeks.org › system design › dependency-injection-di-design-pattern
Java Dependency Injection (DI) Design Pattern - GeeksforGeeks
July 23, 2025 - This external agent, that is the Spring framework will take over the responsibility of injecting these dependencies into the respective Car, Bike and Scooter classess, as depicted in the above image. These dependencies are the instance variables of your Java classes that get injected into our targeted classes through the external agent that is taking control of this process.
🌐
Spring
docs.spring.io › spring-framework › reference › core › beans › dependencies › factory-collaborators.html
Dependency Injection :: Spring Framework
For example, if a third-party class does not expose any setter methods, then constructor injection may be the only available form of DI. The container performs bean dependency resolution as follows: The ApplicationContext is created and initialized with configuration metadata that describes ...
🌐
Stackify
stackify.com › dependency-injection
Design Patterns Explained – Dependency Injection- Stackify
August 26, 2024 - These frameworks provide the technical parts of the technique so that you can focus on the implementation of your business logic. Popular examples are: Weld, the reference implementation of Jakarta EE’s Context and Dependency Injection for Java (CDI) specification
🌐
Medium
medium.com › groupon-eng › dependency-injection-in-java-9e9438aa55ae
Dependency Injection in Java. In this article, we are going to… | by Carlos Chacin | Groupon Product and Engineering | Medium
December 20, 2019 - When a class ClassA uses any method of another class ClassB, we can say that ClassB is a dependency of ClassA. In this example, ClassA is calculating 10% of the value, and calculating that value, it's reusing the functionality exposed by ClassB.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › the-basics-of-java-dependency-injection-fc5573d31655
Java Dependency Injection Essentials | Medium
February 5, 2024 - In this example, the NotificationManager is entirely decoupled from the concrete implementations of NotificationService. This decoupling is achieved through Dependency Injection, specifically constructor injection in this case. It allows for easy extension (e.g., adding new notification methods) and simplifies testing, as dependencies can be mocked or stubbed. The use of Dependency Injection in Java offers numerous benefits, ranging from improved code maintainability to enhanced testing capabilities.
🌐
Java Design Patterns
java-design-patterns.com › patterns › dependency-injection
Dependency Injection Pattern in Java: Boosting Maintainability with Loose Coupling | Java Design Patterns
Factory Method and Abstract Factory: Used to create instances that the DI mechanism will inject. Service Locator: An alternative to DI for locating services or components, though it does not decouple the lookup process as effectively as DI. Singleton: Often used in conjunction with DI to provide a single instance of a service across the application. Clean Code: A Handbook of Agile Software Craftsmanship · Dependency Injection: Design patterns using Spring and Guice
🌐
Wikipedia
en.wikipedia.org › wiki › Dependency_injection
Dependency injection - Wikipedia
3 days ago - In some frameworks, clients do not need to actively accept dependency injection at all. In Java, for example, reflection can make private attributes public when testing and inject services directly.
🌐
GeeksforGeeks
geeksforgeeks.org › advance java › spring-dependency-injection-with-example
Spring Dependency Injection with Example - GeeksforGeeks
Supports constructor, setter, and field injection · Works with XML configuration, annotations, or Java-based configuration · Dependency Injection is necessary because directly creating dependencies inside classes can lead to several problems: 1. Tight Coupling ·
Published   3 weeks ago
🌐
DZone
dzone.com › coding › frameworks › dependency injection implementation in core java
Dependency Injection Implementation in Core Java
November 19, 2020 - Service Class: The service class (dependency class) that provides service to the client class. Injector Class: The injector class injects the service class object into the client class.
🌐
Javatpoint
javatpoint.com › dependency-injection-in-spring
Dependency Injection in spring - javatpoint
>> << Setter Injection with Non-String Map (having dependent Object) Example Setter Injection with Non-String Map In this example, we are using map as the answer that have Answer and User. Here, we are using key and value pair both as an object.
🌐
Baeldung
baeldung.com › home › jakarta ee › an introduction to cdi (contexts and dependency injection) in java
An Introduction to CDI in Java | Baeldung
January 8, 2024 - Whenever we get an ImageFileProcessor instance, CDI will scan the TimeLoggerFactory class, then call the getTimeLogger() method (as it’s annotated with the @Produces annotation), and finally inject the Time Logger service. If we run the refactored sample application with Weld, it’ll output the following: ... CDI supports the use of custom qualifiers for qualifying dependencies and solving ambiguous injection points.
🌐
Android Developers
developer.android.com › app architecture › dependency injection in android
Dependency injection in Android | App architecture | Android Developers
March 5, 2026 - Dagger is a popular dependency injection library for Java, Kotlin, and Android that is maintained by Google. Dagger facilitates using DI in your app by creating and managing the graph of dependencies for you.
🌐
freeCodeCamp
freecodecamp.org › news › a-quick-intro-to-dependency-injection-what-it-is-and-when-to-use-it-7578c84fa88f
A quick intro to Dependency Injection: what it is, and when to use it
October 18, 2018 - So, transferring the task of creating the object to someone else and directly using the dependency is called dependency injection. ... Let’s say we have a car class which contains various objects such as wheels, engine, etc.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Dependency Injection Java Example - Java Code Geeks
July 6, 2022 - Define constructors or setters in the consumer class with metadata to have those dependencies injected. (This is referred to as “autowiring” in Spring.) ... While XML configuration is still supported in Spring, Annotations Configuration and Java Configuration are typically used to provide the metadata used to wire your application. Constructor-based and setter-based are the most common types of injection. A field-based injection is supported but has fallen out of favor due to some undesired side effects. For example, you cannot test your class outside of the IoC container.