Original: developing Spring Boot applications with Kotlin
Written in the forefront, as a Java veteran, for the development of more is to pursue a quick direct, concise, not willing to write boring code, such as Get Set. And Kotlin just out of time, do not catch a cold, after all, it is compiled into javascript, for a non-front-end of the death of loyalty, or not a cold. The recent chance to see Android development using kotlin, but also showed a piece of Java and Kotlin to get the view of the code, immediately raised interest, because I do the service side, in a degree niang, found this article, The original Springboot project was then rewritten into a mixed project of Kotlin and Java. so, I think It's a Translation.
Then wordy, this is not a translation, only to pick up dry goods.
What is kotlin?
1, Kotlin is a popular IDE idea of the company Jetbeans out of a jvm-based language
2, It is statically compiled language, and then have a lot of dynamic language features, so that your code more concise and clear
3. It has a nickname Swift on android, just like the swift language of ios, as Swift is to objective-c.
So using it to do Android development is cool, claiming to cut at least half the code
4, some very attractive Java veteran features, the data class automatically generate get set, do not write the get set method, although eclipse can help me to generate, but the discouragement entity will change, always need to fill a Gun.
extension, can extend the method of the class without inheriting, and is not anxious for a heap of utils.
5, and Java has a good interoperability, you can directly use the Java existing framework and class library, which kind of comfortable.
Springboot Project Introduction Kotlin
1, Pom.xml to join the dependency and plugin
If you use idea, Direct config Kotlin works on line 650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M02/8C/77/ Wkiom1hthvtglkwnaagbvqarjfm698.png-wh_500x0-wm_3-wmp_4-s_2476660615.png "title=" January 5, 2017 12-12-03.png "alt=" Wkiom1hthvtglkwnaagbvqarjfm698.png-wh_50 "/>
which is to join
<dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactid>kotlin-stdlib</ Artifactid> <version>${kotlin.version}</version></dependency>
and Maven's Plugin.
<plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactid>kotlin-maven-plugin</artifactid> <version>${ kotlin.version}</version> <executions> <execution> <id >compile</id> <phase> Compile</phase> <goals> <goal> Compile</goal> </goals> <configuration> <sourcedirs>${project.basedir}/src/main/kotlin</sourcedirs > </configuration> </execution> < Execution> <id>test-compile </id> <phase>test-compile </phase> <goals> <goal> Test-compile</goal> </goals > <configuration> <sourcedirs></sourcedirs> </configuration> </execution> </executions></plugin>
of course, I set up a Kotlin directory under Src/main to store Kotlin Code.
Then you can new a Kotlin file, the file name is Arbitrary.
Let's take a control example.
@RestControllerclass customercontroller (val repository:customerrepository) {@GetMapping ("/") fun FindAll () = Repository.findall () @GetMapping ("/{name}") fun findbylastname (@PathVariable Name:string) = Repository.findbylastname (name)}
is not very familiar with ah, Springboot in the comments directly Use.
@bean @Autowired, for example, are all the Same.
That means you want it. springboot application and other code to Kotlin is completely feasible, but the syntax is a little different, the others are the same
Note the Point:
1. Kotlin annotations do not support the way arrays Are. It must be changed to the following wording
@RequestMapping(method = arrayOf(RequestMethod.GET))
@EnableAutoConfiguration(exclude = arrayOf(Foo::class))
.
2, attribute injection needs to escape the symbol \, because Kotlin itself supports template $
@Value("\${some.property}")
.
3, for beans and need to be managed by the transaction, such as Biz Service flow, in front of class plus Open. Because the Kotlin default class is Final.
@Component
Open Class mytestbizimp:mytestbiz{
Override Fun Dofirst () {
....
}
}
How Java calls Kotlin
For example, with mybatis, use the mapper XML file at this time
Using the package name + class name is OK and Java is the Same. For example
Package xx.xx.xx
Class Myentity {
var name:string = ""
}
Interface Testmapper {
}
Such MyBatis mapper interface definitions and entities used can be written using kotlin.
finally, with idea, which comes with the Kotlin editor, copies Java code to the Kotlin editor, it automatically turns the Java code into Kotlin so Cool.
Summarize
As an obsessive-compulsive and neat-freak development, the use of Kotlin to write business code is a more feasible way, especially in the mixed mode, the old function code does not need to adjust, greatly protect the assets and provide the feasibility of the Attempt.
Its concise syntax is especially suited to the writing of business code, which interface and its IMP can be left in a file, and no other module with ^_^
This article is from the "ideology platform" blog, please be sure to keep this source http://xpbear.blog.51cto.com/391836/1889143
Developing Springboot applications with Kotlin