我无法让 @Inject
正常工作。我正在尝试使用 @Inject
注释从 xml 注入(inject)一个 bean,但我收到错误消息
“java.lang.IllegalArgumentException:‘dataSource’或‘jdbcTemplate’是必需的”
。
我也一直在尝试与 @Qualifier("dataSource")
结合使用,但是无论我把 @Qualifier
放在什么地方,它都会说 "The annotation @Qualifier此位置不允许使用”
。
我一直在阅读有关 @Inject
的大量文档,但我似乎找不到任何提及对在 xml 中声明的 bean 进行任何特殊处理的内容。
但是,我猜测 Spring 正在尝试在扫描 dataSourceBean 之前创建 FooDaoImpl bean。
我将如何使用 @Inject
注入(inject) xml 文件中声明的数据源 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>
干杯!
最佳答案
这在 Spring 中工作正常。我使用 @Autowired
注释,而不是 @Inject
。
关于spring - 使用 @Inject 注释注入(inject)在 xml 文件中声明的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16355724/