title: Bean Creation Exception in Spring-MVC
body: <p>Here are my code and the exception I am getting, Earlier this project is working fine but when I added MySQL connection and Bean class in this, I am getting the exception.</p>
  
  <pre><code>Jul 03, 2017 12:54:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
  SEVERE: Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.service.IStockDetailsService metalist.yashdeep.scrap.controller.DefaultController.iStockDetailsService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iStockDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.dao.IStockDetailsDAO metalist.yashdeep.scrap.service.StockDetailsService.iStockDetailsDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stockDetailsDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: metalist.yashdeep.scrap.resource.DatabaseConnection metalist.yashdeep.scrap.dao.StockDetailsDAO.databaseConnection; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [metalist.yashdeep.scrap.resource.DatabaseConnection] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
      at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:482)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
      at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
      at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
      at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      at javax.servlet.GenericServlet.init(GenericServlet.java:158)
      at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1269)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1182)
      at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1072)
      at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5368)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5660)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;MetalistEnterprises&lt;/groupId&gt;
      &lt;artifactId&gt;MetalistEnterprises&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;build&gt;
          &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.5.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.0&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;warSourceDirectory&gt;WebContent&lt;/warSourceDirectory&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
      &lt;properties&gt;
          &lt;spring.version&gt;4.0.3.RELEASE&lt;/spring.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
              &lt;type&gt;jar&lt;/type&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-framework-bom&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
    &lt;display-name&gt;MetalistEnterprises&lt;/display-name&gt;
      &lt;servlet&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;
           org.springframework.web.servlet.DispatcherServlet
        &lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>@Controller
  public class DefaultController {
  
      @Autowired
      IStockDetailsService iStockDetailsService;
  
      @RequestMapping(value = "/stock")
      public ModelAndView stockMethod(Model model) {
      ArrayList&lt;StockBean&gt; stocksList = iStockDetailsService.getAllStocks();
      model.addAttribute("stocksList", stocksList);
      return new ModelAndView("stock");
  }
  </code></pre>
  
  <p>Service</p>
  
  <pre><code>@Service("iStockDetailsService")
  public class StockDetailsService implements IStockDetailsService {
  
      @Autowired
      IStockDetailsDAO iStockDetailsDAO;
  
      @Override
      public ArrayList&lt;StockBean&gt; getAllStocks() {
          System.out.println("In Service, getAllStocks methods");
          ArrayList&lt;StockBean&gt; stocksList = iStockDetailsDAO.getAllStocks();
          return stocksList;
      }
  </code></pre>
  
  <p>Bean</p>
  
  <pre><code>public class StockBean {
  
      private Integer idStock;
      private String invoiceNumber;
      private Date invoiceDate;
      private String companyName;
      private String materialDescription;
      private float rate;
      private float amount;
      private float tax;
  
  Getter and Settters
  }
  </code></pre>
  
  <p>mvc-dispatcher-servlet.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          http://www.springframework.org/schema/jee 
           http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"&gt;
  
      &lt;context:component-scan base-package="metalist.yashdeep.scrap" /&gt;
      &lt;context:annotation-config /&gt;
      &lt;mvc:resources mapping="/resource/**" location="/resource/" /&gt;
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/view/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>IStockDetailsDAO    </p>
  
  <pre><code>import java.util.ArrayList;
  import metalist.yashdeep.scrap.model.StockBean;
  public interface IStockDetailsDAO {
      public ArrayList&lt;StockBean&gt; getAllStocks();
  }
  </code></pre>
  
  <p>StockDetailsDAO</p>
  
  <pre><code>@Repository
  public class StockDetailsDAO implements IStockDetailsDAO {
  
      @Autowired
      DatabaseConnection databaseConnection;
  
      @Autowired
      DatabaseQuery databaseQuery;
  
      @Override
      public ArrayList&lt;StockBean&gt; getAllStocks() {
          System.out.println("In DAO, getAllStocks methods");
          Connection conn = databaseConnection.getConnection();
          try {
              ArrayList&lt;StockBean&gt; arrayListStockBean = new ArrayList&lt;StockBean&gt;();
              String getAllQueries = databaseQuery.queryGetAllStocks;
              Statement stmt = conn.createStatement();
              ResultSet rs = stmt.executeQuery(getAllQueries);
              while (rs.next()) {
                  StockBean stockBean = new StockBean();
                  stockBean.setIdStock(rs.getInt("idStock"));
                  stockBean.setInvoiceNumber(rs.getString("invoiceNumber"));
                  stockBean.setInvoiceDate(rs.getDate("invoiceDate"));
                  stockBean.setCompanyName(rs.getString("CompanyName"));              
            stockBean.setMaterialDescription(rs.getString("materialDescription"));
                  stockBean.setRate(rs.getFloat("rate"));
                  stockBean.setAmount(rs.getFloat("amount"));
                  stockBean.setTax(rs.getFloat("tax"));
                  arrayListStockBean.add(stockBean);
              }
              return arrayListStockBean;
          } catch (Exception ex) {
              return null;
          } finally {
              try {
                  conn.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>DatabaseConnection</p>
  
  <pre><code>    package metalist.yashdeep.scrap.resource;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  
  public class DatabaseConnection {
  
      public Connection getConnection() {
          System.out.println("In getConnection");
          try {
              System.out.println("Trying for Connection For NEOs");
              Class.forName("com.mysql.jdbc.Driver");
              Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/metalistenterpriseschema",
                      "root", "root");
              System.out.println("Connection Successful For NEOs");
              return conn;
          } catch (Exception e) {
              System.out.println("Exception in getConnection. Exception is : " + e);
              return null;
          }
  
      }
  
  }
  </code></pre>