title: How to autowire one bean to another bean in spring mvc
body: <p>i have two classes i.e two beans annotated with @Component(SampleBean.java)  and @Controller(TestController.java).</p>
  
  <p>I want to autowire  SampleBean.java in TestController.java </p>
  
  <p>here my code is :</p>
  
  <p>MyServletInterface.java </p>
  
  <pre><code>            public interface MyServletInterface {
                    public void m2(); 
  
              }
  </code></pre>
  
  <p>SampleBean.java</p>
  
  <pre><code>            import org.hibernate.SessionFactory;
              import org.springframework.beans.factory.annotation.Autowired;
              import org.springframework.stereotype.Component;
  
              @Component
              public class SampleBean implements MyServletInterface {
  
                  @Autowired
                  SessionFactory sessionFactory;
  
                  public void m2() {
                      System.out.println("SessionFactory &gt;:&lt; " + sessionFactory);
  
                  }
  
              }
  </code></pre>
  
  <p>TestController.java</p>
  
  <pre><code>                package sample.test;
  
                  import org.hibernate.SessionFactory;
                  import org.springframework.beans.factory.annotation.Autowired;
                  import org.springframework.stereotype.Controller;
                  import org.springframework.web.bind.annotation.RequestMapping;
                  import org.springframework.web.servlet.ModelAndView;                    
  
  
                  @Controller
                  public class TestController {
  
                      @Autowired
                      SessionFactory sessionFactory;
  
                      @Autowired
                      MyServletInterface ms;
  
                      @RequestMapping("/form")
                      public ModelAndView method() {
                          System.out.println("inside method");
                          ms.m2();
  
                          System.out.println("SessionFactory : " + sessionFactory);
                          return new ModelAndView("check_size");
                      }
  
                      public MyServletInterface getMs() {
                          return ms;
                      }
  
                      public void setMs(MyServletInterface ms) {
                          this.ms = ms;
                      }
  
                  }
  </code></pre>
  
  <p>i want autowire the SampleBean.java into TestController.java</p>
  
  <p>by above approach i got exception :</p>
  
  <pre><code>Jul 24, 2017 4:18:52 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
              INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
              Jul 24, 2017 4:18:53 PM org.springframework.web.context.ContextLoader initWebApplicationContext
              SEVERE: Context initialization failed
              org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: in.co.velox.webservice.util.MyServletInterface sample.test.TestController.ms; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [MyServletInterface] 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.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
                  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
                  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
                  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
                  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
                  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
                  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
                  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
                  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)
              Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: in.co.velox.webservice.util.MyServletInterface sample.test.TestController.ms; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [in.co.velox.webservice.util.MyServletInterface] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
                  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
                  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
                  ... 22 more
              Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [MyServletInterface] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
                  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
                  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
                  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
                  ... 24 more
  
              Jul 24, 2017 4:18:53 PM org.apache.catalina.core.StandardContext listenerStart
              SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
              org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: in.co.velox.webservice.util.MyServletInterface sample.test.TestController.ms; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [MyServletInterface] 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.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
                  at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
                  at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
                  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
                  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
                  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
                  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
                  at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
                  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)
              Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: in.co.velox.webservice.util.MyServletInterface sample.test.TestController.ms; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [MyServletInterface] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
                  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
                  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
                  ... 22 more
              Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [MyServletInterface] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
                  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
                  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
                  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
                  ... 24 more
  </code></pre>
  
  <p>spring-config.xml</p>
  
  <pre><code>                            &lt;?xml version="1.0" encoding="UTF-8"?&gt;
              &lt;beans xmlns="http://www.springframework.org/schema/beans"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
                  xmlns:mvc="http://www.springframework.org/schema/mvc" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                                     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  
  
                  &lt;context:annotation-config /&gt;
                  &lt;mvc:annotation-driven /&gt;
                  &lt;context:component-scan base-package="sample.test" /&gt;
                  &lt;context:property-placeholder location="classpath:application.properties" /&gt;
  
  
  
                  &lt;bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
                      destroy-method="close"&gt;
                      &lt;property name="driverClassName" value="${database.driverClassName}" /&gt;
                      &lt;property name="url" value="${database.url}" /&gt;
                      &lt;property name="username" value="${database.username}" /&gt;
                      &lt;property name="password" value="${database.password}" /&gt;
  
                  &lt;/bean&gt;
  
  
                  &lt;bean id="sessionFactory"
                      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
                      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
                      &lt;property name="hibernateProperties"&gt;
                          &lt;props&gt;
                              &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                              &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
                              &lt;prop key="hibernate.format_sql"&gt;${hibernate.format_sql}&lt;/prop&gt;
                              &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
                          &lt;/props&gt;
                      &lt;/property&gt;
  
                  &lt;/bean&gt;
                  &lt;bean
                      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
                      &lt;property name="prefix" value="/WEB-INF/jsppages/" /&gt;
                      &lt;property name="suffix" value=".jsp" /&gt;
                  &lt;/bean&gt;     
  
  
              &lt;/beans&gt;
  </code></pre>