簡體   English   中英

春季Kotlin setter依賴注入

[英]Kotlin setter dependency Injection in Spring

我正在學習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>

然后我在科特林嘗試過。

@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解決方案。

像在Java中一樣,您不能使用實例化實例化注入的屬性。 在Java中,當您進行等效操作時,會得到NullPointerException ,例如

@Autowired
private Datasource datasource;
private JdbcTemplate jt = new JdbcTemplace(dataSource);

但是您可以在注入所有內容后讓Spring調用您選擇的方法:

lateinit var jt: JdbcTemplate
@PostConstruct
fun initAfterAutowireIsDone() {
    jt = JdbcTemplate(dataSource)
}

還可以使用InitializingBean接口代替注釋。 在Java中也是如此。

您在Kotlin中擁有的另一個選擇是,當您確保在實例化時不訪問它時,請使用惰性屬性。

val jt: JdbcTemplate by lazy { JdbcTemplate(dataSource) }

當我認為zapl答案正確時,我也建議您考慮構造函數參數注入:

@Component
class StudentDao @Autowired constructor(
    private val dataSource: DataSource
) {

    private val jt = JdbcTemplate(dataSource)

}

這樣,您可以獲取不可修改的變量,而無需發出有關后期初始化的警告。

另外,如果只需要dataSource變量來初始化jt ,則可以從構造函數中刪除val鍵。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM