title: spring cannot find my bean
body: <p>I am new to spring, in my application I need to connect to Mysql, all my database's configuration are in a bean, when I try to get it,the server gives me this error:</p>
  
  <pre><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
  </code></pre>
  
  <p>this is my servlet-context where I define my bean:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          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;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;annotation-driven /&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;
      &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;beans:property name="suffix" value=".jsp" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;context:component-scan base-package="com.metmi.mmasgis" /&gt;
  
  
  
  
  
  
  
      &lt;beans:bean id="dataSource" name="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          autowire-candidate="true"&gt;
          &lt;beans:property name="driverClassName"
              value="com.mysql.jdbc.Driver"&gt;
          &lt;/beans:property&gt;
          &lt;beans:property name="username" value="root"&gt;&lt;/beans:property&gt;
          &lt;beans:property name="password" value="password"&gt;&lt;/beans:property&gt;
          &lt;beans:property name="url"
              value="jdbc:mysql://localhost:3306/springschema"&gt;
          &lt;/beans:property&gt;
      &lt;/beans:bean&gt;
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>this is my controller:</p>
  
  <pre><code>    package com.metmi.mmasgis;
  
  import java.text.DateFormat;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.Locale;
  
  import javax.inject.Inject;
  import javax.sql.DataSource;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.context.ApplicationContext;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.context.ContextLoader;
  
  import com.metmi.mmasgis.dao.DbImpl;
  import com.metmi.mmasgis.model.Db;
  
  /**
   * Handles requests for the application home page.
   */
  @Controller
  public class HomeController {
      @Inject
      DbImpl dbs;
      private static final Logger logger = LoggerFactory
              .getLogger(HomeController.class);
  
      /**
       * Simply selects the home view to render by returning its name.
       */
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String home(Locale locale, Model model) {
          logger.info("Welcome home! The client locale is {}.", locale);
  
          Date date = new Date();
          DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
                  DateFormat.LONG, locale);
  
          String formattedDate = dateFormat.format(date);
  
          model.addAttribute("serverTime", formattedDate);
  
          return "home";
      }
  
      /**
       * get the database list in Mysql
       */
      @RequestMapping(value = "/db", method = RequestMethod.GET)
      public String dbs(Locale locale, Model model) {
          ApplicationContext ctx = ContextLoader
                  .getCurrentWebApplicationContext();
          DataSource ds = (DataSource) ctx.getBean("dataSource");
          dbs = new DbImpl();
          dbs.setDataSource(ds);
          ArrayList&lt;Db&gt; dbList = dbs.getDatabases();
          model.addAttribute("dbList", dbList);
          return "dbs";
      }
  
      /**
       * Simply shows ciao.
       */
      @RequestMapping(value = "/ciao", method = RequestMethod.GET)
      public String ciao(Locale locale, Model model) {
          logger.info("Welcome home! The client locale is {}.", locale);
  
          Date date = new Date();
          DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
                  DateFormat.LONG, locale);
  
          String formattedDate = dateFormat.format(date);
  
          model.addAttribute("serverTime", formattedDate);
  
          return "ciao";
      }
  
  }
  </code></pre>