Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use spring annotations like @Autowired in kotlin?

Tags:

spring

kotlin

Is it possible to do something like following in Kotlin?

@Autowired
internal var mongoTemplate: MongoTemplate

@Autowired
internal var solrClient: SolrClient
like image 777
eendroroy Avatar asked Feb 18 '16 11:02

eendroroy


People also ask

How do you Autowire in Kotlin?

Kotlin @Autowired constructor injection Note that we have annotated the constructor using @Autowired. This annotation instructs the Spring framework to inject the owner dependency into the Car bean. However, as of Spring 4.3, you no longer need to add @Autowired annotation to the class that has only one constructor.

What is difference between @autowired and @inject?

@Inject and @Autowired both annotations are used for autowiring in your application. @Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application. Sr.

What is difference between @bean and @autowire?

@Bean is just for the metadata definition to create the bean(equivalent to tag). @Autowired is to inject the dependancy into a bean(equivalent to ref XML tag/attribute).

Can I use Kotlin with Spring?

Spring Boot Gradle plugin automatically uses the Kotlin version declared via the Kotlin Gradle plugin. You can now take a deeper look at the generated application.


3 Answers

Recommended approach to do Dependency Injection in Spring is constructor injection:

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

Prior to Spring 4.3 constructor should be explicitly annotated with Autowired:

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

In rare cases, you might like to use field injection, and you can do it with the help of lateinit:

@Component
class YourBean {

    @Autowired
    private lateinit var mongoTemplate: MongoTemplate

    @Autowired
    private lateinit var solrClient: SolrClient
}

Constructor injection checks all dependencies at bean creation time and all injected fields is val, at other hand lateinit injected fields can be only var, and have little runtime overhead. And to test class with constructor, you don't need reflection.

Links:

  1. Documentation on lateinit
  2. Documentation on constructors
  3. Developing Spring Boot applications with Kotlin
like image 73
Ruslan Avatar answered Oct 16 '22 15:10

Ruslan


Yes, java annotations are supported in Kotlin mostly as in Java. One gotcha is annotations on the primary constructor requires the explicit 'constructor' keyword:

From https://kotlinlang.org/docs/reference/annotations.html

If you need to annotate the primary constructor of a class, you need to add the constructor keyword to the constructor declaration, and add the annotations before it:

class Foo @Inject constructor(dependency: MyDependency) {
  // ...
}
like image 9
Mike Buhot Avatar answered Oct 16 '22 13:10

Mike Buhot


You can also autowire dependencies through the constructor. Remember to annotate your dependencies with @Configuration, @Component, @Service etc

import org.springframework.stereotype.Component

@Component
class Foo (private val dependency: MyDependency) {
    //...
}
like image 5
Mr.Turtle Avatar answered Oct 16 '22 15:10

Mr.Turtle