Spring Tips: the road to Spring Boot 3: Spring Framework 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign fans welcome to another installment of spring tips in this installment we're going to take a very quick Whirlwind tour of some of the latest and greatest features in Spring framework 6 whose first release candidate just dropped basically it's feature complete we can talk about what's supposed to be in there things might get a little bit more refined there might be you know polishes and Bug fixes and this that and the rest but barring anything substantial that is what will get released uh when the final thing is released in about a month's time at the end of November 2022 and obviously spring Route 3 uh will build upon that and give us even more of the nice integration that we expect with all the ecosystem libraries there's a lot of stuff in spring framework six but a lot of it is also hidden behind the scenes it's foundational uh just an incredible amount of work required to get Java 17 going to get Jakarta ee compliant apis uh going in to get the aot grauvium foundational infrastructure in place for uh Native image support all that stuff is there now and it just works 99 of the time but you will need to be aware of some things and how they might impact your code in this video we're going to just take a brief walking tour of some of the features in Spring framework six and we're going to do so in terms of this springboot 3.0 snapshots obviously by the time you watch this it's very likely uh that there will be uh uh you know release candidate or indeed a finished version of spring boot 3 and you can expect this stuff will just get better in those uh succeeding releases as always my friends I'm really excited about this so let's just Dive Right In and look at the code okay we're going to start that spring Rayo I'm gonna build a new application using 3.0 snapshots we're going to call this the service we're going to bring in the Tomcat web support and the spring boot actuator uh and that's it we're going to use Java 17 obviously a big important part of this new release is that job 17 is a prerequisite it's the Baseline supported version of java there's a lot of things we could say about this but for now let's just dive into the code [Music] I'll enter presentation mode okay a few things to consider first of all our palm.xml is basically the same as it would be uh for any normal spring boot application um obviously here we're doing work against milestones and snapshots so we have those repositories in our build but those won't be required when spring boot 3 uh and spring framework six with it go ga uh later in November of 2022. so basically this is your build this is the build you can expect to have in production and it's trivial I'm just adding the web support and observability support and here we're just going to build a simple HTTP service we're going to have an entity called a greeting which will have a message okay and uh I'm going to create a greeting HTTP controller and we're gonna have an endpoint that returns a greeting when you ask for it so greetings name greeting okay we'll just call this greet path of variable for the path param there return new greeting hello right and then voila okay now there's our basic uh controller make a rest controller I want to be a little bit careful about the kind of data that we take in so let's say if uh string utils has text name or name.car at zero uh is uppercase you know then we want this to be like that uh so if it's Let's see we want a negative right so either if it has text and the character's uppercase right so if it's not that either one of those then we care so we'll say Throw new legal argument exception the name must start with a capital letter okay and uh now we've got ourselves a limit of error validation not too much but you know it's nice to have that and when I just want to extract that out into a single place where I can um you know handle the error handling in the application so of course if you know about spring Frameworks uh you know it's remember spring framework is geared towards not just small apps but also Enterprise skill apps right so there's lots of mechanisms and lots of ways to centralize and that the framework handle various aspects of the code here I'm going to just create a um problem detail uh error handling controller advice okay and I'm gonna be I'm gonna create an exception Handler and this exception Handler is going to be involved when there's an exception of type illegal argument exception and we can actually return a response so actually we could return a response entity of something in this case we're going to return a response entity of problem detail um or you could just return a regular body that might actually be just as convenient right so I can do that as well VAR PD equals problem detail and I'm going to say for status and detail right so HTTP status code foreign that value of 404 and uh you know the name is invalid there you go okay so we turn uh PD now of course you could change this set status set title set details and whatever um you could also return a response entity if you wanted to programmatically set things or you can use annotations like response status uh HTTP status um bad requests or something like that right you could do all that kind of stuff but either way either way the important thing here is that we're centralizing all their error handling and we're using a new art a new newly supported uh thing in the spring ecosystem uh called Problem detail this is new in Spring frame X6 this problem detail satisfies an RFC there's actually an RFC uh 7807 that is intended to provide a consistent way to represent errors and that's Now supported out of the box so you can of course return this problem detail from your method directly but you know it's good practice to centralize your exception handling logic and in an example and so this controller device would would apply to all controllers in the application had I copied and pasted this or you know move this method to the controller itself it would have replied to all methods in that controller so good now I've got a consistent way to represent errors I might want to also uh you know maybe I don't care about the type I'm not going to inject the reference I don't care about it in which case I could just define a class literal there but I do care about the current request and so let's take up look at that I've got an hdb servlet request right request and I have attributes I can say request dot get attribute names dot as iterator for each and you know I'll just log out the attribute names because I want to make sure I understand what's happening when the application is running right so attribute name and we'll say attribute name okay yeah that's that's the one so it's actually just rename name okay good so there we go there's my simple error handling logic I'm just printing out the attributes if there's an error um and I'm doing that in terms of this HTTP servlet request interface right well of course this is in this is a serverless spec interface I don't have to use this spring actually has higher entity higher order uh types that you can inject uh and and stay indirect you know indirectly above either Neti in the reactive world or uh imperative i o in the servo world but the point is I'm choosing to inject that here just so we can make a point this type comes from Jakarta that's servlet.http that Jakarta bit is new you might be familiar with the uh you know javox.servlet.htp API of your but of course a long time ago in a galaxy far far away Oracle goes to the Java ee community and that resulted in some outcry and then finally uh after what three quarters of a year or something like that Oracle came back to the table and they open sourced uh the Java e specifications so that they so that the apis and the specs could evolve there's a little bit of a poison pill however in that they stipulated that all changes to these apis had to happen in new packages so basically the only reasonable pragmatic thing to do was to move everything to a new package and then start evolving on top of that so the um you know from a technical perspective uh the eclipse foundation and the people working on the Jakarta ee apis have done everything anybody could reasonably hope they could have done to make this transition as painless as possible it's just a political perspective that makes it so painful right but that said it could and more than likely is just a matter of find and replace at least for your code we've spent the better part of the last several years uh you know wrangling all these different ecosystem libraries and bringing them in uh to Spring Boot and re-enabling parts of the uh of the project that you know temporary didn't work as we started to rationalize and sanitize all these different uh ecosystem libraries across the portfolio so supporting libraries things like hibernate things like tomcat and Jetty and all that they've also been updated and so you'll you'll see that you know even if we wanted to stay back we really couldn't unless we wanted to risk being stuck on older apis and obviously we don't want to stay back we want to use the latest and greatest always so there's our controller there's our uh controller advice we're using the new Jakarta apis we've got Java uh you know what is a 16 or 15 records right we got that here as well and that's because it's supported right now you know spring framework itself is using Java 17 now as well we've got problem details this is a lot of stuff and yet hopefully it's still very very familiar right you should know exactly what you're looking at now I want to observe this application right in order to observe it I need the spring boot actuator the actuator gives me uh instrumentation that I can use uh to understand the state of the application so of course just for our demo I'm going to include all the actuator endpoints I'll show the details for the health um and I also want to enable problem details I forgot to do that earlier so make sure you do that when you once use problem details with spring boot today you know that should give me everything that I want um but now that I've got the the actuator uh enabled I want to take advantage of the ability to instrument it and I'm going to do it using the new micrometer releases you see another huge theme another major shift in Spring framework 6 is that we've reworked micrometer so that it now supports both tracing and metrics in fact it supports it through a unified API called an observation this API is awesome it's clean delegate and it's uh you know for a lot of use cases it'll just be invisible to you you don't even have to know about it but it is there if you want to to benefit from it to use it so for example here I'm returning a greeting obviously this is just an in-memory thing where I'm creating a reference um you know on the spot so that's not really any latency or any risk there but let's suppose that I was doing some sort of database logic or something like that here I could um intimate one of two ways one is I could say at observed this is i o micrometer observation annotation and uh that's fine but I quite like this new static Builder so observation officer Invasion crate not started and then you give it a name so here I'm going to say greetings uh you know name okay and then you return then you pass in a micrometer observation registry so uh officer Invasion registry inject that into a Constructor there you go okay this dot registry and then we get to observe something so here I'm just going to observe the production of creating that creating object right good so there we are there's our application we're gonna replace that with a Lambda and there we are good stuff so um we've got our uh you know observability built in we've got this new nice API that consolidates both tracing and metrics so you can talk to back ends like a Zipkin and so on uh for for tracing and uh you know you can talk to different uh time series databases things like datadog and uh grafana and so on to visualize all that all with the same standard unified API and the nice part is that since this API now sits squarely as a dependency of uh Spring right as opposed to sitting on top of spring boot in the spring Cloud space because all that lives now at this lower level every Library uh that that depends on Spring can also in turn depend on this right uh and ditto a third-party libraries that instrument using the this new observation API they get the benefit of of being able to communicate Trace information as well as metrics um in a single standardized way so this is very convenient also we have a bridge so if you're using open Telemetry that'll also work right so we have one low level API that uh that everything including spring framing itself for all the common life cycle events we'll use let's go ahead and restart this okay it's up and running uh and let's go to the console here curl HTTP localhost 8080 forward slash greetings and we'll just say Gene okay capital J good what about lowercase j oh it's cached so um what about lowercase Bob Hello is my condition bad when in doubt you know good make it more clear right so I'm going to tease this out if it's an uppercase so if it if it has text and and okay good so if it's not good throw that exception good let's try this again rookie mistake this is why we test in production uh okay so terminal lower case I get that uh problem details showing me the type and by the way this can this is usually a URL explaining the problem I get a title I get the status code I get the name is invalid I get the URI where it happened uh Etc good stuff right um and if I go here and I do uh Jane capital I get uh the the message as well right so that works out just fine as well and just so I can understand yeah that should have failed good stuff right um so I can also then see that reflected in the actuator metrics so actuator for slash metrics and uh JQ and you can see things like you know Tomcat sessions created all that stuff because it's now being instrumented in a low-level way uh we have greetings.name and how many active requests we've got for that as well um so I can say okay give me uh metrics like this it shows okay there's any are there any errors none there's the tags I get statistics like the count the total time Etc all right so I've got a service I want to turn this into a gravyum native image that is just built in right of the box if you've ever used bring native then you know that in order for that to work you would have had to add a number of dependencies to the build but it's no longer required it's all just part of spring framework six another huge substantial change in the foundational layers of spring framework itself so vibin minus P native minus D skip tests clean package okay so we're just gonna build this and it'll take about a minute so we'll let it run [Music] all right there it is so let's go to the Target directory and uh there's our application you can see it is 72 Megs that's the entire application JRE application code Tomcat all that stuff it's in there this is an architecture and operating system specific binary uh it is created by using gravity on the native image compilation mechanism the benefits of this are twofold first yes your application starts up quickly who cares oh there's a port running um run that okay don't care about that I'm with my terminal uh okay there it is so it started up in uh 71 thousandths of a second but much more usefully um if I go back to my terminal here and I look at the existing instance what's the process identifier the pit is one six one five one let's go to the other terminal and we can run my little script here Josh pin RSS by PID this script does PS minus o and then it captures the RSS the resonant set size of the process itself as it's running including all the operating systems libraries right so let's just paste that and it's taking 91 Megs of ram to do its work so you put that in a Docker container uh you know very thin one because you don't need a full operating system anymore um and you've got yourself an application that'll run with very little Ram 91 Megs of ram right that's a less than a tenth of what most applications will take on the jvm these days a large part owing to the fact that it's just running on the jvm right just the jvm itself takes a lot of ram so this means you can now deploy you know whatever anecdotally roughly for a lot of scenarios but certainly not all of them maybe 10 of these for every one that you would you Deploy on the JRE then there are some drawbacks there's no such thing as a free lunch obviously the gravian native image compilation it uh you know you don't have as many transactions per second you won't have as fast a garbage collector and so therefore that'll negatively uh and adversely impact your transactions per second request per second Etc but but again you can scale out and more than compensate right uh and still not exceed the footprint of what you were doing before um it also has a uh no support at the moment for Java agents so if you want to do instrumentation well of course it's a good thing that that's now even more readily integrated into your application using spring Boot and actuator and micrometer and the like um but uh yeah it's a pretty good deal people this is good stuff now I've got a service let's go build a client very quickly we'll go back here we're going to build a client um and this time we're going to use the reactive web support as well uh let's get rid of the web support I'm going to use spring boot um do I need actuator no I don't let's just do that and we'll hit generate foreign Y at client application and all I'm trying to do is to use yet another feature in Spring framework 6 uh namely declarative interface clients so uh greetings client and I'm going to create a record here called greeting string message and I'm going to create a method that returns a greeting given a string name so this is going to be a client that talks to our API the one that we just finished building but that automatically um does so without our sort of having to write all that low-level uh risk template or web client code I can just use this interface I can say get Exchange and the endpoint is greetings name okay I can use these new annotations like git exchange and post exchange Etc to describe the client uh you know characteristics but certain annotations like path variable can be shared across both uh you know git mapping for the service side and get exchange for the client side and now I need to turn this interface into an actual beam that I can use so here I need to create an HTTP service proxy Factory like so so I'll say web client adapter dot create HTTP service Factory for a web client I'll say webclient.builder of our WC Builder base URL HTTP localhost 8080 okay so there's my Builder and WC good okay there's my uh web plan HTTP service proxy Factory now I need to actually build a implementation of that interface so I'll say create a bean here CRM uh sorry I'll create a bean here of type greeting client and I'll use that HTTP service proxy Factory okay create client greetings client dot class voila and then finally I'm going to create a bean of type application listener application ready event ready turn no application listener and we're going to inject the greens client gec dot greet and I will say spring fans bar respond and look at that that's another nice feature that you get with javas17 is VAR and I'm just going to print out the result of that so response good it's a greeting type that we get back typed for us I'm gonna replace that with a Lambda why not okay good so now I want to run this let's just run it on the JRE and see if that that works as we expected to okay it seems like it's worked um now I want to then turn this into a gravium native image now there's a little bit of a of a problem here you see I'm creating a jdk proxy behind the scenes when I use this and that's going to create some issues for us um we need to actually tell grauvium about all the use cases all the scenarios where we do things that run a foul of its very very part you know particular Infinity restrictions on runtime flexibility things like uh class loading you know class.4 name and reflection things like serialization things like jni things like loading resources from jars and you know property files and icons and the like things like uh you know proxies all that stuff needs to be accounted for in configuration files now obviously you see that even though spring does a lot of that stuff it just worked right before so for the very large part most things are going to just work just fine that said in certain cases where spring can't know what you're doing you need to be very explicit and tell it so that it can provide add that configuration to the grav VM native image compiler so for example here we have a greetings client that greetings client is going to be turned into a you know a proxy and then it's going to make an HTTP request and give us a return value of type greeting so we need to register hints these are hints these are what we call the the configuration entries that get fed into the gravia native image compiler right we're going to register a hint here by using the register for reflective binding greeting dot class okay so that's one part of our problem done the other thing is that we need to tell Graham about the fact that we're creating a jdk proxy behind the scenes right so that proxy is going to require three different types uh represents not just our interface greetings client but three others at the spring proxy Factory that the spring proxy Factory will register so let's create a class called greeting client runtime hints registrar and they'll implement runtime hints registrar and we have to override this method here and uh we'll say hints Dot proxies Dot registered jdk proxy and we're going to feed it in in very particular order by the way here I already happen to know what it's going to require but if you run if you try and compile the application without these hints already it'll complain but it'll print out on the console uh which interfaces you know need to be accounted for it or printed them out in the order in which they need to be accounted for as well so you can just copy and paste that and you'll arrive at the exactly the same thing as I've just done um so now we've got this registry hints there's a uh greetings client runtime red hinge registry that we need to then uh tell spring to use so voila okay all right so now we've got our custom hints we've got our proxy uh I guess I wanna I wanna then use the screenings client um in a gravim context so let's go ahead and compile it this will take about a minute all right the application is finished compiling let's go check it out client and there we are there's our result just as we expected it um it worked it's got It's given us a response uh to our query and we can actually see that reflected in the console there all right my friends we've just taken a very brief quick abbreviated look at a number of new features problem details the aot subsystem the Jakarta ee API upgrades the Java 17 Baseline uh and of course the declarative clients now obviously we've just begun to scratch the surface here we could and probably will do videos detailing things like uh you know using other time series databases and chasing with the new the new uh observability support we'll probably do videos we definitely won't do videos looking at the new aot subsystem because believe me we've only just begun to sketch the variativity top of the surface you don't know anything yet my friends it's crazy uh how impressive and how robust this new subsystem is uh We've we could and probably should look at uh things like using other declarative clients uh new other declarative interfaces uh uh for other protocols in particular for our socket because remember this is generic right conceptually we could also do this for uh our you know graphql for example there's a lot of interesting implications here what we have out of the box by the way is meant to supersede the support in Spring Cloud uh in Spring Cloud Square uh retrofit and spring Cloud open Fame and even that experimental project I created around our socket support all that is meant to be superseded by these new declarative types so there's a lot of interesting implications here and of course we get new apis hibernate the latest and greatest hibernate latest and greatest all tomcan Jetty and all these things are now accessible as part of spring boot uh infrastructure so uh you know a lot to look at but uh We've we've got a lot of time and I wanted you to be able to try these bits out and see kind of uh what spring framework itself has done for you I've consumed everything thus far in terms of spring boot 3 which in turn does a fair bit more in their new upgrades but this is a uh you know a lot of this is superficial stuff that will be very interesting on the face of what you're doing but a lot of it is also just back-end uh you know subsystem tuning for you wouldn't believe the amount of work like I say it you wouldn't believe the work we had to do to make Jakarta EE and uh Java 17 and all and all these other things and aot all these new paradigms all them uh work well and consistently across the uh the uh the project in the portfolio obviously we hope you enjoy it and get something out of it as well as always my friends we'll talk to you next time
Info
Channel: SpringDeveloper
Views: 41,463
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: aUm5WZjh8RA
Channel Id: undefined
Length: 28min 36sec (1716 seconds)
Published: Wed Oct 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.