Refactoring Angular and Java Application code

 In This topic we will refactor our code for better readability.

Refactor JAVA Code :

Let's start with JAVA Code so you can see in previous few topic we have added cross origin annotation in each controller to call API from Angular application. what will happen if you have lots of Controller then there will a chance we miss adding it.

So what we will do ? For now we will add a bean in RestWebserviceApplication.class which will handle cross origin. We are going to use WebMvcConfigurer and will override addCorsMappings as per our project needs.

WebMvcConfigurer : It is an Interface which is used to customized JAVA Configuration for spring MVC enabled project.

addCorsMappings/CorsRegistry : It is a default method provided by WebMvcConfigurer to configure global cross origin request.

Update your code as shown below and once you add this you can remove CrossOrigin annotation from Controller class. 

Check from UI if your application is working fine after this change.

Refactor Angular Code : 

As we have defined the URL in method of service which is not the correct way so we will create a JSON file "environment.json" in which we will define all URL that we are going to use. we will create one class in environment.ts" which will read JSON file and set the URL's and then we will use this in place of direct URL call.

so create a JSON file as shown below


Now add below classes in environment.ts file:



After adding these code you have to add APP_INITIALIZER in app.module.ts which is used as Dependency Injection token.
The provided functions are injected at application startup and executed during app initialization. If any of these functions returns a Promise or an Observable, initialization does not complete until the Promise is resolved or the Observable is completed.
The function that we create is executed during the application bootstrap process, and the needed data is available on startup.
APP_INITIALIZER is present in @angular/core package.

Add below code in app.module.ts 

After adding this now you are able to use EnvironmentConstant in place of direct URL change code as below in all services.



Now if you go http://localhost:4200/loginand refresh it in Inspect > Network tab you can see environment.json file gets loaded. As I earlier said that the function that we create is executed during the application bootstrap process, and the needed data is available on startup. 

Screenshot Below:

Now check your application is working fine or not.

From Now onwards we will add JAVA API URL in the bean that we created in RestWebserviceApplication.class.

In Angular we will add API URL's in Environment.json file and also update Environment.ts file.

  

Previous Topic                                                                                 Next Topic

Comments

Popular posts from this blog

Installing Node Js (NPM) & Visual Studio Code

Logout Component and Angular Router (CanActivate)

Authentication and Session Storage