Spring Tips: Spring Plugin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi spring fans welcome to another installment of spring tips today we're going to take a look at the spring plug-in project which has as its core conceit an implementation of the strategy pattern basically you want to defer to runtime the decision about what algorithm or what implementation uh you use as opposed to making the decision upfront at compile time at design time uh and this you know this happens all the time it's so generic i i i think you probably wonder if i'm thinking of something else but no i'm actually talking about exactly what you think i'm thinking about right which is like a plug-in and an ide or uh different strategies for encoding files or you know whatever anytime you've ever had like some requests come in you want to look at the request and say okay based on this i'm going to use that well yeah this solves that this is the very simple plug-in approach that you should use and this thing has been in the spring ecosystem for i think more than a decade it's a very old project and it's something that we use in supporting projects but i don't really think people know about it and so i thought we'd do a little video here it is a very small library it does one thing very well and i'm super excited for you to see what it's doing i was actually reminded of it because i realized that the very popular spring fox project which is an open api implementation supporting swagger it's a community driven project i realize that they are in turn using the spring plug-in project for their spring integration which is super cool so let's dive right into it uh this is going to be a very quick one because by definition it's a very focused specific project and with spring boot there's not a lot of work to do anyway but nonetheless i think you'll get something out of it and hopefully this will be the thing you reach for next time you want to solve this generic thing that nobody needs to solve anymore enjoy all right let's build a new spring plugins based application i'm just going to call this plugins and i don't really need anything else i'll just use java 17 hit generate and go now unsurprisingly this is not on the spring initializer uh it's a very small project i'm not even sure if you're going to never need it but it's just nice to know that it exists so uh we'll add it manually to our maven bill of materials the dependencies are released of course you can find them out there they've been around for many many many many many years so ponder xml we'll go ahead and paste these dependencies so it's spring plug-in core and spring plug-in metadata for any packages org spring framework plug-in we're using 2.0 release this of course can be extracted out into a version property that's common across both of them so there we go spring plug-in version there's our property let's go ahead and re-import everything let's go to the application now this is an implementation of the strategy pattern basically you want to choose an algorithm at run time as opposed to design time and uh you know we call it spring plug-in because you can imagine using this for uh different plug-ins you know in your ide or something like that they have a common interface that they must comply with uh but really i think of it as like the strategy pattern right so let's go ahead and take a very simple look at it let's create a situation where we want to have something to write text for us right uh and how it writes text is kind of it changes we might have a different you know the user might select a string from a drop down menu a choice from a drop-down menu and that chooses how we then write the thing right so um i'm going to create an interface here called the writer plugin so interface writer plugin and it's going to have a very simple job void write string message okay now in order for it to work though in order for this to work i need to it needs to extend plug-in and the predicate that we're going to give it is a string now this is the context object this is the thing that we can use at runtime to decide whether this plug-in is able to whether this plug-in is able to handle the request and we do that by asking do you support t or s or whatever where in this case we've made s a string right it could be an enum it could be a type it could be whatever it could be it's like a content type it could be a lot of different things right so we've got that and let's write a simple example here that uses that and the spring plug-in api we're going to create an application runner that when it starts up we're going to inject a registry of type writer plugins to context uh string so plugins okay return event actually this could be args i'm going to say 4 bar format in let's say csv and txt plugins dot this one right so i'm going to say for that format give me that plugin get it and then i'll use it to write the message and the message you know whatever hello spring plugin okay so i'm using the plugin registry to dynamically ask it for candidates that can handle the format csv and that can handle the format txt okay so now we have to actually implement spring plug-in to make that happen okay so first things first we want to enable plug-in registries and we'll say that the type of registry that we want is a plug-in for a registry for writer plug-in and then we're going to implement some you know we're going to provide some implementations of this interface so class let's see csv writer implement writer plugin so it's a spring bean right mind you it's a spring bean here but we have to implement some methods here good and we'll say okay if s dot equals ignore case csv then we support it and if we do then we'll say uh you know whatever i don't it doesn't have to be anything writing message writing csv right so message whatever that's nothing particularly csv about it but you can see that this is be running because of the log and i'm gonna do the same thing for just plain text okay so uh text writer implements writer plugin okay and we're going to say system out writing text okay and s dot equals ignore case text so we've got csv txt we've got two different implementations we're going to ask for one of each let's go ahead and start the application and see what we get all right writing csv hello plugin writing uh text hello spring plugin all right so you can see it's it's a pretty trivial right and obviously if we only had one type let's just say for uh let's just say csv there you go that would work too so this is it's a little bit different than just injecting all the beans of type runner plug-in here we actually have the ability to dynamically select based on an advertised capability this is very convenient because now you can conditionally involve certain objects based on certain use cases and that's it my friends i there's a lot of other stuff relating to using spring plug-in and xml application context from 10 years ago or you know things like that but the you just saw the the basics of it that's more than most people are going to need and as always thanks for listening and we'll talk to you next time
Info
Channel: SpringDeveloper
Views: 493
Rating: 4.9230771 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: GlV5sXdXPu4
Channel Id: undefined
Length: 8min 7sec (487 seconds)
Published: Wed Oct 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.