Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当指定多个数据源时,从spring注入/自动连接数据源_Java_Spring - Fatal编程技术网

Java 当指定多个数据源时,从spring注入/自动连接数据源

Java 当指定多个数据源时,从spring注入/自动连接数据源,java,spring,Java,Spring,我正试图从context.xml文件中注入一个数据源。我有这个配置文件,但问题是,我在任何时候都有多个数据源。当我尝试注入它们中的任何一个时,我会得到一个错误,表明没有匹配的bean。 这是我的密码 @Inject @Named("dataSourceAccounts") //@Autowired @Override public void setDataSource(DataSource dataSource) { // TODO Aut

我正试图从context.xml文件中注入一个数据源。我有这个配置文件,但问题是,我在任何时候都有多个数据源。当我尝试注入它们中的任何一个时,我会得到一个错误,表明没有匹配的bean。 这是我的密码

    @Inject
    @Named("dataSourceAccounts")
    //@Autowired
    @Override
    public void setDataSource(DataSource dataSource) {
        // TODO Auto-generated method stub
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        System.out.println("jdbcTemplate is null? " + (jdbcTemplate == null));

    }

//for the context.xml
<context:component-scan base-package="business" />
   <context:component-scan base-package="middleware" />
   <context:component-scan base-package="presentation" /> 
   <context:annotation-config/>

   <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />
    <bean id="txManagerProducts" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourceProducts"/>
    </bean >
    <bean id="txManagerAccounts" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourceAccounts"/>
    </bean >

     <bean id="dataSourceProducts" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/productsdb" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/accountsdb" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
@Inject
@命名(“数据源帐户”)
//@自动连线
@凌驾
public void setDataSource(数据源数据源){
//TODO自动生成的方法存根
this.jdbcTemplate=新的jdbcTemplate(数据源);
System.out.println(“jdbcTemplate为null?”+(jdbcTemplate==null));
}
//对于context.xml

感谢您的帮助。

如果您想为应注入的依赖项指定限定名称,则@Named应位于参数定义之前:

@Inject
@Override
public void setDataSource(@Named("dataSourceAccounts") DataSource dataSource) {
    // TODO Auto-generated method stub
    this.jdbcTemplate = new JdbcTemplate(dataSource);
    System.out.println("jdbcTemplate is null? " + (jdbcTemplate == null));

}
或者使用spring注释:

@Autowired
@Override
public void setDataSource(@Qualifier("dataSourceAccounts") DataSource dataSource)
这应该行得通

@javax.annotation.Resource(name="dataSourceProducts")
public void setDataSource(DataSource dataSource) {
                 ...
}

javax.annotation.Resource在Java SE中

它显示另一个错误,即“没有定义名为'dataSourceProducts'的bean”,尽管我在xml中定义了它file@Ayodeji可能是因为XML配置中的数据源名称为:“dataSourceProducts”和“dataSource”,而您指定了“dataSourceAccounts”待注入。原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[javax.sql.DataSource]的匹配bean:应至少有1个符合此依赖项autowire候选项条件的bean。依赖项注释:{@javax.inject.Named(value=dataSourceAccounts)}位于org.springframework.beans.factory.support.DefaultListableBeanFactory.RaiseNouchBeanDefinitionException(DefaultListableBeanFactory.java:920),我不得不修剪其中的一些it@Ayodeji您使用的是哪个版本的spring?很抱歉响应太晚。我正在使用3(3.0.5)