title: Spring @Autowired not working and returning null
body: <p>I have problems on <code>@Autowired</code> in Spring:</p>
  
  <p>MailSender.java</p>
  
  <pre><code>package com.example.package.util;
  
  @Component
  public class MailSender {
      @Autowired
      public MailSender(VelocityEngine velocityEngine, JavaMailSender mailSender) {
          this.velocityEngine = velocityEngine;
          this.mailSender = mailSender;
      }
  
      public void send() {
          // Dosomething
      }
  
      private final VelocityEngine velocityEngine;
  
      private final JavaMailSender mailSender;
  }
  </code></pre>
  
  <p>MailService.java</p>
  
  <pre><code>package com.example.package.service;
  
  @Service
  public class MailService {
      public void sendEmail(String email) {
          // mailSender is always null
          String mailBody = mailSender.send();
      }
  
      // Autowired here is not working
      @Autowired
      @Qualifier("myMailSender")
      private MailSender mailSender;
  
      // Autowired here is working
      @Autowired
      private UserDao userDao;
  }
  </code></pre>
  
  <p>Configuration</p>
  
  <pre><code>&lt;context:annotation-config /&gt;
  &lt;context:component-scan base-package="com.example.package" /&gt;
  
  &lt;!-- Mail Service --&gt;
  &lt;bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"&gt;
      &lt;property name="resourceLoaderPath" value="classpath:/mails" /&gt;
      &lt;property name="preferFileSystemAccess" value="false" /&gt;
  &lt;/bean&gt;
  &lt;bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt;
      &lt;property name="host" value="smtp.mailgun.org" /&gt;
      &lt;property name="username" value="postmaster@testzilla.org" /&gt;
      &lt;property name="password" value="" /&gt;
      &lt;property name="javaMailProperties"&gt;
          &lt;props&gt;
              &lt;prop key="mail.smtp.auth"&gt;true&lt;/prop&gt;
              &lt;prop key="mail.smtp.starttls.enable"&gt;true&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="myMailSender" 
          class="com.example.package.service.MailService"&gt;
      &lt;property name="mailSender" ref="mailSender" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>As I mentioned above, the mailSender object is always <code>null</code> in <code>MailService</code> class.</p>
  
  <p>Maybe there's a conflict in mailSender object in <code>MailSender</code> class and <code>MailService</code> class. The name of variables are the same, but they are different.</p>
  
  <p>So I tried to rename the variables, but still not working.</p>
  
  <p>Unit Test Class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration({"classpath:test-spring-context.xml"})
  public class MailSenderTest {
      @Autowired
      @Qualifier("myMailSender")
      private MailSender mailSender;
  
      @Test
      public void test() {
          String templatePath = "/verifyEmail.vm";
          Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();
  
          System.out.println(mailSender);
  
          String mailBody = mailSender.getMailContent(templatePath, model);
          System.out.println(mailBody);
      }
  }
  </code></pre>
  
  <p>When I ran the test case, I got following exception:</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.package.util.MailSender com.example.package.service.UserService.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.package.util.MailSender] 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), @org.springframework.beans.factory.annotation.Qualifier(value=testzillaMailSender)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
      ... 54 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.package.util.MailSender] 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), @org.springframework.beans.factory.annotation.Qualifier(value=testzillaMailSender)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
      ... 56 more
  </code></pre>
  
  <p>Can you help me with it? Thx.</p>