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.
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.
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.
Comments