Wednesday 21 October 2020

Spring Core Project Configuration without xml bean, using Eclipse Maven

Project Code Structure

Following screenshot shows the structure of the project we will create.





Create pom.xml file


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.ramsis</groupId>

  <artifactId>springConfig</artifactId>

  <version>0.0.1-SNAPSHOT</version>



<properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <springframework.version>5.1.5.RELEASE</springframework.version>

    

    <maven.compiler.source>1.8</maven.compiler.source>

    <maven.compiler.target>1.8</maven.compiler.target>

  </properties>


  <dependencies>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>3.8.1</version>

      <scope>test</scope>

    </dependency>

        <dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${springframework.version}</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>${springframework.version}</version>

</dependency>

  </dependencies>


</project>


---------------------------------------------------------------------------------------------------------------------   


Create Config Class

package com.ramsis.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan(basePackages="com.ramsis.bean")
public class Config {
}


-----------------------------------------------------------------------------------------

Create Employee Class

package com.ramsis.bean;


import org.springframework.stereotype.Component;


@Component

public class Employee {


int id;

String name;

String department;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDepartment() {

return department;

}

public void setDepartment(String department) {

this.department = department;

}

}


------------------------------------------------------------------------------------------------------------------------


Create Runner Class

package com.ramsis.Main;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.ramsis.bean.Employee;
import com.ramsis.config.Config;

public class Runner {
public static void main(String args[]) {

AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext(Config.class);
Employee employee =context.getBean("employee",Employee.class);

employee.setName("Aman");
employee.setDepartment("IT Prod");

System.out.println("name: "+employee.getName());
System.out.println("Department: "+employee.getDepartment());

context.close();
}
}

-------------------------------------------------------------------------------------------------

After run this project you will get the output as below mention image






Sunday 11 October 2020

Create Builder Design Pattern in Java

Builder Design Pattern in Java

The builder pattern is a design pattern that allows for the step-by-step creation of complex objects using the correct sequence of actions. The construction is controlled by a director object that only needs to know the type of object it is to create.


The builder pattern, as name implies, is an alternative way to construct complex objects. This should be used only when you want to build different immutable objects using same object building process.



package com.vehicle;


public class Car {

private final String carCompany;

private final String name;

private final String model;

private final String colour;

private final String engine;

private Car(Builder builder) {

this.carCompany=builder.carCompany;

this.name=builder.name;

this.model=builder.model;

this.colour=builder.colour;

this.engine=builder.engine;

}

//All getter, and NO setter to provde immutability in Car

public String getName() {

return this.name;

}

public String getModel() {

return this.model;

}

public String getColour() {

return this.colour;

}

public String getEngine() {

return this.engine;

}

public static class Builder{

   

private final String carCompany;

private String name;

private String model;

private String colour;

private String engine;

public Builder(String carCompany){

this.carCompany = carCompany;

}

public Builder setName(String name) {

this.name=name;

return this;

}

public Builder setModel(String model) {

this.model=model;

return this;

}

public Builder setColour(String colour) {

this.colour=colour;

return this;

}

public Builder setEngine(String engine) {

this.engine=engine;

return this;

}

public Car build() {

return new Car(this);

}

}

@Override

public String toString() {

return "Builder [carCompany=" + carCompany + ", name=" + name + ", model=" + model + ", colour=" + colour+ ", engine=" + engine + "]";

}

}


------------------------------------------------------------------------------------------------------------------------


package com.vehicle;


public class Main {


public static void main(String[] args) {


Car car1 = new Car.Builder("Maruti").setName("Alto").setEngine("900CC").setModel("k10Vxi").build();

Car car2 = new Car.Builder("Ford").setName("Escort").setEngine("1600CC").setModel("MK2").build();

System.out.println(car1);

System.out.println(car2);

}


}





Tuesday 6 October 2020

Use of Comparable and Comparator class for sorting ArrayList Objects


Use of Comparable class for sorting ArrayList Objects


package com.test;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;


class Employee implements  Comparable{

private Integer rollNo;

private String name;

private Integer age;


public Employee(Integer rollNo, String name, Integer age) {

super();

this.rollNo = rollNo;

this.name = name;

this.age = age;

}


@Override

public String toString() {

return "Employee [rollNo=" + rollNo + ", name=" + name + ", age=" + age + "]\n";

}


@Override

public int compareTo(Object o) {  

Employee e = (Employee) o;

return this.rollNo.compareTo(e.rollNo);

}

}


public class Demo {


public static void main(String[] args) {

Employee emp = null;

    List<Employee> list = new ArrayList<Employee>();

    list.add( emp = new Employee(101, "Cheten", 36));

    list.add( emp = new Employee(603, "Brijesh", 29));

    list.add( emp = new Employee(308, "Ajay", 38));

    list.add( emp = new Employee(902, "Mansi", 26));

    list.add( emp = new Employee(327, "sunil", 40));

    list.add( emp = new Employee(206, "Aman", 32));

    list.add( emp = new Employee(490, "vimal", 25));

    Collections.sort(list);

    System.out.println(list);

    

}

}








Use of Comparator class for sorting ArrayList Objects

package com.test;


import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;


class Employee{

public Integer rollNo;

public String name;

public Integer age;


public Employee(Integer rollNo, String name, Integer age) {

super();

this.rollNo = rollNo;

this.name = name;

this.age = age;

}


@Override

public String toString() {

return "Employee [rollNo=" + rollNo + ", name=" + name + ", age=" + age + "]\n";

}

}


public class Demo {


public static void main(String[] args) {

Employee emp = null;

    List<Employee> list = new ArrayList<Employee>();

    list.add( emp = new Employee(101, "Cheten", 36));

    list.add( emp = new Employee(603, "Brijesh", 29));

    list.add( emp = new Employee(308, "Ajay", 38));

    list.add( emp = new Employee(902, "Mansi", 26));

    list.add( emp = new Employee(327, "sunil", 40));

    list.add( emp = new Employee(206, "Aman", 32));

    list.add( emp = new Employee(490, "vimal", 25));

    

  

    Comparator<Employee> com = new Comparator<Employee>() {

@Override

public int compare(Employee o1, Employee o2) {

if(o1.rollNo > o2.rollNo)

return 1;

return -1;

}

    };


    System.out.println("Before sorting by rollNo : \n"+list);

    Collections.sort(list,com);

    System.out.println("After sorting by rollNo : \n"+list);

    

}

}








Tuesday 5 February 2019

Download HTML Table data into Excel file using javascript




<script>
function exportTableToExcel(tableID){
    var downloadLink;
var  filename = '';
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
    
    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
}
</script>

<html>
<table id="dataTableID" Border="1px">
    <tr>
    <th>From</th>
        <th>To</th>
        <th>Name</th>
        <th>Email</th>
        <th>City</th>
    </tr>
    <tr>
    <td>Agra</td>
        <td>New Delhi</td>
        <td>Aman Kumar</td>
        <td>aman@gmail.com</td>
        <td>UP</td>
    </tr>
    <tr>
    <td>Mussoorie</td>
        <td>Haridwar</td>
        <td>Suraj</td>
        <td>Suraj@gmail.com</td>
        <td>UK</td>
    </tr>
    <tr>
    <td>Jammu</td>
        <td>Ghaziabad</td>
        <td>Ajay Sharma</td>
        <td>Ajay@gmail.com</td>
        <td>MP</td>
    </tr>
<tr>
    <td>Kanpur</td>
        <td>Allahabad</td>
        <td>Rahul Singh</td>
        <td>sam@gmail.com</td>
        <td>MP</td>
    </tr>
</table>
<br/>
<button onclick="exportTableToExcel('dataTableID')">Download Excel File</button>
</html>