Thursday, March 22, 2018

Maven and Spring Boot Basics

Hello Everyone,

By this post I will talk about what maven is and it's uses along with depth knowledge about Spring Boot.

Let's first talk what maven is.........

Maven automates downloads dependencies, putting additional jars on a class path, compiling source code into binary code, running tests, packaging compiled code , minimizing the risk of humans making errors while building the software manually and separating the work of compiling and packaging our code from that of code construction.

Inorder to check whether your computer has installed with maven check below command,



What is a POM?

A Project Object Model or POM is the fundamental unit of work in Maven.It is XML format which contain all the dependencies and configuration details project need to process and get build.



Few commands that maven uses
  • mvn clean       - previously generated artifacts will be removed
  • mvn compile   - compile source code
  • mvn package   - bundle is created
  • mvn install      - package is copied to local repo
  • mvn deploy     - final package is copied to remote repo
  • mvn test          - testcases are executed


Before talking about Spring Boot let's talk about spring framework......

Spring is a lightweight framework which act as a framework of frameworks. It provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF etc. The framework is used to find solution to the various technical problems.

The Spring framework comprises several modules 


  • IOC
  • AOP
  • DAO
  • Context
  • ORM
  • WEB MVC 

What is a Dependency Injection (DI)?

By the name it gives you the maening. Dependencied are injected to classes rather than allowing them to have links to other classes.
This reduces coupling between each classes. DI is a specific type of Inversion Of control(IOC)


 Figure 1

 Figure 2


In figure 1 there is a tight coupling between address and student.If the address class get changed, the student class also need to be changed.But in figure 2 Address Object is passed to the constructor to set the value which reduces the coupling between the address and student.

DI can be achieve through using 3 methods.

● Setter Based
@Autowired
public void setName(StudentService sv){
this.name=sv;
}


● Constructor Based
@Autowired
public Student(StudentService sv){
this.student=sv;
}


● Fields
@Autowired
private CacheMonitor cache;


Now let's talk about what Spring Boot do...

Why to use Spring Boot?

Though Spring is a good framework, It is hard to configure and manage the code while it takes some time to bootstrap the project. But by using Spring Boot we can minimize those drawbacks.Integration of the 3rd party libraries are highly supported by Spring Boot. Presence of integrated server to run the application also provides Spring Boot more special.

Starters can be downloaded from the Spring Boot website easily.Below are some starters used by spring boot..


  • spring-boot-starter-security 
  • spring-boot-starter-web-services
  • spring-boot-starter-integration
  • spring-boot-starter-validation
  • spring-boot-starter-actuator

First Spring Boot Code

Bootstrapping the application.



By using run method, Spring Boot server gets started.By annotation @SpringBootApplication we tells to the application that we want to run the application using spring boot.

Let's see how Rest controller can be used with spring boot..



Using the browser we can navigate to the localhost and relevant port number we can get the output as hello world.This is the base of building large web services.

Let's meet again with another exiting post.Happy coding :)

©Copyright Viraj Wickramasinghe.

Monday, March 12, 2018

NodeJS Basics I

Hello Everyone,
By this post you will be able to understand the basic stuff about NodeJS.
Firstly I will introduce what NodeJS is,

Node.js is a very powerful JavaScript-based framework.Platform was built on Google Chrome's JavaScript V8 Engine. Node.js is a server side scripting language which is mostly used for back end of the application.

In order to see whether node.js is successfully installed in your machine, use the following commands.




Basic Hello World Program


Using the require directive I have used the http module and returned an instance of it.Instance of server is created from createServer method and in the body 'Hello World' is printed.By using listen keyword, we define that server runs on that port number and output will be available in that port number.After connection is successfully established, console log statement will be executed in the console.

In order to run the node.js file, first open a terminal in the directory where your js file exists.




Output to the console

Reading a File

Now Let's look at some of the capabilities node.js has. It has the ability to read files in either synchronous way or asynchronous way.When both forms are present, node.js tends to read in the synchronous way first.



To read files node file system module is required.It is represented as fs in node.js. In here read.txt is an external file which contains some content which is read by node application. Below is the read.txt file.



Asynchronous reading takes callback function with 2 parameters err and data.First parameter is used to represent any errors that can occur while reading and data parameter is used to state the completion of the function.
In synchronous reading readFileSync keyword is used where no callback functions is used.In general Asynchronous reading is more approprite than synchronous reading where no blocks can occur in the reading process.


Output to the console




Writing a File

In here we over write the existing file using writeFile method.First parameter is said to be the file name where we want to write the content and second parameter is the content that we want to write to the file and at third we use call function with err as the parameter to display any errors that can occur during writing.




Output to the console







Let's meet again with another exiting post.Happy coding :)

©Copyright Viraj Wickramasinghe.

Saturday, March 3, 2018

JavaScript Basics II

Hello Everyone,

This is the continuation of my 1st JS Basics post, In this post I will talk about Inheritance hierarchy with Prototyping objects and What Closure is?

Let's first talk about how inheritance is implemented using JS... 



In here Car extends from Vehicle class.Drive is a prototype object of class Vehicle.For the car to use that object, Car creates a object from Vehicle prototype and access the drive method of Vehicle using car.drive().

In here if we set this.model = model inside Car function as Vehicle('car'), property will be set to car in DOM(Document Object Model) but it gives an undefine case in the console.


Also when we use Vehicle.call(this, 'car') console will give the output as Car but DOM gives a no type scenario.

As prototypes doesn't inherit properties of parent class Vehicle, console.log(car.type) is undefined.

Output to the console




What is a Closure?

Closure is refers to as function which invokes/returns another function.Simply function inside another function.Let's simplifies this by using an example.





In here printAmount is the main function and I have written another function inside of that function print() which prints percentage calculation.That function is equals to a variable which returns from main function.

Then printAmount function is called from outside which execute both the functions and prints the console.log part in the sub function.

Output to the console





Let's meet again with some indepth about Node.js in the next post.


©Copyright Viraj Wickramasinghe.