Spring Tips: Java 14 (or: Can Your Java Do This?)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi spring fans welcome to another installment of spring tips in this installment we're going to take a look at Java we're not gonna look at any one particular feature of spring but instead I just want to pull back the covers and kind of look at the new version of Java Java 14 which is really impressive accumulatively the last several years I've seen a number of releases which if you haven't encountered them and all the features that they bring might be fairly well astonishing I couldn't believe writing some code as I was yesterday with the new release what this looks like and I I had to step back and say oh hey this is really nice this is actually a very different language then even Java 8 and that was of course a very different anguish then Java 7 just as Java 5 was a very different language then Java 1.4 we have come a long way since Java file and I wouldn't just show off some of those features and right now we're at a perfect time where you can now get access to IDE s at now support languages the build tools support them the frameworks like spring support them and so we're gonna take a look at them by using spring boot 2.30 m2 or later so anything later will work just fine of course and we're just gonna call this 14 all right now I'm gonna choose Java 13 this is a 14 there yet but as you'll see it is possible to make that work so we're gonna go ahead and add that I'm gonna bring in I'm gonna build an application that uses post cos and the JDBC support spring data JDBC or just the spring JDBC support so we'll go ahead and build a very simple application I'm gonna hit generate and that'll give me in a new directory which I'll open up the new project CD downloads I'm gonna unzip this and I'm gonna open this with JetBrains IntelliJ EAP right so here it is EAP 2021 this is a early access preview release it's not the final one but you know of course by the time watches it may where will be a very old ancient legacy version by the time you were sort of confronting this issue so who knows let's open this up hopefully it'll open up sometimes these eaps are a little less and stable okay come on not today huh okay let's see it seems like every other restart works just fine and just do it one more time good stuff Hey look at that huh it worked so now we've got we're gonna open up a project okay and here I'll do a maven install and this is an amazing project okay okay synchronizing now in order for this to work in order to the for this to be like just it works out the Box kind of thing I have to save a few plugins right so I'm going to do plugin and the first plugin will do is the compiler planning okay so artifact ID may even compile a plugin I'm just overriding the default plugin that's provided out of the box so I can use a number of different features here when you use release 14 of course I'm by the way you have to specify Java 14 up here by the time you watch this maybe this maven compiler stuff this business I'm doing here won't be required but in the meantime act as though it is okay okay enable the preview feature so every release of Java these days has preview features that are sort of meant to test the waters for new features that will eventually be made part of the actual release well in order to enjoy all the few stuff you have to enable those arguments those are those PD features which is fine that's a good problem to have so we can say yes I want unable to for the Java compiler and what else do I want to do I guess I want to do parameters equals true so primary Z equals true this actually comes from the maven balm I'm sorry the maven parent that comes in Springboro started parent they've already provided definitions for the maven compiler plugin to I don't know what this does but I know it's set in the parent configuration for this plugin and since I'm overriding it I want to make sure to carry it forward okay so artifact ID we also have to configure the testing so like that I want configuration good and then we're here it's a little differently it's a little different state of having compiler args and argh inside of it you have a compiler line and I'll just say anyone please okay so that's it that's now if you have a IntelliJ IDEA 2021 that later or later and you have this configuration you can just open up this project and assuming you're running Java for team and that's important by the way I use SDK men if you do it SDK man or SDK manager you can list all the JDK is out there that you might want to install you can see I've got a bunch of them installed I've got 13 I've got two versions of 13 I've got an old eleven I've got old eight right do you know Amazon sorry Amazon I don't have anything installed there but you know you could we've got a Zulu Zulu we've got growled the m's version the job that knit person job that net is where I get the latest and greatest versions of Java 14 so you have to have you have to have installed that and I've already done that and I've also said SDK used space D space Java space and then version name and that makes it the default so if I - Java version it consists of a 14 and Java version this is Java 14 ok so we have the current version of the tool chain this is just telling the build about it and I think we're ready to write some code actually that's why you need in the future you probably won't need this right you can just say version equals 14 and the parent pond direct you know in the public smell at the top there and it'll just work but in the meantime we've got what we need so what we're gonna do is build an application that reads and write data from an SQL data store so I've got I've got a Postgres running my local machine here and I'm gonna ask for all the data from the people did miss Pete okay there we are so delete from people good okay now I'm gonna build an application but it just works with JDBC and there's it'll give us a chance to look at a lot of new features in Java 14 and so on so yeah I'm really impressed in this language you know I'm a big continent plan I love Scala I love coffee I love groovy I love a lot of these more productive languages because they've always offered me a chance to do more but here with John 14 I thought wow this is actually a really felt very you know useful little language and gives me the ability to do interesting things so I'm gonna create a little bean that's gonna be loaded with the application when the application starts up okay and in this code all the I'll exercise the class we're gonna build which is a service ok and our service is going to manage people okay and the people is you know in this case I'm doing it kind of a like a an anemic domain right and so normally if I had Lombok I'd use at dad I have a logs constructor you know no args constructor and then I would create a class class person that kind of thing right and then I'd actually start defining the field so I create a field private integer ID private string name and all that just seems a little bit done it's just like all I really care about is hey I've got a thing it's a struct basically that holds an integer called ID and a string called name and everything else is immaterial well now in Java 14 there's a feature called a record and you might recognize this feature as case classes from Scala or do the classes from cotton and what the way this works is you just create a constructor and that's it that's the class when I use this class let's actually try it out here let's say person P new person here I'm given a constructor first of all I can provide an ID and a name okay and if I want to access that information I can just say that and I can say beat up name right and you can see it's already got that if I print it out watch this P okay so I've got this really nice you know being stopping and not a job of being exactly but it has everything I need right I can let's run it and see what it gives us okay oh I don't have a post ghost connection to me M specify that so spring data source URL JBC : post quiz QL local host orders okay and then we want the username orders and the password words alright let's try it again okay so they go you can see when I printed out the record it gave me a nice two string like it hasn't working equals and hashcode and all that stuff and all I had to do is just to say this class it even gave me a constructor right it has a constructor now of course I can provide my own constructor I can provide a compact constructor and here I can do things like you know do assertions I can say assert that you know is true ID it's greater than zero for example right the ID must be greater than zero so you could also it's a validation there you can even change the data whatever you maybe maybe you want to say this dot name equals name dot to uppercase so here it's gonna create a property it's gonna create a field for me called name here I'm setting the value explicitly I'm processing the the constructor argument and setting it on the local object right and notice that there's no parentheses here this is called a compact constructor and it you know it acts as though it's a constructor but unless we modify the syntax or the parameters from the constructor in the constructor like that I can also do a traditional one right like this which case I'm just doing the work for before the for this thing right you can do that if you want I quite like the compact style if I use one at all but this case I don't really need to okay so that's it that's the entire object there now let's create a service and our services spring beam that's going to work with JVC so I'm gonna use a JDBC template create a constructor okay and in my constructor I will I'm gonna do some work I'm gonna actually sorry in my service I'm going to do some work I'm gonna have a couple methods here one that returns an object by its ID so I'll say public person find my ID integer ID okay and in order to make this work I'm gonna need some sequel I'm gonna create a sequel statement so here I can use another nice feature in um in Java 14 and indeed Java 13 but that was a preview feature ID same same here so find by a sequel find by ID SQL equals and I can use multi-line strings how cool is this so again not controversial if you've ever used any other language on the JVM like calendar groovy or Scala they've had multi-line line strings for years ditto almost every other language out there it's just really cool that it's finally here for us so the people that's the constructor ID and name so I'm gonna say select all from people where ID equals question mark that's my query okay so here I can say this dot template dot query for object and I'll just do it this time fine by sequel I'll provide an object array okay and here I'm gonna wrote I'm gonna map the objects back into a type in order to do that I need a real mapper and this is another feature it's not new it's not particular to uh it's not particular to Java fourteen it's an old feature but it's just kind of nice right I can actually create a person role map and it's gonna be a lambda this is again if you've ever just using Java 7 this is gonna seem like witchcraft but it's just nice that we can do that okay so we want our s get int ID RS get string name alright so that's a lambda okay no that's it that's one method what about save what about this what about writing the data to the database so public person create I'm gonna create endpoint that will take a name and I want to have something else what if we have like an emotional state okay like a like a status or emotions or current feels right so I wanna have a an enum that current you know let's say emotional state okay and let's just say sad happy okay so I wanna be able to take one of those as well so that's an enum and I want to turn that into a persistent record so if you can see here I've got an integer for that it takes an index it takes a value 0 1 or negative 1 right let's say neutral let's say that okay so what I want to do is I want to say okay let's let's just decide what index to use what ID to use for the emotional state so emotional state index or whatever equals and I could do you know like this I could say it's some that and then if emotional state you know I can do comparison to basically but this is very tedious and also so is the int you know bars are nice I like I like bar so wouldn't it be nice if I could branch through the different states here right and say okay if you're this then you're that if you're that you know blah blah blah well you can now we have now we have switch cases which are expressions so here I can say a case sad negative one case happy one case neutral 0 okay and that's it I'm actually assigning the result of the switch expression to the variable and they're the variable is an int right but it's you know autoboxing for the window um do that so now I've got my index and I've got my data now when I write some data to the database okay so I need to create a sequel statement for that so private final string insert SQL equals look at that I love that the tools are cooperating right I'm using a modern version of IntelliJ and I'm using a modern version of Java and a modern version of spring boot it's just making everything a little bit nicer just a quite a bit nicer especially just stuck in Java 7 this will seem like a different language to you might as well go from Java 7 to cotton or Scala at this point because it you know it is so different but it's so nice I just I gotta tell you I'm just really pleased with them what they've done how they've integrated and by the way as I'm typing all this tip to you consider that I'm showing you the stuff that I started I type you know they're not every release of Java has new syntax features but every version of Java has new features and some of those features are performance improvements or security improvements I much prefer you know enjoy I have a lot more fun with this performance improvements but everybody needs security improvements right so I'm glad that they were there and I'm glad they're working on these features all the time because they make our run times faster and snorts felt and more compact and more efficient and so on by the way you can see the tool the IDE here right like you can see there's a little grey line right where my cursor is but above it in the text there's a green line it's telling me that if I were to print out this text right now it would have a bunch of tabs at the beginning of each line and actually it would print out each you know it print this out as a multi-line string right if I did this and then get rid of the extra space after that someday now it's multi-line right it's a multi line it's a string that's on multiple lines but it's actually with just one line right if I did that now it's a string that's on one line with a large amount of space it's basically you know from here you're gonna see that much space between the closing parenthesis of people name comma emotional state and the beginning of values so that there is actually like I left a margin there which i think is super cool and it for our purposes it doesn't really matter right its sequel so you know take as much space as you want I guess but it's just nice it we can finally do this no more concatenate strings together all kind of stuff okay so now insert sequel we're gonna use we're gonna do an insert which and JDBC is not fun but uh you know we'll get it done so let's see we are going to create a we're gonna call this dot template thought update okay and we're use the insert sequel and then in order to do that though we need to actually wrap it in an object that's gonna create a prepared statement for us and the reason we want to do this because we have to create a prepared statement that will execute an update that will return a key that we can then use so let's go back to here bar PSC F equals new prepared statement creator factory okay and then in the factory we're going to use this type insert in SQL and we're gonna provide some parameters okay now these parameters we're gonna we're gonna get from a list of parameters this is another thing that's new since Java 8 is these nice collection liberals right so here this dot of okay and I can just there's others for you know sets and maps and whatever but here I'm gonna say I want to create a new sequel parameter of type far car right so right now name and one critic it's gonna be called name and another sequel parameter of type integer okay emotional state good and so you can see that's actually a list I can actually extract it out it's a list of sequel parameters very convenient right we can see that or you know just bar it because it's it's more natural that way okay so now we have this and now we want to create a a key holder generated key holder so we're gonna create a key holder key holder equals newly generated key holder and the key holder we're gonna you know when we're done XK in the statement we're gonna ask for the key okay so so we do that we uh we pass all this into the template so this template update PSC you know PS CF the fact we're gonna we're gonna actually go ahead and turn that into an executed call so new prepared statement creator I'm gonna pass in the actual parameter values themselves so list of name and the emotional state or status I guess state there you go okay pass that in and then here we're gonna pass in the key holder and then down here we can get the data we can extract it now I'm gonna get back a key I'm gonna say th doctor kit key it's a number right so what I want to do you know what I actually want to do is I want to say if th instance of integer right which is what I'm hoping it is then I can return you know fine by id integer key H doesn't get key yikes look at all that redundant I've like I've had to confirm it's an integer and then down here Rhea Rhea firm or reconfirm that I want an integer that's annoying so now in in Java 14 just like in common and other languages there are smart casts so here I can say if it's an instance or instance of an integer give her a local instance name for the integers if it for the cast integer and now I can remove that ridiculous boilerplate right so there you go if that works you know so it's saying in this block it's already cast I don't have to cast again yet this is true otherwise I want to throw an exception okay okay so the um we couldn't create the person class start kidney okay so this is my my update oh did I put the wrong index I did I didn't I didn't put the right one index that's what it should be in there okay see integers that we derive by looking at the end different dies in the enum okay so now let's try it out let's see what we get we've got listener here I'm going to inject my service and down here this that people service dot create Liz this and we'll say emotional state sad okay let's see if that works good so there's that I'm gonna run the program what did I do wrong let's see always it always pays to make sure you follow through so well first of all I didn't get the integer I didn't define the integer for the record did I so int emotional state I've got to do that that rule mapper is no longer correct so I have to say yeah ørestad get integer emotional state okay good so there's this now what else do they do wrong I have I have my sequel statement I've got the parameters I've got the oh I have to set the um I have to configure the keys to return don't I so set return generated keys is true right it's true like that okay and then we've got generated key names ID so whoops I don't need that all okay there we go that's a little bit better I knew I was forgetting something that's not gonna do what I wanted to do otherwise cool how cool is that huh so we wrote the record to the database we were able to map it back into an object you know that would get a nice two string I didn't have to waste time with that no extra notations no nothing just straight Java this is just a straight java application yeah it's a little bit of a forced example I did JDBC maybe you would be using JP or spring data DBC but uh you know we could see smart search expressions we could see nice things like the collection the rules right the list of and mapped up of and all that stuff we've got to look at smart casting we got to look at multiline text strings we got to look at records and and you know they're used where's my record up here record right we got to look at var Auto boxing or auto in front I mean our Auto type in for it which I really love that's not a thing that's new and 14 but all these things taken together my friends they make for a really interesting Java that I just I could not have imagined being able to write even two years ago or especially not six years ago you know this remember javis twenty five years old now but you couldn't do them does until six years ago really right so for the for the very large majority of Java's lifetime it's just not passed right code like this and the fact that I can write it now it's awesome I mean Java is kind of an interesting language I think it's a it's been around for a long time and so it was you know the very beginning and it's the foundation on which a lot of other language have since patterned themselves but it's been very successful in its minimalism I think and it's become the most abused and without there and now it's it's okay for the language now that it's you know prolific and successful it's okay for it to start patterning itself off of other languages that have had smaller communities and we're able to try out different things and so this is I think a really interesting time to be using Java the the language already works we know it works people are happy so we can add a little syntax you know to make things more friendly right to be more competitive to be more accommodating and so I just really like the what the language team is doing and like I said there's a bunch of other features beyond syntax but what I know what I'm gonna deal with day to day my exit everyday life is syntax and I'm just very happy to see that we're not disappointed on that front um this is by no means a reflection of all the amazing new features in the language I've only begun to cover a few of them I just think it's Paul it's just nice that we can actually do this kind of stuff today so go ahead and get the get the bits try out the latest versions of spring boots the latest version of Java 14 and the latest version of your favorite IDE that supports Java 14 and have fun thanks so much for watching my friends and as always we'll see you next time you
Info
Channel: SpringDeveloper
Views: 30,661
Rating: undefined out of 5
Keywords: Web Development (Interest), spring, pivotal, Web Application (Industry) Web Application Framework (Software Genre), Java (Programming Language), Spring Framework, Software Developer (Project Role), Java (Software), Weblogic, IBM WebSphere Application Server (Software), IBM WebSphere (Software), WildFly (Software), JBoss (Venture Funded Company), cloud foundry, spring boot, spring cloud
Id: mr-7kGy8Yao
Channel Id: undefined
Length: 27min 53sec (1673 seconds)
Published: Wed Mar 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.