How to create your first Spring Application (without Spring Boot)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back friends dan vega here and today we're going to talk about building your first spring application but there's a little catch today we're not going to use spring boot now why would we do that stick around and i'll tell you all about it right after this [Music] all right so as i said in the intro today we're going to be building a spring application but we're not going to use spring boot and there are a couple reasons behind this first of all spring boot does a lot for you and if you're not really sure of how spring works then some of it can be a little bit magic and so what i want to dive into is just build a simple spring application we'll start by using a maven project you can use any ide you like i'll be using intellij today but what we'll do is we'll start from a simple maven project add the dependencies that we need and that will really allow us to focus in on the basics of the spring application or the spring framework and so we'll look at things like dependency injection and configuration and i think it'll give you an appreciation for uh just how spring applications are built and then once you get into spring boot applications you'll start to notice some of these things that spring has done for you i mean one of the biggest things that we're not going to get into today is if you try to build an mvc application there are a lot of things and configurations and plumbing that goes into that and spring boot really just gives us a lot of this out of the box so that's why i wanted to do this i've gotten a lot of questions regarding this so it should be a fun one and what are we waiting for let's go ahead and just jump right in and start building this app all right so the first step to creating your very first spring application is to create the project to do so i'm going to be using intellij's ultimate edition today but you do not need this version to go ahead and follow along if you want to grab the community edition that will work just as fine you can also use something like vs code or eclipse or netbeans whatever your favorite ide is ultimately what we are doing today is we're creating a new maven project from there we'll add some dependencies and we'll write some code so here in intellij i'm going to click on new project and we're going to bring this up and what we want to do again is we are creating a new maven project so there is a spring initializer in here and that is for creating a spring boot application if you're interested in that i have a ton of tutorials on spring boot i'll go ahead and link a playlist up above and down below in the description but this is just getting started with maven or getting started with spring using the maven new project so we are not going to create an archetype you can go ahead and choose whatever project sdk you like i'm going to use java 16. if you are going to use java 16 make sure you're up to date on your version of intellij if not any other versions fine 11 8 doesn't really matter for this specific project so i'm going to click next and what we're going to do is we're going to throw something here in my spring folder we'll call this hello spring and here in the artifact coordinates i'm going to go ahead and set up dev.denvega we'll call this hello spring and i'm going to just make sure that directory is empty okay now it should be let's go ahead and click finish and this is going to open up our project i'm going to go ahead and maximize this okay so everything's kind of done this will start up real fast because there really isn't much to this application right now so the first thing that we want to do is we need to add any dependencies so if we look in main there's just a java and a resources folder we look in tests there's a java folder this is what you get this is kind of the plumbing out of the box when you create a maven project and the maven project also gives us this palm.xml where we get to kind of declare some of the properties and dependencies about our project so you'll see some properties here right away we've just stated that we're using java 16 here so that's that was part of the the kind of wizard and it wrote that for us again next what we want to do is we want to define what dependencies we need so we need some some outside code to go ahead and start our project and to do that we're just going to create a dependencies block and you see as i start typing i get some code assist here which is really nice so then in dependencies i'm going to add a new dependency so the first dependency that we need is spring context so again we're getting that assist here just one of the many many reasons i really love intellij we kind of get that help there so if i hit enter i will go ahead and suggest some groups that is in fact org.spring framework and the latest version is 5.3.5 now if you're brand new to maven don't worry about it that is really the gist of what you need to know for today's tutorial but if we go ahead and click save you see this little circle up here um basically says hey i've noticed you've changed the palm.xml you want to go ahead and load those changes and we do so i'm going to click that and if i pull over this maven dialog here again if you're using another id don't worry about this i'm just kind of doing this to explain something but our dependency that we hit that we put in here was spring context so what it will show you below is that there are some transitive dependencies on spring context now in order to have that we need spring aop we need spring beans we need spring core and we need spring expression so by declaring that one dependency we've kind of brought all these other dependencies with us and we can double check that here if we look under external libraries you'll see all the libraries that we have brought into our project so that's really all we need to get going uh now we can go ahead and start writing some code all right so first thing i'm going to do is go ahead and close this down under java we're going to create a default package here so we're going to say new package this is going to be dev.dan vega inside of there i'm going to create a new class and call it application so in application we need to set up set up a public static void main and let's just go ahead and print hello spring and what we're doing here is we just want to make sure application runs everything works without error before we go ahead and add any kind of business logic to our application so there we go we have hello spring if you didn't see what i did there there's a little play button next to that method or the class you can click on that to run it you can also right click and go to run application you can also come up here to run and run application and you see the keyboard shortcut for your os there so that's kind of the plumbing now what we need to do is start talking about how we're going to build this system out so this system that we're going to build is going to be based on courses i've used this kind of domain before but what if i was building a course platform where i wanted to be able to sell courses so at the very minimum the first thing i'm going to need is a model package and what we're going to do inside of here is create a course.java and this course is going to represent a single course in our system what are the different properties of a course so i'm going to come in here and say that we may have an integer course id we may have a title we may have a description we may have a link to the course so in a real world scenario there's going to be a lot more involved in this but you can start with this you don't need to build out your entire model to begin with i really like to start mvp minimum viable product and and work from there so we'll start with this i'm going to create a few more things i'm going to create a constructor that takes all of these in his arguments i'm also going to create getters and setters for each of these and finally i'm going to create a tostring and that is all we need for course so now that we have a model we need to come up with a system where we can store these models right and this would usually belong in some type of service class so what i want to do is actually create a new say package and we'll call this service and inside the service class i'm going to create an interface called crud service so anytime i create a service that is basically used for crud create read update and delete i'm going to have an interface for this and this interface is going to be based on a type of t so that means uh when we create a new um course service it'll basically implement this crud service and that type of t is now the course if we add something later on like a user that will be a user type and this just allows us to avoid duplication of all these different you know instead of creating say a course crud service interface and a user crud service interface this will kind of clean that up so the first thing we want is probably a list of that type we'll call that list we also want to return that type when we create one we might want an optional of that type called git which will take an integer and this is assuming every type in our application is of type integer but for this case it'll work fine we want to update t t and id and we want to delete into id so that's really it and what we're going to do now is just create a java class here called course service which we are going to implement service of course and we're going to go ahead and implement those methods to satisfy that contract all right so now what we need is when we call this list we need a way to return a list of courses so what i'm going to do is create an instance variable here that is a list of courses we'll call this courses and maybe in the constructor i'll just go ahead and set these so eventually we're going to move this into a repository class and ultimately you know this would be something that pulls from the database but right now what i just want to do is say um courses is equal to new array list and i should spell that right and what we're going to do is we'll just create a new course so let's say a course spring boot is equal to new course say getting started with spring boot 2 which is a course of mine how to build real applications with spring and the link will be there okay so now what we can do one thing i like about intellij here is i can come in here and get this little help and say i like to put these on separate lines just to clean that up so now with a single course ready i can say courses dot ad spring boot and that should be it so now when this crowd service starts we have this list of courses we create one on the constructor and guess what we're going to go ahead and return this so again we're going to kind of add on to this application but i want to start small just to kind of walk through each step so now back in our application what if we wanted to go ahead and print out all of those courses well what we could do is we can create a new course service and we can say all right service please go ahead and get all of those instances for us so when we create this new core service that one will get created and then what we can do is just say service dot list and this should go ahead and print out that one course to the console so it did so in a very simplistic world this will work fine now when we start building real world applications though anytime you kind of see yourself newing up instances of a class especially in the spring world this is a no-no this is because we want spring to manage those instances for us and there are a lot of reasons behind this one of which you don't want to have that tight coupling between classes so spring is going gonna help us with that because we aren't any time that we have to change it then we don't have to go ahead and if that constructor were to change you know we'd have to go everywhere in our application and change where we've kind of created a new instance of it especially if we overrode that and didn't have a default the default noir constructor anymore another thing that it's going to help us with is when we get into especially on the website when we are creating dependencies in our controllers you know those are are basically uh called every time a request happens so in that case we'd be creating many many instances of a single class that doesn't need instances so it's going to help us there as well all right so then the question is if you aren't going to just new up a new instance of the course service how do you get an instance of it and this is where configuration comes in and so the first thing that we're going to do is we need to tell spring about the classes that we want spring to manage for us and so there are several ways to do this if you've kind of looked around online and seen old examples this used to be xml based you can do this through a java configuration these days you can also as you as we'll see later in this tutorial skip configuration all together by using annotations but let's start with the config approach and what we're going to do is create a new package here called config and in here we're going to go ahead and create something called app config again this could be called whatever you want it to be so in here the important part of this is we're going to mark this with an at configuration annotation and so if we click into this and let's pull that down and take a look at just exactly what is going on here this indicates that a class declares one or more at bean methods and may be processed by the spring container to generate bean definitions and service requests for those beans at run time so here's an example so when you say app bean so bean is really a class that's the spring container is going to manage for us so when you mark that with that bean spring is going to know hey i have this thing called my bean and whenever you ask for one i can go ahead and give you one and so that's what we want to do here with our service so we're going to say at bean we're going to say public course service we'll say get course service is the name and all we're going to do is return new course service and that's it so now you're probably thinking well what's the difference with that dan you just said not to use new core service well i did but i said don't tie yourself to that instantiation over in our main application class you have to do that somewhere but here we're only doing it in one place so if anything changes there with that contract you only have to update it in one place so now back in our application instead of saying new core service we're going to do a couple different things so let's get rid of this the first thing that we need to do is we need to set up that what's called an application context and it's really um actually we'll dive into that let's say application context now that is an interface and the class that we're going to use for this case is the annotation config application context and we're going to point to our appconfig.class um so that is probably going to need whoops um okay so let's talk about this for a second so again this is the interface uh this is an interface the central interface to provide configuration for an application this is the [Music] actual class that we're going to use to work with this is the implementation so standalone application contact accepting component classes as input um so we'll see more more of how this works in a second so now that we have that instance what we can do is we can ask for a reference to that core service because spring now has an idea of where your configuration is right and it's going to say all right i'm going to go ahead and create a type course service bean for you so we can just go ahead and ask for it now so we can say we want to something of type course service we'll call it service but now instead of new we're just going to say hey application context go ahead and get me a bean the name is going to be course service and the type is going to be courseservice.class so now we should be able to just go ahead and say service dot list just like we did before let's go ahead and run this and could not find a being named core service so all we need to do over here is actually give it a name so we're going to say course service and that's how we refer to that specific bean we refer to it as a course service so back in our application that is the name that we are using to reference that bean so if we go ahead and run it again we see our one course there all right so i'm going to go ahead and improve on this application a little bit here in our car service we have our instance variable for our list of courses and the constructor we're creating just a single course now in the real world we don't want to do this type of work in a service this is going to be delegated to something like a dao or a repository um so what we're going to do is we're going to create a new package here and we're going to call this repository and in this repository i am going to create an interface called crud repository and in here we are going to create a single list of p we need that so similar to what we did with the service our repository is going to have one method called find all um next what we want to do is create a new course repository pository and that is going to implement the crud repository for course again we're going to implement these methods and so far nothing's really changed the next thing that i want to do is over in a core service i want to kind of get rid of the logic inside of here so let's get rid of this let's get rid of this now this is going to throw an error because we no longer have courses in here and what i'll do is inside of our find all method here i'll kind of just replace that logic so this allows us to again we're just using an in-memory representation of a list and we're adding one course to it and we're returning that now we would probably make that an instance variable just like we did in the service class before but again this is just a demo so i want to try and keep this as simple as possible so here we're going to return that so now what i need to do is i need that course repository here in my service so i might say something like course repository we'll just call this a repository again i need an instance of this so i might say repository is equal to new course repository and now i can use this to go ahead and find all so now back in my application if i run this everything should work as it did before but of course you probably already see the problem here is in our service we are going ahead and we're creating a new course repository and we don't want to do this we want spring to manage this class for us so how can we fix that well we can go back over to our app config and what we need to do is create a new bean so let's say at bean and this is going to be called course repository i don't know why i always misspell that we're going to say public course repository is going to be get course repository return new course repository okay so this is going to work but now if we look in our course service we don't want to new this up right what we want is we want this constructor to accept an argument of type course repository and then that way we would just set this dot repository equal to course repository and actually the name isn't the same so that should work so now this is what we want but we do have one related problem here and that is back here when we create our new core service we need to get an instance of the course repository and to do that all we need to do is say get course repository and there we go so now that should satisfy this argument and of course if we go ahead and run this everything should work as we expect okay so application's looking pretty good right now but i just want to show you one final way you can start to clean some of this up and again this is going to start to make more sense especially if you get into spring boot work of what is actually happening behind the scenes and that's that's why i really like to kind of show this stuff um not that i think you would write applications this way from scratch but really just to understand spring and why it's important to you you know what it's useful for and and what is kind of happening behind the scenes when things start to get a little more complicated so back in our app config what i'm going to do is i'm going to just comment these out as if they don't exist so now there's going to be some problems in our application because in our application when we try to go get a course service that is not going to exist and there is no course repository either so we're going to have some problems so you don't need to manually create all of these beads unless you have a reason to if you have some like extra configuration or logic that you want to add in here there's not a lot of reason to do this so we can go ahead and simplify this if we go over to course service what we want to do is i'm just going to clean up these imports so we can add a special annotation to these classes the biggest one is at component and there are specialized versions of this annotation for things like services and repositories so we're going to add at service and if we look at this we can say this is really just a an at component underneath the hood and so an ad component indicates that this class is a component such classes are considered as candidates for auto detection when using annotation based configuration and class path scanning so this is going to be important so now in our repository we're going to add one more called a repository so again that is a specialized component they're all at component annotations so that is going to add this to the application context for us so now in our app config all we have to do is say we need to tell our configuration where to look for our classes so we're going to say at component scan and i'm going to say dev.dan vega is the main package that i want you to look in so now it's going to search down that package for classes that are marked with either a component at service at controller at rest control or at repository it's going to look for these components and add them to the application context for us so now if we come back over here and we go ahead and run this we should see that it works just fine and that cleans up our code a lot so i think that is the extent of it that's really what i wanted to show you today and again this is going to help you understand dependency injection how this you know why the spring framework is so useful but more importantly that when you spin up a new spring boot application there's a lot going on there it adds so much it makes things so much easier for us but lost in that shuffle could be you know what what the foundation is what is actually happening underneath the hood and this is really the basics the basis for that whole framework you know being able to manage dependencies like this and so much more but um just thought i'd show you this i hope to hope to kind of shed some light on on what's happening there in the spring framework so i think that is where we'll end today's tutorial i hope you enjoyed it if you found value in this please go ahead and subscribe to the channel give me a thumbs up and as always friends happy coding [Music]
Info
Channel: Dan Vega
Views: 19,607
Rating: undefined out of 5
Keywords: application.properties spring boot, how spring boot works internally, java spring, spring, spring aop, spring application, spring boot, spring boot application, spring boot project, spring boot test, spring boot tutorial, spring boot web application, spring core, spring data jpa, spring framework, spring mvc, spring project, spring security tutorial, springboot, springboot project, spring framework tutorial, java
Id: e8aSyQo0nHo
Channel Id: undefined
Length: 30min 59sec (1859 seconds)
Published: Thu Apr 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.