如何在kotlin中使用像@Autowired这样的弹簧注释?

时间:2016-02-18 11:03:26

标签: spring kotlin

是否有可能在Kotlin中做类似的事情?

$scope.GridOptions =
            {
                pageable: { refresh: true, pageSizes: true },
                columns: [
                    // other columns here
                    {
                        field: "RegistrationStatus", title: "Status", width: 13,
                        filterable: {
                            operators: {
                                string: {
                                    eq: "Is equal to",
                                    neq: "Is not equal to"
                                }
                            },
                            ui: function(element) {
                                element.kendoDropDownList({
                                    dataSource: [
                                        { Name: "Registered", Value: "PassedInterview" },
                                        { Name: "Pending", Value: "PassedInterview" },
                                        { Name: "Rejected", Value: "Rejected" }
                                    ],
                                    dataTextField: "Name",
                                    dataValueField: "Value",
                                    optionLabel: "--Select Status--"
                                    defaultValue: "PassedInterview" // smth like this, is this possible? or achieve in any other methods instead of taking dropdown id, which I dont know because this is hided inside
                                });
                            }
                        }
                    }
                ],
                sortable: true,
                filterable: true,
                selectable: 'row',
                editable: 'inline'
            };

5 个答案:

答案 0 :(得分:112)

可以肯定这是可能的,你有几个选项,我建议使用带注释的构造函数,但是lateinit也有用,在某些情况下可能有用:

Lateinit:

@Component
class YourBean {

    @Autowired
    lateinit var mongoTemplate: MongoTemplate

    @Autowired
    lateinit var solrClient: SolrClient
}

构造

@Component
class YourBean @Autowired constructor(
    private val mongoTemplate: MongoTemplate, 
    private val solrClient: SolrClient
) {
  // code
}

Spring 4.3构造函数:

@Component
class YourBean(
    private val mongoTemplate: MongoTemplate, 
    private val solrClient: SolrClient
) {
  // code
}

构造函数版本检查bean创建时的所有依赖关系以及所有注入的字段 - val,在其他方面,lateinit注入的字段只能是var,并且运行时占用空间很小。要使用构造函数测试类,您不需要反射。

链接:

  1. Documentation on lateinit
  2. Documentation on constructors
  3. Developing Spring Boot applications with Kotlin

答案 1 :(得分:5)

是的,Kotlin支持Java注释,主要是在Java中。 一个问题是主构造函数上的注释需要显式的'constructor'关键字:

来自https://kotlinlang.org/docs/reference/annotations.html

  

如果需要注释类的主要构造函数,则需要将constructor关键字添加到构造函数声明中,并在其前面添加注释:

class Foo @Inject constructor(dependency: MyDependency) {
  // ...
}

答案 2 :(得分:2)

您还可以通过构造函数自动关联依赖项。请记住用@Configuration, @Component, @Service等来注释您的依赖项

import org.springframework.stereotype.Component

@Component
class Foo (private val dependency: MyDependency) {
    //...
}

答案 3 :(得分:0)

喜欢

@Component class Girl( @Autowired var outfit: Outfit)

答案 4 :(得分:0)

如果您想要属性注入但不喜欢 lateinit var,这是我使用 property delegate 的解决方案:

private lateinit var ctx: ApplicationContext

@Component
private class CtxVarConfigurer : ApplicationContextAware {
    override fun setApplicationContext(context: ApplicationContext) {
        ctx = context
    }
}

inline fun <reified T : Any> autowired(name: String? = null) = Autowired(T::class.java, name)

class Autowired<T : Any>(private val javaType: Class<T>, private val name: String?) {

    private val value by lazy {
        if (name == null) {
            ctx.getBean(javaType)
        } else {
            ctx.getBean(name, javaType)
        }
    }

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T = value

}

然后您可以使用更好的 by 委托语法:

@Service
class MyService {

    private val serviceToBeInjected: ServiceA by autowired()

    private val ambiguousBean: AmbiguousService by autowired("qualifier")

}