我很难得到看似非常基本的自动接线.我正在尝试为我们的系统添加另一个数据源以用于仓库,但我遇到了一个问题.我一直不是Spring的专家,而且我一直在阅读这个问题,看起来我已经正确设置了所有内容.
我尝试更改我的组件扫描以包含存储库,但这并没有解决问题.我还尝试了指定bean名称的@Resource.
在我尝试构造jdbcTemplate之前,它永远不会抛出任何错误,因为dataSource为null.
我们使用注入的其他类似乎都运行正常,只是这个使用数据源注入的新类失败了(这是我们第一次进行数据源注入,其他一切都是Domain实体,但我们赢了' t使用传统的域/实体作为仓库db).
我确信我只是错过了一些非常简单的东西,但到目前为止我还没有找到它是什么.
只是一个调试回购
package com.foobar.repositories
@Repository
public class WarehouseRepository
{
@Autowired
private DataSource warehouseDataSource; // This is always null
private JdbcTemplate jdbcTemplate;
public WarehouseRepository()
{
jdbcTemplate = new JdbcTemplate(warehouseDataSource);
}
public void debug()
{
SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet("select * from foobar");
int i = sqlRowSet.getInt(0);
String test = sqlRowSet.getString(1);
}
}
Run Code Online (Sandbox Code Playgroud)
XML配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd"
>
<context:annotation-config/>
<context:component-scan base-package="com.foobar.domain"/> …Run Code Online (Sandbox Code Playgroud)