1

Can someone explain me how to use Dependency Injection in JAVA SE using JSR330?

Andrew Tobilko
  • 44,067
  • 12
  • 74
  • 128
Aniket Kulkarni
  • 1,607
  • 2
  • 14
  • 20
  • I have heard that we can use JSR330 in JAVA SE for Dependency injection i googled it but their is no link which explain in detail. – Aniket Kulkarni Mar 18 '16 at 13:10
  • [Spring](http://docs.spring.io/autorepo/docs/spring/3.2.x/spring-framework-reference/html/beans.html) may help you – Andrew Tobilko Mar 18 '16 at 13:12
  • is it possible without using any framework? – Aniket Kulkarni Mar 18 '16 at 13:19
  • 1
    It's not possible. Java SE doesn't implement the spec. It's expected for third parties to implement it. Therefore you need to use one of those third party frameworks. As an aside DI is different from IoC (inversion of control). Using annotations and expecting someone else to resolve the dependencies, is IoC. See [What is Dependency Injection?](http://stackoverflow.com/q/130794/2587435) – Paul Samsotha Mar 18 '16 at 13:45
  • You can try with hk2: http://hk2.java.net. Works gr8 in Java SE. Guice and Spring can also help. I prefer hk2 though lol! – jwells131313 Mar 20 '16 at 15:47
  • Just FYI, I've created a benchmark program to comparing the performance (and usage) of common DI solutions: https://github.com/greenlaw110/di-benchmark – Gelin Luo Aug 01 '16 at 09:59

2 Answers2

1

You can achieve the dependency injection without using any third party framework. This link will help you understand DI and how to implement it without any framework.

I would advise you to use a framework for standardization. You may choose to use Google Guice or Dagger if you are new to DI. If you are already using Spring, use the Spring module.

Sandeep Sukhija
  • 1,072
  • 14
  • 26
0

Maybe you can use the java service provider api. Its nearly to DI concept. See here for info:

Or you will use DI in a pure java manier then see here:

I hope this links are helpfull.

Huluvu424242
  • 706
  • 8
  • 24