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

itachi101 :

springconfig.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />  
        <property name="username" value="hr" />  
        <property name="password" value="hr" />  
    </bean>

    <bean id="prodDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />  
        <property name="username" value="hr" />  
        <property name="password" value="hr" />  
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="dataSource"></property>  
    </bean>

    <bean id="prodJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
        <property name="dataSource" ref="prodDataSource"></property>  
    </bean>  

</beans>  

Also my main class is like:

@ImportResource("springconfig.xml")
@SpringBootApplication
public class TestingFrameworkrunner {

    public static void main(String[] args) {
        ConfigurableApplicationContext context=SpringApplication.run(TestingFrameworkrunner.class, args);
        EmployeeDao employeeDao=context.getBean("employeeDao",EmployeeDao.class);
        employeeDao.deleteEmployee(1);
        employeeDao.getAllEmployees().forEach(e->e.display());
        context.close();
    }

}

This is the error I get-

Field jdbcTemplate in com.NettingTestingFramework.EmployeeDao required a single bean, but 2 were found:
    - dataSource: defined in class path resource [springconfig.xml]
    - prodDataSource: defined in class path resource [springconfig.xml]


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

In my dao file I have:

@Component
public class EmployeeDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    JdbcTemplate prodJdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void setProdJdbcTemplate(JdbcTemplate prodJdbcTemplate) {
        this.prodJdbcTemplate = prodJdbcTemplate;
    }

    public void addEmployee(Employee e) {
        String sql="INSERT INTO EMPLOYEE VALUES(?,?,?,?)";
        jdbcTemplate.update(sql,new Object[] {e.getId(),e.getName(),e.getDescription(),e.getSalary()});
    }
}

Why is this giving this error? I have provided the dataSource for both the jdbcTemplates. Also for now can we just ignore the datasource properties values as they are gonna be different for the two data sources.

So I have used ref in springconfig.xml as shown below and I have autowired both the jdbcTemplates.

The "prodjdbcTemplate" is for connecting to the prod database.

The "jdbcTemplate" is for connecting to performance database.

Update

When I changed my main class to :

package com.NettingTestingFramework;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestingFrameworkrunner {

    public static void main(String[] args) {
        ConfigurableApplicationContext context=new ClassPathXmlApplicationContext("springconfig.xml");
        EmployeeDao employeeDao=context.getBean("employeeDao",EmployeeDao.class);
        employeeDao.deleteEmployee(1);
        employeeDao.getAllEmployees().forEach(e->e.display());
        context.close();
    }

}

and removed @Component from EmployeeDao class and just created an empty bean in springconfig like:

<bean id="employeeDao" class="com.NettingTestingFramework.EmployeeDao">

    </bean>

Everything worked fine. But the big question is why did the previous thing not work? As I want this to be a SpringBootApplication is there some solution so that I can do this as in my previous code.

Xavier Bouclet :

The error is cristal clear. You have to use only one datasource between (dataSource and prodDatasource).

Remove one of them in your XML or set one as primary.

It should be the same bean and you should just change url and credentials in the config file of each environment.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

class required a single bean, but 2 were found:

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

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

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

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

Spring with MyBatis: expected single matching bean but found 2

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

Spring AWS - required bean could not be found

Referenced bean 'jpaMappingContext' not found

SpringFramework: expected single matching bean but found 2

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

Spring boot Field required a bean of type that could not be found

No qualifying bean, expected single matching bean but found 2

Spring MVC AuthenticationManager expected single matching bean but found 4

No qualifying bean of type found for dependency in Spring Boot single table

CommandLineRunner required a bean that could not be found

App.config key values are not being found using when referenced with ConfigurationManager.AppSettings

Entity Framework Core - No referenced design-time services were found

Spring Bean Not Found

Bean parameter could not be found when migrating spring boot

Spring - Inject null instead of throwing an exception when bean is not found

Spring required a bean of type 'AuthenticationManager'

Spring configuration - Autowired bean required?

'Field required a bean of type that could not be found.' error spring restful API using mongodb

Spring boot basic application: field NotesRepository required a bean of type 'com.demo.NotesRepository' that could not be found