Reputation: 327
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.
Upvotes: 9
Views: 21436
Reputation: 2394
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:
Upvotes: 2
Reputation: 205
Yes, you can! JSR 330 completely talks about this.
You can also take a look at the Spring documentation.
Upvotes: 0
Reputation: 12855
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.
Upvotes: 3
Reputation: 279880
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.
Upvotes: 2
Reputation: 63991
@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
Upvotes: 2
Reputation: 121692
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...
Upvotes: 0