0

I have a project with spring-config.xml I have something like this:

 <!-- Job & Stage status notification via email-->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${smtp.host}"/>
        <property name="port" value="${smtp.port}"/>
        <property name="username" value="${smtp.username}"/>
        <property name="password" value="${smtp.password}"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.transport.protocol">smtp</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>

In an existing class I have something like this:

@Autowired
private StatusEmailSender emailSender;

This seems to work. When I put the same code snippet in my own classes, emailSender is null. I can't find any dependencies that I am missing. Any idea how to do this? My classes are in a separate module from the one in which this works. Any idea how I can tell Spring I want to use that bean?

kryger
  • 12,906
  • 8
  • 44
  • 65
Serban Stoenescu
  • 3,136
  • 3
  • 22
  • 41
  • What is the relationship between `StatusEmailSender` and `JavaMailSenderImpl`? It seems that something is missing you your post. I guess that you have your own bean named `StatusEmailSender` that probably calls `JavaMailSenderImpl` initialized using the XML. – AlexR Dec 03 '15 at 10:09
  • Try to get paricular object from appContext. – PatNowak Dec 03 '15 at 10:55
  • 2
    Seems likely that it's a duplicate of http://stackoverflow.com/q/19896870/1240557 – kryger Dec 03 '15 at 10:56
  • id doesn't matches emailsender and mailsender how that can be autowired – Ashish Ani Dec 03 '15 at 10:59
  • Sorry, I pasted the wrong side of the xml. Anyway, emailSender is in the xml as well. – Serban Stoenescu Dec 03 '15 at 11:27

1 Answers1

2

It can happen if you directly creating objects with new YourOwnClass() for your class which have emailSender as a dependency.Spring injects dependencies only if dependent bean is created by spring.