title: @Autowired and @Service working from controller but not from a different package
body: <p>I need help understanding the concept behind <code>@Autowired</code> and <code>@Service</code>. I have a DAO defined with <code>@Service</code> and controller with <code>@Autowired</code> and everything seems fine, however, I use the same <code>@Autowired</code> in a different class then it does not work. </p>
  
  <p>Example:</p>
  
  <p>Service</p>
  
  <pre><code>@Service
  public class MyService {
      private JdbcTemplate jdbcTemplate;
  
      @Autowired
      public void setDataSource (DataSource myDataSource) {
          this.jdbcTemplate = new JdbcTemplate(myDataSource);
      } 
  
      public void testUpdate(){
              jdbcTemplate.update("some query");
      }
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>package com.springtest.mywork.controller;
  
  @Controller
  @RequestMapping(value = "/test.html")
  public class MyController
  {
    @Autowired
    MyService myService;
  
    @RequestMapping(method = RequestMethod.GET)
    public String test(Model model)
    {
      systemsService.testUpdate();
      return "view/test";
    }
  }
  </code></pre>
  
  <p>The above all works fine. However, If I want to use <code>MyService</code> in a POJO then it just doesn't work. Example:</p>
  
  <pre><code>package com.springtest.mywork.pojos;
  public class MyPojo {
  
      @Autowired
      MyService myService;
  
      public void testFromPojo () {
          myService.someDataAccessMethod(); //myService is still null
      }
  }
  </code></pre>
  
  <p>Spring Config:</p>
  
  <pre><code>&lt;beans&gt;
      &lt;mvc:annotation-driven /&gt;
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/views/"/&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
      &lt;context:component-scan base-package="com.springtest.mywork" /&gt;
      &lt;bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://127.0.0.1:3306/mydb" /&gt;
          &lt;property name="username" value="hello" /&gt;
          &lt;property name="password" value="what" /&gt;
      &lt;/bean&gt;
  
      &lt;bean name="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"&gt;
          &lt;constructor-arg ref="dataSource"/&gt;
      &lt;/bean&gt;
   &lt;/beans&gt;
  </code></pre>