Annotations Overview - Component Scanning
A free video tutorial from Chad Darby
Popular Java Spring Instructor - Best Seller
4.6 instructor rating • 8 courses • 452,352 students
Learn more from the full courseSpring & Hibernate for Beginners (includes Spring Boot)
Spring 5: Learn Spring 5 Core, AOP, Spring MVC, Spring Security, Spring REST, Spring Boot 2, Thymeleaf, JPA & Hibernate
41:00:15 of on-demand video • Updated July 2021
- Develop a REAL-TIME project with Spring MVC, Spring REST, Spring Boot and Hibernate CRUD ... all from SCRATCH
- You will TYPE IN EVERY LINE of code with me in the videos. I EXPLAIN every line of code to help you learn!
- LEARN key Spring 5 features: Core, Annotations, Java Config, AOP, Spring MVC, Hibernate and Maven
- I am a RESPONSIVE INSTRUCTOR ... post your questions and I will RESPOND in 24 hours.
- POPULAR VIDEOS for: Spring Boot 2, Spring Security, Spring REST, Spring Data JPA, Spring Data REST and Thymeleaf
- Join an ACTIVE COMMUNITY of 185,000+ students that are already enrolled! Over 47,000+ Reviews - 5 STARS
- Students have LANDED NEW JOBS with the skills from this course. Spring and Hibernate developers are in HIGH-DEMAND!
- You can DOWNLOAD all videos, source code and PDFs. Perfect for offline LEARNING and REVIEW.
English Teacher: Hey in this video, we're gonna learn how to configure Spring with Java annotations. So what exactly are Java annotations? Well all they are, they're simply special labels or markers that are added to Java classes and they actually give you meta-data about the class. So like I have here in this photo of a shoebox, we have meta-data or a label on this shoebox. So we have the actual size of shoe, the style of shoe, the model number, and so on. So that's meta-data about this shoe. So, again, Java annotations are simply meta-data about a class. So we can actually have annotations that are processed at compile time or at run-time, and we'll see both examples in this video. So we've actually seem some annotations already. So when we created that TrackCoach, we had an annotation called at override. So what this annotation does is it basically tells the compiler, hey, we're gonna implement a given interface, or extend a class, and we're gonna override the method. So when you give that annotation of at override, you're basically telling the compiler, hey, we're compliant, we're gonna override the method exactly as listed in the interface or the parent class. Now, what happens at compilation time is that the compiler will check your class and make sure that you really are overriding the method. So they will verify or perform an audit check on your class. As long as you override it exactly as advertised, then everything works out just fine. However, if there are any problems, then the compiler will say, hey you're not really overriding the method as you stated that you were, and it'll actually give you a compilation error. Alright? So again, at override's an annotation that's actually processed at compilation time, and we've seen this already in our coding examples so far. Alright, so now, why would you wanna use spring configuration with annotations? Well, if you were doing normal XML configuration, it can be very verbose for very large projects. Imagine a scenario where you had a spring project and you had 30 beans or maybe 100 beans, you'd have to list each one of those in your XML config file, and that would just take a lot of time and be very verbose, or a lot of work. Instead what you can do is you can actually configure your Spring beans with annotations. So the annotations will basically allow you to minimize the XML configuration. Again, annotations are like meta-data for your class, just like the photo here. We basically have boxes or moving boxes and then we annotate those boxes or we basically write the name of the destination of those boxes on the actual box itself. So a similar thing with annotations, we'll add an annotation to a given class and then Spring can use that for configuring the system. Alright, so let's kind of look at the background here and see how this works. Well basically once you add an annotation to a class, then Spring will actually scan your Java classes for those annotations. When it finds a class that has a special Spring annotation on it, it'll automatically register that bean with the Spring container. So instead of doing everything long hand via XML config file, Spring will just scan and say, oh that's a Spring bean, let me grab that bean and let me register it with the Spring container. So Spring will kinda help you out here and do a lot of work for you in the background by scanning your classes. Alright, so let's go ahead and look at the development process for using annotations with Spring. And again, I love doing things step by step. So then the first step is that we're gonna enable component scanning in our Spring configuration file. And then in step two, we're gonna add the component annotation to our Java class. And finally in step three, we're gonna retrieve the bean from the Spring container. And we'll go through this in the following slides step by step. Alright, so step one, enable component scanning in the Spring config file. So our Spring config file, well that should be really simple now. So instead of listing out all the beans, we can remove all of that stuff and simply have one entry here. We'll simply say, context component scan, and you give it the base package that you want it to scan. And so Spring will actually go through and scan all classes in this package and all subpackages and it'll identify the components that have that annotation on it and it'll automatically register them with the Spring container. That happens in the background for you automatically. So then with step two, we need to add the component annotation to our Java class. So this is an annotation, we're gonna tell Spring, hey when you scan, this class is a special Spring bean, so I'd like for you to register it. So here, we make use of the at component annotation. And here, this is for our tennis coach, and we simply give the actual bean ID that we wanted to use. So at component, that Silly Coach, that'll actually register this tennis coach as a Spring bean and it'll have the bean ID of that Silly Coach. And again, the bean ID can be anything. You can make it whatever you like, I'm just keeping it silly here in this example. So that's it. So Spring will scan, it'll find this at component annotation, automatically register it with that given bean ID. And this is really, really cool, I like this. And then finally step three, retrieving the bean from the Spring container. And this is nothing really new, same coding as before, you simply use your Spring context, you say, context dot get bean, and you simply give the bean ID. So in the previous slide here I had the bean ID of that Silly Coach. You simply give that bean ID and it'll return back a coach implementation. And that's it really, that's pretty much all you have to do. So the key here is that you didn't have to list out all of the beans in the Spring config file. All you had to do was simply add the appropriate component annotation to your Spring beans and they're automatically registered thanks to that component scanning and thanks to Java annotations. So this is good stuff here. Now in the following videos we're gonna move into eclipse, we're gonna get our hands dirty, and we'll set up a project, we'll start writing code, and we'll do all of this from scratch, and we'll do it all step by step. So see you in the next video.