1 00:00:00,230 --> 00:00:01,690 The approach we're going to tackle next, 2 00:00:01,690 --> 00:00:05,150 is one of deploying your build to an existing application server. 3 00:00:05,150 --> 00:00:07,580 In order to deploy your app to an application server, 4 00:00:07,580 --> 00:00:09,370 you'll likely need a WAA file. 5 00:00:09,370 --> 00:00:12,910 WAA stands for Web Application Archive and it's kind of like a jar file, but 6 00:00:12,910 --> 00:00:14,920 its contents have a different structure. 7 00:00:14,920 --> 00:00:17,640 Check the teacher's notes for details on WAA files. 8 00:00:17,640 --> 00:00:20,520 The server all use for demonstrating is WildFly. 9 00:00:20,520 --> 00:00:24,000 Now, this is what used to be known as the JBoss Community Edition. 10 00:00:24,000 --> 00:00:26,820 JBoss is a flagship product as an application server 11 00:00:26,820 --> 00:00:31,540 that's a full implementation of Java Enterprise Edition or Java EE. 12 00:00:31,540 --> 00:00:33,860 This means that the server provides implementations for 13 00:00:33,860 --> 00:00:35,810 all Java EE interfaces. 14 00:00:35,810 --> 00:00:38,840 For example, JBoss implements dependency injection, or 15 00:00:38,840 --> 00:00:42,370 a DI, by leveraging the inject annotation, and 16 00:00:42,370 --> 00:00:47,700 also implements the Java Persistence API, or JPA, with Hibernate. 17 00:00:47,700 --> 00:00:52,600 For more on Java EE, check out the teachers notes as well In any case JBoss 18 00:00:52,600 --> 00:00:58,490 was bought by Red Hat in 2006, but the community edition of JBoss remains 19 00:00:58,490 --> 00:01:03,680 open source and freely available and in 2014 was renamed WildFly. 20 00:01:04,680 --> 00:01:07,890 Before we do anything with WildFly, we'll need to make some code changes to 21 00:01:07,890 --> 00:01:12,140 our application can play nicely with another web server. 22 00:01:12,140 --> 00:01:16,860 Specifically, we need to configure our app for a Servlet 3.0 container. 23 00:01:16,860 --> 00:01:19,400 What is a servlet container you ask? 24 00:01:19,400 --> 00:01:23,150 A servlet container is a component on a web server that is configured to handle 25 00:01:23,150 --> 00:01:26,850 HTTP requests that are process by Java applications. 26 00:01:26,850 --> 00:01:30,320 The container creates and maintains, what are called servlets and these servlets 27 00:01:30,320 --> 00:01:33,250 were the things that interact directly with our application code. 28 00:01:33,250 --> 00:01:36,950 For Spring weather applications typically we'll need to Servlet container to create 29 00:01:36,950 --> 00:01:40,720 Spring's DispatcherServlet which will marshal a quests for 30 00:01:40,720 --> 00:01:42,790 configured controllers. 31 00:01:42,790 --> 00:01:46,780 To configure a Servlet 3.0 container such as that provided by WildFly. 32 00:01:46,780 --> 00:01:50,310 To bootstrap our application and create this DispatcherServlet, we'll need to 33 00:01:50,310 --> 00:01:53,830 implement the web application initializer interface which is an interface 34 00:01:53,830 --> 00:01:58,280 detected by the Servlet container Spring has a handy way of doing this. 35 00:01:58,280 --> 00:02:00,525 So let's go to the application class and do that. 36 00:02:00,525 --> 00:02:04,910 Spring-boot provides an abstract class called 37 00:02:04,910 --> 00:02:09,030 SpringBootServletInitializer that we can extend right here. 38 00:02:09,030 --> 00:02:14,880 So extends SpringbootServletInitializer just like that. 39 00:02:14,880 --> 00:02:18,300 Now this is an abstract class and if you look at the source, 40 00:02:18,300 --> 00:02:22,190 it implements that WebApplicationInitializer interface. 41 00:02:22,190 --> 00:02:28,590 Cool, now in this class we'll then override the configure method right 42 00:02:28,590 --> 00:02:33,740 here which allows us to add the sources of all of our configuration classes. 43 00:02:33,740 --> 00:02:36,890 Now, in this method, we'll just add the application class since it 44 00:02:36,890 --> 00:02:39,390 already includes an annotation to scan for 45 00:02:39,390 --> 00:02:43,245 components in its package and all sub-packages. 46 00:02:43,245 --> 00:02:48,975 So let's do that now, I will use that builder or parameter value there. 47 00:02:50,315 --> 00:02:54,435 I will add the source application that class, cool. 48 00:02:54,435 --> 00:02:57,015 There are other things you could do here in this method. 49 00:02:57,015 --> 00:02:58,895 So check the teacher's notes for an example. 50 00:03:00,215 --> 00:03:05,160 Next, let's move to the data config class In our application deployed 51 00:03:05,160 --> 00:03:08,760 to WildFly we'll configure the data source on the web server itself. 52 00:03:08,760 --> 00:03:12,520 As you'll see on the web server we'll end up giving our database an easy to look up 53 00:03:12,520 --> 00:03:16,870 name using the Java naming and directory interface or JNDI. 54 00:03:16,870 --> 00:03:23,300 This is just a way for Java applications to look up databases or objects by name. 55 00:03:23,300 --> 00:03:27,900 What this means for us in our data config class is that we'll need an alternate 56 00:03:27,900 --> 00:03:32,810 data source bin to be used in our application that's deployed to WildFly. 57 00:03:32,810 --> 00:03:35,870 This one that's configured in the application will no longer suffice 58 00:03:35,870 --> 00:03:40,800 since that configuration will now appear on WildFly itself. 59 00:03:40,800 --> 00:03:44,870 So what we'll wanna do is create another data source bean method. 60 00:03:44,870 --> 00:03:46,300 Just like this. 61 00:03:46,300 --> 00:03:51,610 Bean public datasource and 62 00:03:51,610 --> 00:03:55,360 I'll call this jndiDataSource. 63 00:03:57,680 --> 00:04:01,840 Now, we want this DataSource being to be picked up sing the simple name 64 00:04:01,840 --> 00:04:05,910 DataSource but this method name is not data source in fact in order for 65 00:04:05,910 --> 00:04:09,930 the Java code to compile a cannot be DataSource that's a compiler error. 66 00:04:09,930 --> 00:04:13,740 Two methods with the same signature and same class cannot exist. 67 00:04:13,740 --> 00:04:19,310 So let's specify that the name of this being is DataSource. 68 00:04:19,310 --> 00:04:22,930 Now the name of this bean up here is already by default data source because by 69 00:04:22,930 --> 00:04:27,600 default spring will use the method name to name beans. 70 00:04:28,800 --> 00:04:35,900 Now to be sure that our application only has one unique data source being available 71 00:04:35,900 --> 00:04:40,970 to it at run time, we need to specify what are called profiles in spring. 72 00:04:40,970 --> 00:04:45,580 For example we could use this being here all specify the profile we 73 00:04:45,580 --> 00:04:50,060 could use this bean in production abbreviated prod and 74 00:04:50,060 --> 00:04:57,400 this bean in will say a development environment abbreviated dev. 75 00:04:57,400 --> 00:04:58,870 Now, in any given deployment, 76 00:04:58,870 --> 00:05:01,810 whether it's in a development environment or in a production environment, 77 00:05:01,810 --> 00:05:07,520 I want to make sure that exactly one of these beans is available, this bean for 78 00:05:07,520 --> 00:05:11,670 the production environment and this bean for the development environment. 79 00:05:11,670 --> 00:05:17,180 In order to do that, I need to set the active profile in spring 80 00:05:17,180 --> 00:05:22,380 If we don't set the active profiles, spring will detect two dataSource beans 81 00:05:22,380 --> 00:05:25,450 as injectable and when that happens and 82 00:05:25,450 --> 00:05:29,790 a dataSource bean as needed, we will get a no such bean definition exception. 83 00:05:29,790 --> 00:05:35,010 Since bean definitions need to be unique in any running application. 84 00:05:35,010 --> 00:05:38,260 Now, there are a couple ways to do this, I'll demonstrate one option here and leave 85 00:05:38,260 --> 00:05:41,770 a couple other options in the teacher's notes, so, be sure to check those out. 86 00:05:41,770 --> 00:05:46,270 The optional show here, is setting the active profile and application.properties. 87 00:05:46,270 --> 00:05:51,610 This is a pretty speedy task for us, we'll create a property at the top, 88 00:05:51,610 --> 00:05:56,360 named spring.profiles .active and 89 00:05:56,360 --> 00:06:00,160 set it to prod and that's it. 90 00:06:00,160 --> 00:06:03,250 Now when an application has multiple qualifying beings available as a lot of 91 00:06:03,250 --> 00:06:07,540 wired or otherwise injected dependencies assuming only one of those has been 92 00:06:07,540 --> 00:06:12,700 annotated with the profile prod that one will be chosen and 93 00:06:12,700 --> 00:06:15,150 we can avoid that no such being definition exception 94 00:06:16,260 --> 00:06:19,610 the other thing this allows us to do is pretty handy here. 95 00:06:19,610 --> 00:06:23,170 It allows us to avoid the scenario where we're commenting out one of these or 96 00:06:23,170 --> 00:06:23,990 the other. 97 00:06:23,990 --> 00:06:27,160 When we're switching between the two environments, we can leave both intact and 98 00:06:27,160 --> 00:06:30,260 set a profile for each one of them, individually. 99 00:06:30,260 --> 00:06:31,440 Pretty handy feature in spring. 100 00:06:32,770 --> 00:06:36,690 Okay, let's implement this jndiData Source method right here. 101 00:06:38,840 --> 00:06:40,870 Do here is use Springs 102 00:06:42,040 --> 00:06:46,260 jndidatasource to look up to grab the name of the data source. 103 00:06:46,260 --> 00:06:52,120 So that's new jndidatasourcelookup, and since I don't want to 104 00:06:52,120 --> 00:06:55,650 hard code configuration in my source code I'll grab this from the properties file. 105 00:06:55,650 --> 00:06:58,865 So getdatasource(end). 106 00:06:58,865 --> 00:07:01,840 .get property and 107 00:07:01,840 --> 00:07:07,300 I'll say it's Weather.jndi let me stick my sim icon at the end and 108 00:07:07,300 --> 00:07:11,180 of course we better add that property to our properties file. 109 00:07:11,180 --> 00:07:15,410 So let's go back to application.properties and 110 00:07:15,410 --> 00:07:20,110 I will add that right here whether.jndi and 111 00:07:20,110 --> 00:07:24,300 I will set that equal to java colon slash weather. 112 00:07:24,300 --> 00:07:31,000 Now, by convention, we begin a jndi name with this URL scheme, java colon slash. 113 00:07:32,090 --> 00:07:34,440 Cool, that should do it for the properties put these file here. 114 00:07:34,440 --> 00:07:37,530 But hey speaking of properties we need to be careful with our 115 00:07:37,530 --> 00:07:39,810 property source annotations. 116 00:07:39,810 --> 00:07:43,440 Now, unless otherwise specified in our app that's deployed to wild life 117 00:07:43,440 --> 00:07:47,490 those properties file paths will be considered relative to the root of our app 118 00:07:47,490 --> 00:07:48,940 instead of the class path. 119 00:07:48,940 --> 00:07:51,690 To make sure that our properties are still found we need to indicate 120 00:07:51,690 --> 00:07:53,470 that those are on the class path. 121 00:07:53,470 --> 00:07:59,250 Now I'll use Control + Shift + F to trigger a global search here. 122 00:07:59,250 --> 00:08:07,310 We'll do PropertySource just like that, and I see I have five locations 123 00:08:07,310 --> 00:08:12,550 where I have the PropertySource annotation referencing api.properties. 124 00:08:12,550 --> 00:08:16,720 And you can see in each one of these, that down here I just used api.properties 125 00:08:16,720 --> 00:08:21,380 now to make sure that this is relative to the class path. 126 00:08:21,380 --> 00:08:26,600 I need to prepend this with class path colon just like that and 127 00:08:26,600 --> 00:08:31,410 I can do that in each one of these class hath right from the search window, 128 00:08:32,640 --> 00:08:35,910 course you could also go in to those files directly. 129 00:08:35,910 --> 00:08:39,040 Let me just copy that 130 00:08:39,040 --> 00:08:49,400 there also. 131 00:08:49,400 --> 00:08:53,720 So all of those have been updated to include class path 132 00:08:53,720 --> 00:08:56,510 in the path of the properties file. 133 00:08:56,510 --> 00:09:01,580 So that wild fly doesn't give me an error when I try to deploy the application. 134 00:09:01,580 --> 00:09:03,080 Excellent let me close this window. 135 00:09:04,700 --> 00:09:07,920 Our second and the last task before creating a wire is to configure a great 136 00:09:07,920 --> 00:09:09,890 I'll build file to use the war plot again. 137 00:09:09,890 --> 00:09:14,910 So let me hop over to the build file and under the apply plugin spring boot 138 00:09:14,910 --> 00:09:19,730 I'll say apply plugin war excellent. 139 00:09:19,730 --> 00:09:25,480 Then we should set a couple properties for the grid wire task like this right here. 140 00:09:25,480 --> 00:09:28,860 In fact, I'll just copy that and paste it right here. 141 00:09:28,860 --> 00:09:31,950 Changing this from a jar to war. 142 00:09:31,950 --> 00:09:33,460 Cool. 143 00:09:33,460 --> 00:09:36,360 Okay, before we address the issue of the embedded web server. 144 00:09:36,360 --> 00:09:38,090 I'm gonna to revert my code. 145 00:09:38,090 --> 00:09:41,400 My dependencies here, so that it uses the default. 146 00:09:41,400 --> 00:09:45,710 That is the included Web server in the Spring Boot start thymeleaf, 147 00:09:45,710 --> 00:09:46,550 which is Tomcat. 148 00:09:46,550 --> 00:09:51,540 So, I'm going to remove jetty as a dependency, and 149 00:09:51,540 --> 00:09:56,650 I'm gonna remove that line of code that says exclude the Tomcat module. 150 00:09:56,650 --> 00:09:59,600 So, if I were to run the boot run task we'd have Tomcat up and running again. 151 00:10:00,660 --> 00:10:03,210 Okay. So now that we have Tomcat included by 152 00:10:03,210 --> 00:10:06,520 default again, we want to make sure that Tomcat Servlet container doesn't conflict 153 00:10:06,520 --> 00:10:10,210 with Wildfly's Servlet container because its Wildfly's Servlet container that would 154 00:10:10,210 --> 00:10:11,980 be running our application. 155 00:10:11,980 --> 00:10:15,770 So we'll need to mark Tomcat as a provided dependency at runtime. 156 00:10:16,850 --> 00:10:19,370 In order to do that, I'll just stick that right here. 157 00:10:19,370 --> 00:10:22,500 We use providedRuntime. 158 00:10:23,540 --> 00:10:33,109 And then we just name that dependency ‘org.springframework.boot:spring-boot- 159 00:10:33,109 --> 00:10:36,960 starter-tomcat’. 160 00:10:36,960 --> 00:10:39,680 Excellent. So, this will make sure that when 161 00:10:39,680 --> 00:10:43,820 our war file is built it will not include this imbedded web server here 162 00:10:43,820 --> 00:10:48,050 because we are well we're deploying the war file to an actual web server. 163 00:10:48,050 --> 00:10:50,800 We don't need the embedded one and even further, 164 00:10:50,800 --> 00:10:54,990 it will conflict with wild flies server like container. 165 00:10:54,990 --> 00:10:56,530 Okay, almost there. 166 00:10:56,530 --> 00:10:58,240 One final task before building. 167 00:10:58,240 --> 00:11:01,360 If we want to specify the context root of our application, 168 00:11:01,360 --> 00:11:03,570 to be something other than the name of our archive. 169 00:11:03,570 --> 00:11:07,560 That is, we want a URL that isn't, localhost:8080/whether-0.0.1-snapshot. 170 00:11:07,560 --> 00:11:11,620 Well then we've got to add 171 00:11:11,620 --> 00:11:16,870 a certain file to our application, that WildFly can detect. 172 00:11:16,870 --> 00:11:20,170 And this file has to be named jboss-web.xml and 173 00:11:20,170 --> 00:11:22,900 it's got to be located in a certain directory. 174 00:11:22,900 --> 00:11:24,320 What directory is that? 175 00:11:24,320 --> 00:11:28,215 Well it's under source/main and I'll create the other directories here. 176 00:11:28,215 --> 00:11:32,980 /webapp/web-INF and then the file 177 00:11:32,980 --> 00:11:38,960 there jboss-web.xml And 178 00:11:38,960 --> 00:11:43,860 these directories will be created when I click OK yes I will add that to get and 179 00:11:43,860 --> 00:11:50,570 here is our X.M.L. file so again source main web app web and and there's our file. 180 00:11:50,570 --> 00:11:52,770 Now we' gonna need to create a couple X.M.L. 181 00:11:52,770 --> 00:11:55,460 elements here the first one the top level one is J. 182 00:11:55,460 --> 00:11:56,680 Boss web. 183 00:11:56,680 --> 00:12:00,010 And then in order to specify the context root will use 184 00:12:00,010 --> 00:12:02,400 an XML element named just that. 185 00:12:02,400 --> 00:12:07,870 We want it to be the single slash will it will be the root of the Web Server and 186 00:12:07,870 --> 00:12:09,460 that is all. 187 00:12:09,460 --> 00:12:11,840 With that said let's build this thing. 188 00:12:11,840 --> 00:12:13,830 So I'll open my terminal here. 189 00:12:13,830 --> 00:12:19,182 All this up to give myself a little more space and I will do .slash gradlew or 190 00:12:19,182 --> 00:12:24,330 gradlew.bat and if you wanna build here it'll run the unit tests and 191 00:12:24,330 --> 00:12:26,410 then you'll get the war file as well. 192 00:12:26,410 --> 00:12:29,250 If you want to skip unit tests you could use war. 193 00:12:29,250 --> 00:12:32,130 In the last video you could have used ./gradlew 194 00:12:32,130 --> 00:12:34,160 jar if you wanted to skip the unit tests. 195 00:12:34,160 --> 00:12:37,580 So I'll just run the war here since I know my unit tests are already passing. 196 00:12:43,079 --> 00:12:45,012 You should see the code compiling and 197 00:12:45,012 --> 00:12:47,940 hopefully you'll have a successful build there. 198 00:12:47,940 --> 00:12:52,150 So in the build directory now what you should see yes, 199 00:12:52,150 --> 00:12:57,670 you should see the original jar and then you should see a war there as well. 200 00:12:57,670 --> 00:12:58,180 Cool. 201 00:12:58,180 --> 00:13:00,810 We're ready to deploy this to wild fly in our next video.