title: Dependency injection without using spring framework
body: <p>I have a rather naive question. Can we inject dependencies using core java just like how we inject using Spring framework? </p>
  
  <p>For now, I do something like this: </p>
  
  <p>In web.xml:</p>
  
  <pre><code>&lt;context-param&gt;
       &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
       &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;listener&gt;
       &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p>spring applicationcontext.xml: </p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;bean id="mybean" class="com.test.app.MyService" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Class where I will use the injected bean: </p>
  
  <pre><code>public class MyResource {
  
      @Autowired
      private MyService mybean;
  
       public MyResponse doService(MyRequest req) {
             mybean.doBusiness(req);
       }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>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. </p>
  
  <p>Please help and correct me if I am wrong. </p>