注入Kotlin类的不可变构造函数的正确方法

时间:2015-10-25 02:08:11

标签: java spring spring-mvc spring-boot kotlin

使用Spring + Kotlin声明不可变构造函数注入类的正确方法是什么?目前我有:

@RestController
public class AuthorizationController
{
    @Inject
    lateinit var facebookAuth: FacebookAuthorizationService //Mutable?

    @RequestMapping("/authorization")
    public fun authorization(@RequestParam(value = "network-type", defaultValue = "Facebook") name: String,
                             @RequestParam(value = "oauth-token") oauthToken: String,
                             @RequestParam(value = "oauth-token-secret",
                                     required = false) oauthTokenSecret: String?): Authorization
    {
        //TODO: Handle other network types
        return facebookAuth.authorization(oauthToken)
    }
}

我希望facebookAuth属性是不可变的。

1 个答案:

答案 0 :(得分:3)

在这里对Kotlin的经验不多,但我认为必须用

完成
public class AuthorizationController @Inject constructor(private val facebookAuth: FacebookAuthorizationService) {

    ...

}