spring-5-with-spring-boot-2 / 09 - Working with Java Based Configuration /001 Java Based Config_en.srt
| 1 | |
| 00:00:00,000 --> 00:00:03,000 | |
| (gentle music) | |
| 2 | |
| 00:00:03,000 --> 00:00:06,000 | |
| -: So till this point we were able to create a project, | |
| 3 | |
| 00:00:06,000 --> 00:00:09,000 | |
| the spring project, with the help of XML configuration. | |
| 4 | |
| 00:00:09,000 --> 00:00:11,000 | |
| So whatever beans we want Spring to manage, | |
| 5 | |
| 00:00:11,000 --> 00:00:14,000 | |
| we were doing that in the XML file. | |
| 6 | |
| 00:00:14,000 --> 00:00:18,000 | |
| And XML file was your spring.xml, right? | |
| 7 | |
| 00:00:18,000 --> 00:00:20,000 | |
| Of course this name can change. | |
| 8 | |
| 00:00:20,000 --> 00:00:23,000 | |
| The only thing you have to change then is this thing, | |
| 9 | |
| 00:00:23,000 --> 00:00:26,000 | |
| you know in the ClassPathXmlApplicationContext, | |
| 10 | |
| 00:00:26,000 --> 00:00:29,000 | |
| you have to mention that we are not working with spring.xml. | |
| 11 | |
| 00:00:29,000 --> 00:00:31,000 | |
| We are working with some other name, but that's fine. | |
| 12 | |
| 00:00:31,000 --> 00:00:33,000 | |
| We can go with a name. | |
| 13 | |
| 00:00:33,000 --> 00:00:36,000 | |
| But now, let's say we don't want to use xml. | |
| 14 | |
| 00:00:36,000 --> 00:00:39,000 | |
| A lot of people are not a big fan of XML, | |
| 15 | |
| 00:00:39,000 --> 00:00:41,000 | |
| so they prefer to go for Java configuration. | |
| 16 | |
| 00:00:41,000 --> 00:00:44,000 | |
| So yes, for the Spring you can do this configuration | |
| 17 | |
| 00:00:44,000 --> 00:00:46,000 | |
| using Java code, that's right. | |
| 18 | |
| 00:00:46,000 --> 00:00:48,000 | |
| So if you are happy with XML, continue with that. | |
| 19 | |
| 00:00:48,000 --> 00:00:50,000 | |
| If you are not happy with XML, | |
| 20 | |
| 00:00:50,000 --> 00:00:54,000 | |
| you can try out the Java based configuration. | |
| 21 | |
| 00:00:54,000 --> 00:00:56,000 | |
| Sometime you don't have a choice when you join a project | |
| 22 | |
| 00:00:56,000 --> 00:00:58,000 | |
| and if they're using XML, you have to use XML. | |
| 23 | |
| 00:00:58,000 --> 00:01:00,000 | |
| If they're using Java based configuration, | |
| 24 | |
| 00:01:00,000 --> 00:01:01,000 | |
| you have to use Java based. | |
| 25 | |
| 00:01:01,000 --> 00:01:02,000 | |
| We have one more way, | |
| 26 | |
| 00:01:02,000 --> 00:01:04,000 | |
| which is the annotation based configuration, | |
| 27 | |
| 00:01:04,000 --> 00:01:05,000 | |
| which we'll do later. | |
| 28 | |
| 00:01:05,000 --> 00:01:06,000 | |
| But let's see, how do you work | |
| 29 | |
| 00:01:06,000 --> 00:01:08,000 | |
| with the Java based configuration? | |
| 30 | |
| 00:01:08,000 --> 00:01:10,000 | |
| So what we do is the first thing if we change is | |
| 31 | |
| 00:01:10,000 --> 00:01:13,000 | |
| we don't want to use the XML approach | |
| 32 | |
| 00:01:13,000 --> 00:01:15,000 | |
| we want to use a Java approach. | |
| 33 | |
| 00:01:15,000 --> 00:01:19,000 | |
| So of course for that you need a a file, a Java class. | |
| 34 | |
| 00:01:19,000 --> 00:01:21,000 | |
| So what I'll do is in this package I will right click | |
| 35 | |
| 00:01:21,000 --> 00:01:24,000 | |
| and I will say, hey, I want to create a new class. | |
| 36 | |
| 00:01:24,000 --> 00:01:26,000 | |
| In fact, first let me create a new package called config. | |
| 37 | |
| 00:01:26,000 --> 00:01:28,000 | |
| So the config file will go in this. | |
| 38 | |
| 00:01:28,000 --> 00:01:30,000 | |
| And inside this config package, | |
| 39 | |
| 00:01:30,000 --> 00:01:33,000 | |
| I will rightly can say new Java class | |
| 40 | |
| 00:01:33,000 --> 00:01:35,000 | |
| and let me create a Java class as let's say AppConfig. | |
| 41 | |
| 00:01:35,000 --> 00:01:37,000 | |
| Again, name doesn't matter, you can give any name. | |
| 42 | |
| 00:01:37,000 --> 00:01:40,000 | |
| But let me go for AppConfig, user enter. | |
| 43 | |
| 00:01:40,000 --> 00:01:42,000 | |
| So you can see we got AppConfig file here | |
| 44 | |
| 00:01:42,000 --> 00:01:45,000 | |
| and now what we are going to do with this? | |
| 45 | |
| 00:01:45,000 --> 00:01:49,000 | |
| So this is going to replace your XML configuration, right? | |
| 46 | |
| 00:01:49,000 --> 00:01:51,000 | |
| So of course if you don't want to use this, | |
| 47 | |
| 00:01:51,000 --> 00:01:53,000 | |
| I'm not going to delete this from the project. | |
| 48 | |
| 00:01:53,000 --> 00:01:56,000 | |
| If you want to use it, I will just keep this file there. | |
| 49 | |
| 00:01:56,000 --> 00:01:58,000 | |
| I will just close this or maybe I will just keep it open | |
| 50 | |
| 00:01:58,000 --> 00:02:00,000 | |
| so that we can refer. | |
| 51 | |
| 00:02:00,000 --> 00:02:03,000 | |
| Go back to your App.java. This is where things will change. | |
| 52 | |
| 00:02:03,000 --> 00:02:05,000 | |
| So we don't want to go for this approach. | |
| 53 | |
| 00:02:05,000 --> 00:02:08,000 | |
| What I will do is I will just comment everything here. | |
| 54 | |
| 00:02:08,000 --> 00:02:11,000 | |
| In fact, first let me just uncomment this, yeah. | |
| 55 | |
| 00:02:11,000 --> 00:02:12,000 | |
| So let me comment everything | |
| 56 | |
| 00:02:12,000 --> 00:02:15,000 | |
| and let's do our coding on top here. | |
| 57 | |
| 00:02:15,000 --> 00:02:18,000 | |
| So if I go back up, imagine we don't have any of this code, | |
| 58 | |
| 00:02:18,000 --> 00:02:21,000 | |
| it's just that for your reference, I'm keeping it there. | |
| 59 | |
| 00:02:21,000 --> 00:02:24,000 | |
| But here, let me create object of application context | |
| 60 | |
| 00:02:24,000 --> 00:02:26,000 | |
| or the reference of application context, | |
| 61 | |
| 00:02:26,000 --> 00:02:28,000 | |
| I would say context is equal to new. | |
| 62 | |
| 00:02:28,000 --> 00:02:31,000 | |
| Now instead of using the XML approach, | |
| 63 | |
| 00:02:31,000 --> 00:02:33,000 | |
| we are going for a Java based approach. | |
| 64 | |
| 00:02:33,000 --> 00:02:36,000 | |
| For that we have to use a class | |
| 65 | |
| 00:02:36,000 --> 00:02:39,000 | |
| which is called AnnotationConfigApplicationContext, | |
| 66 | |
| 00:02:39,000 --> 00:02:40,000 | |
| let's go with this. | |
| 67 | |
| 00:02:40,000 --> 00:02:44,000 | |
| And in this particular constructor | |
| 68 | |
| 00:02:44,000 --> 00:02:46,000 | |
| you have to pass a name of your config file, | |
| 69 | |
| 00:02:46,000 --> 00:02:49,000 | |
| which is AppConfig.class. | |
| 70 | |
| 00:02:49,000 --> 00:02:50,000 | |
| The only thing is | |
| 71 | |
| 00:02:50,000 --> 00:02:52,000 | |
| you have to basically import the package for this | |
| 72 | |
| 00:02:52,000 --> 00:02:55,000 | |
| and package importing done. | |
| 73 | |
| 00:02:55,000 --> 00:02:57,000 | |
| So, in both the cases, even if you're using XML, | |
| 74 | |
| 00:02:57,000 --> 00:02:59,000 | |
| even if you're using a Java based configuration, | |
| 75 | |
| 00:02:59,000 --> 00:03:03,000 | |
| you are basically using the same container, | |
| 76 | |
| 00:03:03,000 --> 00:03:05,000 | |
| but the way you talk to a container changes. | |
| 77 | |
| 00:03:05,000 --> 00:03:08,000 | |
| So here we were using XML way of talking to the containers, | |
| 78 | |
| 00:03:08,000 --> 00:03:11,000 | |
| here we are using Java based configuration. | |
| 79 | |
| 00:03:11,000 --> 00:03:13,000 | |
| Now how do we do the configuration? That's a real question. | |
| 80 | |
| 00:03:13,000 --> 00:03:14,000 | |
| Okay, so what we will do is | |
| 81 | |
| 00:03:14,000 --> 00:03:18,000 | |
| let's create the object or let's use the object for a class. | |
| 82 | |
| 00:03:18,000 --> 00:03:20,000 | |
| Now let's not use Alien | |
| 83 | |
| 00:03:20,000 --> 00:03:21,000 | |
| because Alien is dependent upon computers, | |
| 84 | |
| 00:03:21,000 --> 00:03:23,000 | |
| so we have to do multiple things. | |
| 85 | |
| 00:03:23,000 --> 00:03:25,000 | |
| Let's work with desktop because if you look at desktop, | |
| 86 | |
| 00:03:25,000 --> 00:03:28,000 | |
| it's a independent class like laptop as well. | |
| 87 | |
| 00:03:28,000 --> 00:03:32,000 | |
| So, let me create the instance for desktop. | |
| 88 | |
| 00:03:32,000 --> 00:03:35,000 | |
| So I will say desktop, I will say des, | |
| 89 | |
| 00:03:35,000 --> 00:03:39,000 | |
| or maybe I'll say dt is equal to, | |
| 90 | |
| 00:03:39,000 --> 00:03:42,000 | |
| instead of saying new, I want to say context.getBean. | |
| 91 | |
| 00:03:43,000 --> 00:03:47,000 | |
| And I want to say I want the object of the desktop.class. | |
| 92 | |
| 00:03:47,000 --> 00:03:48,000 | |
| That's it. | |
| 93 | |
| 00:03:48,000 --> 00:03:49,000 | |
| So I want this to work. | |
| 94 | |
| 00:03:49,000 --> 00:03:52,000 | |
| And once you've got the object, I can also print something. | |
| 95 | |
| 00:03:52,000 --> 00:03:54,000 | |
| I can call the method, which is compile. | |
| 96 | |
| 00:03:54,000 --> 00:03:55,000 | |
| So it it should print something. | |
| 97 | |
| 00:03:55,000 --> 00:03:57,000 | |
| If you run this code, let's see what happens. | |
| 98 | |
| 00:03:57,000 --> 00:04:00,000 | |
| The moment you run this code, you got an error. | |
| 99 | |
| 00:04:00,000 --> 00:04:04,000 | |
| The error is no qualifying bean of type desktop available. | |
| 100 | |
| 00:04:04,000 --> 00:04:05,000 | |
| Of course, right? | |
| 101 | |
| 00:04:05,000 --> 00:04:07,000 | |
| Now we are not using a XML configuration. | |
| 102 | |
| 00:04:07,000 --> 00:04:08,000 | |
| We are using Java based configuration | |
| 103 | |
| 00:04:08,000 --> 00:04:12,000 | |
| and in this file we did nothing. | |
| 104 | |
| 00:04:12,000 --> 00:04:13,000 | |
| That's weird, right? | |
| 105 | |
| 00:04:13,000 --> 00:04:14,000 | |
| So if you want to make this work, | |
| 106 | |
| 00:04:14,000 --> 00:04:16,000 | |
| the first thing you will do is you will use annotation, | |
| 107 | |
| 00:04:16,000 --> 00:04:18,000 | |
| which is called configuration. | |
| 108 | |
| 00:04:18,000 --> 00:04:20,000 | |
| So if you want to configure your application | |
| 109 | |
| 00:04:20,000 --> 00:04:22,000 | |
| or if you want to configure | |
| 110 | |
| 00:04:22,000 --> 00:04:23,000 | |
| with the help of Java based configuration | |
| 111 | |
| 00:04:23,000 --> 00:04:26,000 | |
| we have to make your config class | |
| 112 | |
| 00:04:26,000 --> 00:04:29,000 | |
| with annotation called add configuration. | |
| 113 | |
| 00:04:29,000 --> 00:04:31,000 | |
| That's one. | |
| 114 | |
| 00:04:31,000 --> 00:04:32,000 | |
| What next? | |
| 115 | |
| 00:04:32,000 --> 00:04:33,000 | |
| We have one more tag | |
| 116 | |
| 00:04:33,000 --> 00:04:36,000 | |
| or one more annotation which we have to use, which is bean. | |
| 117 | |
| 00:04:36,000 --> 00:04:38,000 | |
| So in the XML, we were using bean tag. | |
| 118 | |
| 00:04:38,000 --> 00:04:41,000 | |
| Here we are going to use the bean annotation. | |
| 119 | |
| 00:04:41,000 --> 00:04:43,000 | |
| Okay, so what do you want? You want desktops. | |
| 120 | |
| 00:04:43,000 --> 00:04:45,000 | |
| I will just go back here, say desktop. | |
| 121 | |
| 00:04:45,000 --> 00:04:47,000 | |
| I mean I want the object for desktops. | |
| 122 | |
| 00:04:47,000 --> 00:04:51,000 | |
| I will say public, Desktop. And I will say desktop. | |
| 123 | |
| 00:04:51,000 --> 00:04:53,000 | |
| I want the object for desktop, right? | |
| 124 | |
| 00:04:53,000 --> 00:04:55,000 | |
| We have to import the package, let's do that, okay. | |
| 125 | |
| 00:04:55,000 --> 00:04:58,000 | |
| Now since this is a Java based configuration, | |
| 126 | |
| 00:04:58,000 --> 00:05:01,000 | |
| we have to manually create the object, okay? | |
| 127 | |
| 00:05:01,000 --> 00:05:05,000 | |
| So you will say return new desktop, that's it. | |
| 128 | |
| 00:05:05,000 --> 00:05:07,000 | |
| Now you'll be saying, hey, you know, | |
| 129 | |
| 00:05:07,000 --> 00:05:10,000 | |
| by using new keyword, we are cutting the object. | |
| 130 | |
| 00:05:10,000 --> 00:05:13,000 | |
| The thing about this, yes, we are writing this code here, | |
| 131 | |
| 00:05:13,000 --> 00:05:15,000 | |
| but then we are not injecting the object, right? | |
| 132 | |
| 00:05:15,000 --> 00:05:17,000 | |
| Spring is injecting it. | |
| 133 | |
| 00:05:17,000 --> 00:05:19,000 | |
| So who will call this particular method? Spring. | |
| 134 | |
| 00:05:19,000 --> 00:05:22,000 | |
| Who will manage the object? Spring. | |
| 135 | |
| 00:05:22,000 --> 00:05:23,000 | |
| We are not doing it, | |
| 136 | |
| 00:05:23,000 --> 00:05:25,000 | |
| we are just writing a code by saying new desktop. | |
| 137 | |
| 00:05:25,000 --> 00:05:28,000 | |
| But all this thing will be created, injected, | |
| 138 | |
| 00:05:28,000 --> 00:05:30,000 | |
| and managed by Spring framework. | |
| 139 | |
| 00:05:30,000 --> 00:05:31,000 | |
| That's something you have to remember. | |
| 140 | |
| 00:05:31,000 --> 00:05:32,000 | |
| Okay, so once we have done this, | |
| 141 | |
| 00:05:32,000 --> 00:05:33,000 | |
| the only thing you have to do here | |
| 142 | |
| 00:05:33,000 --> 00:05:36,000 | |
| is we have to add an annotation called bean. | |
| 143 | |
| 00:05:36,000 --> 00:05:39,000 | |
| So if you want your spring framework to create the object, | |
| 144 | |
| 00:05:39,000 --> 00:05:41,000 | |
| you have to use add bean. | |
| 145 | |
| 00:05:41,000 --> 00:05:42,000 | |
| Now this is similar to what we have done in the XML. | |
| 146 | |
| 00:05:42,000 --> 00:05:45,000 | |
| In the XML also we are reading bean tag, right? | |
| 147 | |
| 00:05:45,000 --> 00:05:47,000 | |
| The same thing we have done here. | |
| 148 | |
| 00:05:47,000 --> 00:05:50,000 | |
| We just shared using bean annotation. | |
| 149 | |
| 00:05:50,000 --> 00:05:52,000 | |
| And now let's see if this works. | |
| 150 | |
| 00:05:52,000 --> 00:05:56,000 | |
| Let's go back here, let's rerun and it works. | |
| 151 | |
| 00:05:56,000 --> 00:05:59,000 | |
| You can see it says compiling using desktop. | |
| 152 | |
| 00:05:59,000 --> 00:06:01,000 | |
| So what we have done is instead of using XML approach, | |
| 153 | |
| 00:06:01,000 --> 00:06:04,000 | |
| we are using the Java approach. | |
| 154 | |
| 00:06:04,000 --> 00:06:07,000 | |
| Even if we have this confusion still till now | |
| 155 | |
| 00:06:07,000 --> 00:06:09,000 | |
| that we are creating our own object here. | |
| 156 | |
| 00:06:09,000 --> 00:06:11,000 | |
| See we are just writing this code, | |
| 157 | |
| 00:06:11,000 --> 00:06:14,000 | |
| but this will be executed by Spring, | |
| 158 | |
| 00:06:14,000 --> 00:06:16,000 | |
| creating, injecting, managed by spring framework. | |
| 159 | |
| 00:06:16,000 --> 00:06:18,000 | |
| So we are not manually doing it. | |
| 160 | |
| 00:06:18,000 --> 00:06:21,000 | |
| Okay? So the approach which we have used here | |
| 161 | |
| 00:06:21,000 --> 00:06:23,000 | |
| is we are looking for the desktop class. | |
| 162 | |
| 00:06:23,000 --> 00:06:25,000 | |
| We are not specified the name of a bean, | |
| 163 | |
| 00:06:25,000 --> 00:06:29,000 | |
| we are looking for the type of the bean, which is desktop. | |
| 164 | |
| 00:06:29,000 --> 00:06:31,000 | |
| So it is looking for the desktop bean and it call it. | |
| 165 | |
| 00:06:31,000 --> 00:06:34,000 | |
| That's how we use the Java based configuration | |
| 166 | |
| 00:06:34,000 --> 00:06:37,000 | |
| with the help of this particular class here. | |
| 167 | |
| 00:06:37,000 --> 00:06:39,000 | |
| Now what if you want to use a bean name? | |
| 168 | |
| 00:06:39,000 --> 00:06:40,000 | |
| As of now you can see | |
| 169 | |
| 00:06:40,000 --> 00:06:42,000 | |
| nowhere we have mentioned the be name. | |
| 170 | |
| 00:06:42,000 --> 00:06:44,000 | |
| So what is a bean name and how do we change it? | |
| 171 | |
| 00:06:44,000 --> 00:06:46,000 | |
| Let's see that in the next video. | |