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.