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.
Dependency Injection is a pattern, not any particular framework. You could even implement it manually, but that would be too much trouble.
Dependency Injection is a concept first and then a framework. When the application under question is small, we can always meet the needs by injecting dependencies manually, without using any framework like Spring.
What About Optional Dependencies? With setter injection, Spring allows us to specify optional dependencies by adding @Autowired(required = false) to a setter method. This is not possible with constructor injection since the required=false would be applied to all constructor arguments.
An alternative to dependency injection is using a service locator. The service locator design pattern also improves decoupling of classes from concrete dependencies. You create a class known as the service locator that creates and stores dependencies and then provides those dependencies on demand.
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.
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With