Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring @在字段和setter方法上自动连线_Spring_Autowired - Fatal编程技术网

Spring @在字段和setter方法上自动连线

Spring @在字段和setter方法上自动连线,spring,autowired,Spring,Autowired,我在自动连接bean的属性时遇到了一个奇怪的情况@Autowired的put-on-setter方法不能正常工作,当我试图访问它的一些方法时,对应于该setter的字段返回我NullPointerException。另一方面,@Autowired-put-On-field直接工作正常 这种行为的原因可能是什么?将@Autowired放在字段和字段的相应setter之间有什么区别 以下是我的代码摘录: private JdbcOperations jdbcTemplate; protected

我在自动连接bean的属性时遇到了一个奇怪的情况@Autowired的put-on-setter方法不能正常工作,当我试图访问它的一些方法时,对应于该setter的字段返回我
NullPointerException
。另一方面,@Autowired-put-On-field直接工作正常

这种行为的原因可能是什么?将@Autowired放在字段和字段的相应setter之间有什么区别

以下是我的代码摘录:

private JdbcOperations jdbcTemplate;

protected JdbcOperations getJdbcTemplate() {
    return jdbcTemplate;
}

@Autowired
@Qualifier("jdbcTemplate")
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}
以下是上下文:

<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg ref="routingDataSource"/> 
</bean>


非常感谢您的解释

如果它适用于字段,那么它也适用于setter


很可能你错过了别的什么。在测试setter注入时,您是否偶然使用
new
关键字创建了这个类的实例?或者,在测试setter注入时,
jdbcTemplate
在上下文中不存在。有很多可能性…

感谢Sotirios,更新为3.0.x。找不到更多最新版本。您可以将
current
放在URL中版本应位于的位置,您将获得最新版本。