KotlinConf 2018 - Kotlin and Spring Boot, a Match Made in Heaven by Nicolas Frankel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] you [Music] [Applause] [Music] thanks to beer for this talk about spring boot and cotton that is my first time in Amsterdam that's my first time at cutting comps so I'm Nicola Frankel I love cotton I love spring goods and as soon as cotton was let's say usable on the server side I try to play with it and now I'm super happy that inside of of pivotal there are initiatives to integrate them and this talk will be about this I now works for a company called exoskeleton before I joke that I work for a small startup called SAP now I really do work for small start-up in Switzerland and I also changed my position from consultants to a developer advocate so if you enjoyed the talk please go on the exascale website so that can send me to other conferences I if you don't well now you can do whatever you want we offer cloud computing services in Europe and some that means that our data centers are in Europe which means there is no cloud acts letting greedy government get you their hands on your data if you know what I mean ok back to the talk who here is already a Springwood developer yeah that's great that's what I thought so you know about Kotlin you know about spring boots you know everything so this slide is already gone and you might have issues with spring boot at some point either talked saying if there is no magic about spring boot here is how it works but still there are a lot of annotations you must know the annotation the big issue with annotations it's that it's very hard to follow the flow of program I mean it's not declarative meaning that the annotation is handled somewhere and there is no direct link between the annotation and the annotation and law and it's very hard to understand yeah you cannot put breakpoints on annotations right so many people are bitching against framework in general again spring boot and spring in particular saying it's all magical and I want to be in control of my code and in some places I am I agree yeah it's a lot of magic and if you cannot spend the time to understand the framework if you just want to do a quick fix to an existing application there might be issue with that and so this in there is initiative inside of pivotal that says okay we heard you we agree with that let's try to do the same but with explicitness with what they called functional configuration which i think is i might disagree on the term functional I prefer to say explicit explicit configuration so instead of implicit let's have a real flow of the program let's see everything what happens and let's put breakpoints if they need be demo time works for you great yeah I don't like slide where so much so the first thing that you might know already is that if you start a new project on on the start of spring IO there is already cutting that is the first-class citizen that's already super great as for me what I did is I created already a project because yeah III don't want to do everything here that basically is a like small light no please it's a small application that delivers a web service over a database that can let you read the repository so everybody understand this code yes great I just run it just to show you that it's not no that's not going to be fun too long okay now if I go on this stuff yeah it's not the right one up again that's the end mo I start the beginning demo never reuse your demo just before never I always said that but I still do it because yeah okay this should work now these very simple stuff and of course since I'm right and doing some red stuff I can access the night and city by its own handle this is this is college to you okay no magic everything works now I want to try to remove as much as please Cygnus as possible I want to remove everything that seems like magic like annotations okay the first is there is a venue about that is that first we need to migrate to reactive right now we need to migrate to reactive it might change in the future there is plan to change that but right now and there is no other way so the first thing is I will use MongoDB the second is I will replace the web by web flux because in spring file you've get reactive and I will remove the database that is embedded that is h2 with a MongoDB embedded I mean nothing mind lowing I just replace everything and normally at this point everything should work or not yeah I like interactivity you cannot just light thanks for that so it doesn't work because of the import it doesn't worry because of here it's not an entity anymore and here it's not the flickering gets on my nerve it's not great I hope you accept it because I cannot do anything against it at this point normally everything should be fine I've replaced the dependency I've replaced everything so will it work not yeah you know yeah I used something very magic in spring I used the light a let's define a sequel file and this will get imported automatically and of course since and now I'm using MongoDB it doesn't work that well so I need to migrate these the like implicit stuff into something more explicit so I will need to create a beam and to insert my data no no repository that insert it doesn't know it that's not very bad why I love it yes now I know I cannot use the paging and sorting repository I need to like change it to the reactive repository now it works again and if you checked it went very fast honestly but here I have the iterable and optional and when I change it to the repository which is reactive it changes to like the reactive data types that flux and mono so let's restart everything and normally at this point it's a command line or an ER so it will get executed when the application is run when the contexts is created and it inserts my data and normally at this point I should get my data inside yes so I didn't change anything well I just lost my transactions huh since it's not was no snap who cares about transactions anyway but I still have this magic add bean stuff and I don't like it so much I have still have my rest controller so what I would like to do is the first thing I would want to do is to remove the controller and create routes and I can do it something like that yeah I won't type everything every time so here I can create because in spring 5 there is this routing function that lets you like handle that stuff with not a real DSL but like and if you want it to be it a bit more yes le you can do stuff like that so that at least it gets your static import and stuff so it's a bit better and of course this is only the first one a word of an interesting stuff is that here I cannot just get the person /id and put it into a pot variable I need to get the post variable from the request itself I need again it's not about annotations I need to be explicit about it and the second one I need to do is like that and now with some luck that should be the equivalent something like that let's start the application again and check are you familiar with this kind of code already who is doing reactive stuff already yeah the brave one and I still have the same code running still works everything is wearing that's pretty good right that's pretty good I already remove the controller's this is less magic however what I don't like into this code is that stuff I mean root should be rotting here we already have like kind of like business logic so let's apply the single responsibility principle and create a handler that a dedicated class to handle that stuff and that's why or it's so here I will just need because here I have an expression body I just need to do like that I return it here it tells me you should say the type I tell it the type and now I can at this place create a handler which is a person handler and pass it the repository at this point I am able to just say n ler dot read one and say ok I pass the request which is I T in that case and here I can say n ler dot read all and pass I T this is a bit better now I have taken the light business light codes whatever it is and put it into a handler and so the route does only routing to the place it's supposed to go let's check every time because I'm a bit paranoid yeah and there is a catch at some point yeah I doesn't work one will work no not m % not m % not m % yes ok this works as well here at this point we can say mmm this is nice if you if you attended the previous talk you know about method references so we can say here let's call let's convert the lambda to reference I didn't change anything just using references if you want I can rerun because just to be sure but IntelliJ is converting it for me so at this point probably there is not that much issue still works so next up to migrate is now I have like I have moved the controller to the bean I have moved that to the logic to a handler the endler is now completely annotation free you can test it in isolation completely in isolation for unit testing no magic required I still have this beam method and I mean that's better than self annotated class because at least you like main code is still free but I would like to be as explicit as possible and so I would love to remove those bean stuff let's do it I create and that is pretty brand new beans methods using the beans definition DSL and that is the exact same stuff as here I can remove it now the question is how does it get cold well there is a bit of a trick there we will register it using an auto wired function it happens but at least it's just one simple like Auto wired annotation and I can have all my Dean's there at least that's what you think now does it still work yes perfect so now your reaction would be thank you come afterwards I will give you a nice cup and for our clouds yeah there are rewards if you are interactive I would like to do the same so I will do the same here the but first I will use likewise I will use another DSL there are so many DSS not only two but I mean I create another DSL and likewise I can remove it and as you can sorry I know unfortunately resolution ok I will reduce the resolution better now I need to reduce here as well okay no flickering that for everything is fine let's continue thanks for the gentleman sorry excuse me a bit now I should assume in the eye I mean you need to make some effort and this is not free you you need to put some some strain into that okay so okay I'm sorry for that I unfortunately I cannot do anything against that but yeah the code is available on github it's on my last slides you will have the reference so you can use it yourself and do the same stuff so pretend you see the screen the screen not flickering please so now I've used the Rueter DSL to create a Rooter right and basically what I need to do is do the same as here so I will replace this stuff and say okay here i create a new beam right and in this new beam and here i need something from you I need you to believe me I will do that what WTF for non-american speakers yes so I think that deserves a bit of an explanation right okay I expected that that's good okay I am in my bidding definition DSL right this is my context so imagine I create a beam of type person and ler and that person and law requires a person repository so far so good okay what I could do is say context that get bin and get me the bin of type person repository everybody who has done a bit of string can do that even though context dot get bin you should never do it yourself right right well that's pretty cool because this context is not available it's like internal inside of the bin definition DSL and if you look at the ref it does the context that get Ben here so it's just a shortcut right no magic now the thing is we say we want the beam of type person repository and I wrote just that no ref I mean ref but no type why because again if you attended the previous talk you see that the function is in line and refight meaning that it is kept at compile time and because the person being the person in lasari requires a type of person repository ref is not enough to understand what type it needs magic anymore or just simple code yeah type inference ref eyes type inference which is even better okay so is that good and then the next step I will display it in the end so do we all agree on that great thanks would that be pretty awkward otherwise and all I started again Oh oops doesn't work anymore the issue there is that there is a lifecycle issue because the roots they need to be declared and inferred at the beginning of the lifecycle and when this function is called it's already too late what shall we do any idea okay I will create a bins initialize class with a specific method and no I don't want that in initialize and here I must use application context initializer now what I can do is I can remove the auto wired finally I hate and how does this stuff gets cold well there is a nice property cold context that initialize about classes who knew about it yeah good good point yeah I didn't know about it so this is also you solve the issue so now I have these been initializer and I remove the auto wires and now everything should be working again no because now I need to pass a beam of type personnel or I forgot because here what I'm telling them is yet here is ya take the beam of type person endler but I have no beam of type person and law so I should do something like that person and ler and say refu and now it's the same it's person endler needs a thing of type person repository the person repository because I'm using spring data is already in the context though normally that should work and yes it works again and now if you followed my points you might say something at this point saying it still super implicit if you don't know about this property what can you do so I remove the annotations and put them in a property file if you don't have IntelliJ if you don't look at the documentation it's very hard to auto discover that stuff okay fair point so what we can do is remove the property and in the function we can explicitly add initializers that is much better right that is explicit and now you can follow the flow of the code from the beginning still works but I mean developers are lazy right who is not lazy here really yeah developers are lazy so that that class is pretty just a thin wrapper about this coal it's pretty stupid it's pretty useless the good thing about the DSL the bean DSL is that here we can directly call the beans function because the beans function that is defined here I will do that this is a bin bin definition DSL and if I check begin definition SL sorry no not that one where is it now here the bin definition DSL it already innovates from the same type so this is just a shortcut and you don't need that class anymore so we can remove this bin initializer class and we can start again and now it works at this point I have like like documents not withstanding I have one single annotation in my application the rest is just everything is declarative that is pretty cool right we think this is pretty cool but some of you might argue that is still too much magic around I mean what does the spring bootie application annotation does yeah you wonder right yeah me as well what we can do and then I I won't code it because I'm again I'm super lazy for that I will just remove everything we have done so far and check the latest stuff and behold if it works please do work this is my new poem I've added an artifact called Kofu I don't know how it's pronounced like runnin disco food because like kung foo but perhaps it's not that much but it means cutting functional again it's more declarative than functional but anyway previously we had to put our plugins you might know that if you use rain boots and cotton spring would will try to inherit from your configuration classes and by default Kotlin classes are final they are not often they cannot be inherited from so we had to add this compiler plugin to make those open key word automatic no more no more some people are super happy here and I'm happy about that and now this is the application no single annotation nothing everything can yeah not this one okay I'll come on this is Mongolia but this is a simple main function and this simple main function called the AB Doron method and it can be found everything is in the code and if this app is declared here and there is a new DSL called application DSL and everything here works out of the box and more importantly can be checked up on and you can say Oh what does the server does bla bla bla and now if I try to run the stuff and I run a simple : class I run no spring boot bla bla bla in IntelliJ I ran a single cutting class I've got the exact same result but everything is declared everything is explicit same stuff same stuff yeah at this point in general someone say a but sorry excuse me again new lines yeah and I didn't change the application or properties but again this is not explicit so it's not taken into account but what we can do is even if you don't know anything you can check that there is this Jackson stuff and here inside the code you can say these dots and any IDE which is basically even middly smart can tell you there is something like that perhaps even vs code I don't know never used it don't take me wrong a lot of people use it I should probably try and here in the end output true fix everything yeah you don't know the API don't worry just try to autocomplete and use the IDE it will work for you you don't need to browse into the tons of spring' documentation that you have everything is available codec you everything like embedded these dots whatever and then you can it's a proper DSL I mean who likes that great a-and just do I'm just doing the demo I I didn't cut anything so the first takeaway of that is that you need to be reactive yet might change in the future I hope it change because again I'm not a reactive guy but right now you need to migrate to reactive afterwards you take your routes sorry you take your controllers you migrate them to routes you take the logic that could have been into your service or your controller you put them into an lures and you use that using the route or DSL cutting route or DSL bins use the bin definition DSL and that's all afterwards again if you want to go further like I displayed that you use something called spring foo that is basically to implementation so for Kofu and Ja foo which is made by this guy sitting here yeah just a word of advice this is experimental some stuff might end up in spring boots some stuff might not I would really really be cautious about using it now in production we are developers we love the bleeding edge but at the end the software I must still run so be careful about that you can of course like I do play with it but perhaps not use it right now right out of the bat and so basically this configuration is now an incubator this project is an incubator and Sebastian will work on it like have some feedback because you will be using it change it blah blah blah and at the end it will or not some parts or not might end up in the main spring good photo satori so just that I displayed then you have the slides you have explicit configuration no more annotations at all everything is explicit and declarative the good thing about that is who knows about growl VM I have the room so REM is a new project by Oracle that basically one of the feature of güell VM is that it allows you to take a jar or class but basically a jar and compile it to native code I mean everybody loves Java but the greatness the performance of Java is because you run its server-side over a long period of time and that it improves its performance over time because it checks which pass is mistaken la blah blah the problem is for command-line interface you run it only once not that great if you are doing like function as a service gvm not that great for this reason if you already know Java if you already know how to compile your jar if you take the jar it will be super easy using rel VM to compile it to native code and then you can even use case you ever dreamed of and the problem if you take a normal spring with application you try to grow a VM it to native is that because it uses a lot of reflection a lot of magic and recover Braille VM doesn't know how to infer what it should compile or not if you make everything declarative then girl VM can follow the path that you follow as well and then compile everything to native code and at this point you have very fast command-line interfaces in Java well not at this point but it's a bit further into the future so the pre take away that spring would love scottoline as you all do thanks for your attention I think there is some time for QA you can you can read my blog I try to publish weekly post you can follow me on Twitter and more importantly if you want to do the same stuff as I did here on stage there is a repository for that where it's on github so you can try and for each step you have you have the the the the little logical step that goes to the next step just as I did here any question hey okay thanks thanks a lot so thanks for the nice presentation Nicola so the GSL the bins and the route our DSL our production ready you can already use them in production I would advise to mainly use the Reuter DSL and the reactive stuff for now it's perfectly integrated into spring boot admin's and that works for the bins DSL as it was shown in the presentation there is some lifecycle issue is boot so you have to use initializer and the integration is not that that good that that's why I have started a spring foo to go the next step into leveraging and explicit and declarative cut in DSL to configuring a spin boot application so in a nutshell it's just applying the same principle that are currently used in bins and rotor and applying to this new applications yes l this part the application DSL is super new I'm going to walk on that quite a lot and already ten external contributors have contributed to the project so it's not just about using it it's also about maybe provide some poor request give some feedbacks create some issues it's quite easy in fact it's just functional code it's just some kind of dsl on top of regular spring from our api so it's quite quite easy to contribute in fact and so in spring foo there is this DSL for cotton there is also a DSL for Java so these two things are quite experimental and I'm not sure how long it will take to incubate as there is a lot of people interested so I tend to think we will make it prediction ready and not sure exactly how but I tend to think that would be the case the result so other features like official coroutines report so we are going to support officially co-routines in spring from Mach 5.2 that will be available in boot to the two if we get call streams report but I have token with Roman about that and I think we should get that so in the next measure spring Russian there will be cool routines reporter that's important because like you said not everybody need to use a reactive API because maybe you just want to build a small crude application that leverage a non-blocking runtime so we are going to provide a cou routine API on top of the web flags report spring that a MongoDB support and also we have announced last week that we are working on reactive SQL initiative which is called A to D BC and we are going to provide a reactive encouraging API on top of SQL with PostgreSQL support and that will be production ready next year so this part is ready to be production ready the coroutines report is going to be production ready next year including the SQL support and that's something super new in super useful I think and the application DSL will take a little bit more time to mature but I'm yeah super motivated to to make it production ready at some point not sure exactly how will it be integrated into spring boots will it be another thing I'm not sure it's about or about the question is ping boot equals auto configuration or is ping boots something that could leverage to model Auto configuration and a declarative approach with this kind of dsl time will say what is a good answer but feel free to provide feedback and contribute to to spin foo they're interesting stuff about this initiative is that before I mean when I went to create a spring of normal spring project I told my team guys go go home and see you in two days then I craft my palm and I end up my version and I create a use case everything fine tune that I wanted it to do and string boot was meant to address that because now with spring boot you can create a project with everything ready in like 15 minutes the palm is everything is handled the the versions in the in the dependency are all addressed it's very nice and now people are bitching yeah but it's to magic and it goes from one side to the other and now we go back to the I want to understand what happens and now I have to declare everything again so this is how it goes in computer science it's just like one swing to this side to the other side every now and then yeah and I think that's interesting and I think both makes sense I mean it's it's about choice it's about you personal personal preferences so I tend to think that in Java world people will stay in majority to use annotation auto configuration mode I tend to think that uncut inside people try to prefer explicit things especially given the fact that with DSS we are able to provide some kind of declarative but very sure they're expressive configuration so I think this kind of approach is more cartoonish but time will say and it's perfectly okay to just use a notation if you prefer with Auto configuration that two different things and that is a very better or was it just different and you have the IDS to give you the choice basically we still have time for one more question yes gentlemen in the end since we move all the configuration and we declare them in the court how do we deal with different configuration for different environments for example and spring boot config server and so on so you mean profiles well yeah like well there is a DSL for that it's just like for yeah okay now it sits in the if I remember well it's in the bin definition DSL there is a profile stuff conformation yes or no yes the idea is to put everything that is that does not vary in the codes DSL and just put the things that change in the environment in property files or on the unknown variables so you can directly access to the unknown variables via all with the array operator to access to any available environment and you can just declare explicitly it's not an annotation at configuration properties but it's the same binding mechanism so you declare properties between brackets configuration properties a class name with the prefix and you can use the same binding mechanism that you have in Springwood but that just explicit and without a notation that is very important that it doesn't change the mainspring boot engine it's just a thin wrapper above it that average the that replaces the annotation stuff so you keep the same mechanism inside so you can also reuse all your code and what you did so you reuse data you use configuration properties you reuse the looking infrastructure or this kind of thing just reused but declared in another way okay thank you very much [Applause]
Info
Channel: JetBrainsTV
Views: 12,858
Rating: 4.5080214 out of 5
Keywords: jetbrains, kotlin, kotlinconf, kotlinconf18, kotlinconf 2018, Spring boot, spring, java
Id: pSiZVAeReeg
Channel Id: undefined
Length: 43min 55sec (2635 seconds)
Published: Fri Oct 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.