【发布时间】:2018-09-25 15:32:27
【问题描述】:
我正在学习 spring data,因为我也在学习 kotlin,所以我决定在春季学习期间使用 kotlin。所以我想问一下我们如何在kotlin中实现setter依赖注入?在 Java 中,我们可以如下所示。
@Component
public class StudentDaoImp {
public DataSource dataSource;
public JdbcTemplate jdbcTemplate;
public DataSource getDataSource() {
return dataSource;
}
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
这是我的spring.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:~/test" />
</bean>
<context:component-scan base-package="com.package.*" />
</beans>
然后我在kotlin中试了一下。
@Component
class StudentDao {
@Autowired
lateinit var dataSource: DataSource
var jt = JdbcTemplate(dataSource)
}
然后我得到了异常。
引起:kotlin.UninitializedPropertyAccessException:lateinit 属性 dataSource 尚未初始化
我知道这个异常,因为我在 autowired 发生之前使用了 dataSource。所以我也试过这个。
@Autowired
fun setDataSource(dataSource: DataSource) {
this.jt = JdbcTemplate(dataSource)
}
这也是一个错误,因为 JVM 在幕后已经有了那个签名。
那么如何使用dataSource参数初始化JdbcTemplate?
注意:我只想要代码端示例/解决方案。我知道 XML 解决方案。
【问题讨论】: