【发布时间】:2015-02-24 04:38:30
【问题描述】:
我使用@Autowired 数百次,但今天我不明白,@Autowired 和 @Inject 在我刚刚创建的新项目中都没有工作,我得到了著名的错误
bean 类 [com.xxx.SomeDAO] 的无效属性“jdbcTemplate”:Bean 属性“jdbcTemplate”不可写或具有无效的设置器 方法。 setter的参数类型和getter的返回类型是否匹配?
当我在SomeDAO 中添加jdbcTemplate 的设置器时,它可以工作...
applicationContext.xml:
...
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="com.xxx.SomeDAO" class="com.xxx.SomeDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
...
SomeDAO.java:
import org.springframework.jdbc.core.JdbcTemplate;
import javax.inject.Inject;
//import org.springframework.beans.factory.annotation.Autowired;
public class SomeDAO {
@Inject // Doesn't work
//@Autowired // Doesn't work either
private JdbcTemplate jdbcTemplate;
...
/* Works if I add this setter
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}*/
}
什么可以防止通过注解注入?谢谢!
【问题讨论】:
-
你试过没有applicationContext.xml中的property元素吗?也许它与那个有冲突。我没有把注解和xml结合起来。
-
没有属性,只有字段。使用
@Autowired或类似方法的全部意义在于您不再需要在 XML 中指定依赖关系。
标签: java spring dependency-injection