9

I have a rather naive question. Can we inject dependencies using core java just like how we inject using Spring framework?

For now, I do something like this:

In web.xml:

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

spring applicationcontext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="mybean" class="com.test.app.MyService" />

</beans>

Class where I will use the injected bean:

public class MyResource {

    @Autowired
    private MyService mybean;

     public MyResponse doService(MyRequest req) {
           mybean.doBusiness(req);
     }
}

}

So, is there a way we can do this dependency injection using core java? I have been reading a bit on CDI but didn't understand it well. Plus, it also felt like it was not a direct substitution of what Spring does.

Please help and correct me if I am wrong.

3
  • What do you mean by "core Java"? That's not a generally-recognized term. Commented Mar 19, 2014 at 15:31
  • i changed the title. is it more appropriate? Commented Mar 19, 2014 at 16:04
  • That's easier to understand. Commented Mar 19, 2014 at 16:20

6 Answers 6

3

javax.inject (JSR-330) and CDI (JSR-299, JSR-346) are just APIs, not implementations. There are implementations of these APIs that do work in Java SE (as opposed to Java EE, which I suppose is what you mean by "core Java"), e.g. Weld SE.

CDI builds upon JSR-330.

While javax.inject is only about basic injection and does not address scopes and lifecycles, it is fair to say that CDI and Spring Core are more or less equally powerful. Both can be used in Java SE.

Sign up to request clarification or add additional context in comments.

Comments

2

@Autowired is a Spring annotation that is not standardized. The corresponding standard annotation is @Inject from javax.inject

Aside from Spring, Google Guice supports also javax.inject

Another extremely lightweight framework for dependency injection (that does not support javax.inject) is PicoContainer

Comments

2

Dependency injection is a pattern. What you want, and what Spring is at the base, is an Inversion of Control container.

No, Java SE and Java EE do not provide such an Inversion of Control container.

Java EE does provide an API in the form of CDI for dependency injection. There is a reference implementation called Weld that you can use.

The other answers list a number of other IoC containers.

Comments

2

You can also look at HK2, which is a full JSR-330 and runs in base JDK. It has features for the base JDK such as security. See more here:

HK2 Main Page

Comments

0

Maybe you want to have a look at javax.inject. The API is available as a separate jar and a few dependency injections framework exist which depend on, and use, this jar.

Among them are Guava and the more lightweight Dagger.

Your @Autowired here would be @Inject in javax.inject language; the two frameworks above allow injection in pure Java (maybe others, don't know because not interested).

Hope this helps...

3 Comments

Spring also supports @Inject.
I've heard that; however, since when? Spring predates javax.inject.
Since at least 3.1. Spring's added support for most of the Java EE annotations; for example, it supports all the lifecycle annotations as well. It treats @Inject just the same as @Autowired, except that @Inject doesn't have a required option.
0

Yes, you can! JSR 330 completely talks about this.

You can also take a look at the Spring documentation.

1 Comment

actually this is specification aka standard, not real implementation. So this is not proof that you can use DI in vanilla Java :) Spring, Dagger, Guice etc is implementations that one can use for DI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.