简体   繁体   中英

Spring Boot Send Mail can't Autowired Bean

I have some problem when autowired my config bean.

@Configuration
@ImportResource("classpath:/spring-config.xml")
public class MailConfig {


private JavaMailSenderImpl impl;
@Autowired
public MailConfig(JavaMailSenderImpl impl) {
    this.impl = impl;
}
 ...

Spring say that:

Could not autowire. There is more than one bean of 'JavaMailSenderImpl' typr.

Beans: 1)mailSender (MailSenderJndiConfiguration.class)
2)mailSenderBean (spring-config.xml)

I don't use jndi bean. And have no idea about it in this app.
How to ignore Jndi bean? Or maybe simpler config Jndi bean instead mine? Thanks.

Please autowire Interface instead of Implementation

 <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

but in your class replace JavaMailSenderImpl with JavaMailSender

---------------------------------RESOLVED------------------------------
I add property in xml

  <qualifier value="main" />       

And change code

    private JavaMailSenderImpl impl;
    @Autowired
    public MailConfig(@Qualifier("main")JavaMailSenderImpl impl) {
         this.impl = impl;
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM