class required a single bean, but 2 were found:

Jeff :

I have the below interface:

public interface MailSender {
    void sender(String to, String subject,String body);
}

With 2 imlementation of it:

public class SmtpkMailSender implements MailSender   {

    static Log log=LogFactory.getLog(MailSender.class); 

    public void sender(String to, String subject,String body){
        log.info("SMTP To: "+to);
        log.info("SMTP Subjecy: "+subject);
        log.info("SMTP body: "+body);
    }

}

and the second one is:

@Primary
public class MockMailSender implements MailSender   {

    static Log log=LogFactory.getLog(MailSender.class); 

    public void sender(String to, String subject,String body){
        log.info("To: "+to);
        log.info("Subject: "+subject);
        log.info("body: "+body);
    }

}

I used the dependency injection into the controller class which is as following:

@RestController
public class MailController {

    @Autowired
    private MailSender smtpkMailSender;

    @RequestMapping("/send")
    public String send(){
        smtpkMailSender.sender("Person", "Important", "Take Care");
        return "mail is sent";
    }
}

At the end i have a configuration class which contains my Beans:

@Configuration
public class MailConfig {

    @Bean
    public SmtpkMailSender getSmtpkMailSender(){
        return new SmtpkMailSender();
    }

    @Bean
    public MockMailSender getMockMailSender(){
        return new MockMailSender();
    }

}

Unfortunatly, when i run my application it complains with:

Description:

Field smtpkMailSender in com.example.demo.MailController required a single bean, but 2 were found:
    - getSmtpkMailSender: defined by method 'getSmtpkMailSender' in class path resource [com/example/mail/MailConfig.class]
    - getMockMailSender: defined by method 'getMockMailSender' in class path resource [com/example/mail/MailConfig.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

As you see, although i have specified the MockMailSender as Primary the spring still complains, and cannot identify it

Leffchik :

Since you're using java config you should mark config method with @Primary annotation, and not a class:

@Configuration
public class MailConfig {
    @Bean
    public SmtpkMailSender getSmtpkMailSender(){
        return new SmtpkMailSender();
    }

    @Bean
    @Primary
    public MockMailSender getMockMailSender(){
        return new MockMailSender();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Field restTemplate required a single bean, but 2 were found

Generic Service implementation required a single bean, but 2 were found

Parameter 0 of method standardParameterResolver ... required a single bean, but 2 were found

Required a single bean, 2 found

required a single bean, but 2 were found in Resource Server with OAuth2

Required Single bean but 2 were found even when referenced in Spring config

Spring boot app failed to start with error "Application required a single bean, but 2 were found"

Spring boot Keep getting "required a single bean, but 10 were found" although I declared only one service

Spring Boot - Parameter of constructor required a single bean, but multiple were found for Generated ApiClient Constructor

SpringFramework: expected single matching bean but found 2

Class required a bean of type 'java.lang.String' that could not be found

Spring Boot - Required bean not found - but interface is defined in same class

No qualifying bean, expected single matching bean but found 2

CommandLineRunner required a bean that could not be found

Spring with MyBatis: expected single matching bean but found 2

Spring FactoryBean and autowiring not working : expected single matching bean but found 2

Parameter 0 of constructor in Class B required a bean of type Class A that could not be found

Parameter 0 of constructor in my class required a bean of my second class that could not be found

PHP - Class not found, but is required

Merging 2 request classes into a single class with required fields in Java?

No qualifying bean of type 'javax.persistence.EntityManager' available: expected single matching bean but found 2

NoUniqueBeanDefinitionException: no qualifying bean of type ... i defined, expected single matching bean but found 2

getting error No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2

No qualifying bean of type 'org.springframework.batch.core.Job' available: expected single matching bean but found 2:

Field required a bean of type ... that could not be found

Error required a bean of type 'XXX' that could not be found

Field required a bean which could not be found in springboot

Field personRepositary in () required a bean of type () that could not be found

Spring AWS - required bean could not be found

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive