spring - 使用 @Inject 注释注入(inject)在 xml 文件中声明的 bean

标签 spring cdi

我无法让 @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/

相关文章:

java - 使用 CDI 转换器进行 JSF selectOneMenu 实体转换失败

Java:用于更新不共享相同引用的所有相等对象(在相同上下文中)的模式

java - 简单的 CDI 注入(inject)出现问题...在 SessionScoped Controller bean 中使用 @Inject 时出现 "Passivation"相关错误

java - 当配置文件不活动时包括 bean 定义

spring - Hibernate 返回 PersistentBag 而不是 List

java - 为什么 Spring Framework jpetstore 示例有几个依赖项问题?

java - CDI 和池化

java - 注入(inject)实现相同接口(interface)的 bean 列表

java - 使用对象的新实例初始化 bean

java - spring-boot2 上的 Resilience4j - 断路器未打开