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>
我的主要课程也是如此:
@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();
}
}
这是我得到的错误-
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
在我的dao文件中,我有:
@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()});
}
}
为什么会出现此错误?我已经为两个jdbcTemplates提供了dataSource。同样,现在我们可以只忽略数据源属性值,因为这两个数据源的值将有所不同。
所以我在springconfig.xml中使用了ref,如下所示,并且我已经自动连接了两个jdbcTemplates。
“ prodjdbcTemplate”用于连接到生产数据库。
“ jdbcTemplate”用于连接到性能数据库。
当我将主班改为:
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();
}
}
并从EmployeeDao类中删除了@Component,并在springconfig中创建了一个空bean,如:
<bean id="employeeDao" class="com.NettingTestingFramework.EmployeeDao">
</bean>
一切正常。但是最大的问题是为什么以前的事情不起作用? 因为我希望这是一个SpringBootApplication,所以有一些解决方案,以便可以像之前的代码中那样进行。
答案 0 :(得分:0)
该错误非常清楚。您只需在(数据源和prodDatasource)之间使用一个数据源。
在您的XML中删除其中之一,或将其中之一设置为主。
它应该是同一个bean,您只需要在每个环境的配置文件中更改url和凭据。