【发布时间】:2013-04-27 16:03:20
【问题描述】:
我无法让@Inject 正常工作。我正在尝试使用 @Inject 注释从 xml 注入一个 bean,但我收到错误消息
"java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required".
我也一直在尝试与@Qualifier("dataSource") 结合使用,但无论我将@Qualifier 放在哪里,它都会显示"The annotation @Qualifier is disallowed for this location"。
我一直在阅读大量关于 @Inject 的文档,但我似乎找不到任何提及对 xml 中声明的 bean 进行任何特殊处理的任何内容。
但是,我猜 Spring 会在扫描 dataSourceBean 之前尝试创建 FooDaoImpl bean。
我将如何使用@Inject 来注入在 xml 文件中声明的 dataSource bean?
甚至可能,使用@Inject?
FooDaoImpl.java
@Repository
public class FooDaoImpl extends NamedParameterJdbcDaoSupport implements FooDao {
@Inject
private DataSource dataSource;
DSLContext create = DSL.using(dataSource, SQLDialect.DB2);
}
Spring-Module.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: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 />
<context:component-scan base-package="com.example.foobar" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.ibm.db2.jcc.DB2Driver" />
<property name="jdbcUrl" value="jdbc:db2://localhost:50000/BLABLA" />
<property name="user" value="PAPAYA" />
<property name="password" value="COCONUT" />
</bean>
干杯!
【问题讨论】:
-
尝试添加
@Component作为类的注解? -
@Ben 我认为
@Repository是@Component。我也试过这个,但它没有用,我害怕。还是谢谢!