DI

Wrong:

package com.gs.ilp.logic;

public class Car {
	private int carId;
	private FordWheel FordWheel;

	public Car() {
	}

	public Car(int carId, com.gs.ilp.logic.FordWheel fordWheel) {
		this.carId = carId;
		FordWheel = fordWheel;
	}

	public int getCarId() {
		return carId;
	}

	public void setCarId(int carId) {
		this.carId = carId;
	}

	public FordWheel getFordWheel() {
		return FordWheel;
	}

	public void setFordWheel(FordWheel fordWheel) {
		FordWheel = fordWheel;
	}

	@Override
	public String toString() {
		return "Car [carId=" + carId + ", FordWheel=" + FordWheel + "]";
	}

}
package com.gs.ilp.logic;

public class FordWheel {

	private int id;
	private String nameOfTheWheel;

	public FordWheel(int id, String nameOfTheWheel) {
		this.id = id;
		this.nameOfTheWheel = nameOfTheWheel;
	}

	public FordWheel() {
	}

	public int getId() {
		return id;
	}

	public void setId(int idd) {
		id = idd;
	}

	public String getNameOfTheWheel() {
		return nameOfTheWheel;
	}

	public void setNameOfTheWheel(String nameOfTheWheel) {
		this.nameOfTheWheel = nameOfTheWheel;
	}

	@Override
	public String toString() {
		return "FordWheel [id=" + id + ", nameOfTheWheel=" + nameOfTheWheel + "]";
	}

}
package com.gs.ilp.logic;

public class MakeCar {
	public static void main(String[] args) {
//
//		FordWheel fordWheel = new FordWheel(90, "Ford's Tyre");
//		Car genericCar = new Car(1, fordWheel);
//		System.out.print(figo);
		
		
		MarutiWheel marutiWheel = new MarutiWheel(90, "Maruti's Tyre");
		Car genericCar = new Car(1, marutiWheel);
		System.out.print(genericCar);
	}

}
package com.gs.ilp.logic;

public class MarutiWheel {

	private int id;
	private String nameOfTheWheel;

	public MarutiWheel(int id, String nameOfTheWheel) {
		this.id = id;
		this.nameOfTheWheel = nameOfTheWheel;
	}

	public MarutiWheel() {
	}

	public int getId() {
		return id;
	}

	public void setId(int idd) {
		id = idd;
	}

	public String getNameOfTheWheel() {
		return nameOfTheWheel;
	}

	public void setNameOfTheWheel(String nameOfTheWheel) {
		this.nameOfTheWheel = nameOfTheWheel;
	}

	@Override
	public String toString() {
		return "MarutiWheel [id=" + id + ", nameOfTheWheel=" + nameOfTheWheel + "]";
	}

}

package com.gs.ilp.logic;

public interface Wheel {

}
public class MarutiWheel implements Wheel {
.....
.....
.....
}
public class FordWheel implements Wheel {
.....
.....
.....
}
package com.gs.ilp.logic;

public class Car {
	private int carId;
	private Wheel wheel;

	public Car() {
	}

	public Car(int carId, Wheel wheel) {
		this.carId = carId;
		this.wheel = wheel;
	}

	@Override
	public String toString() {
		return "Car [carId=" + carId + ", wheel=" + wheel + "]";
	}

}
package com.gs.ilp.logic;

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

		Wheel fordWheel = (Wheel) new FordWheel(90, "Ford's Tyre");

		Car genericCar = new Car(1, fordWheel);
		System.out.println(genericCar);

		Wheel marutiWheel = (Wheel) new MarutiWheel(90, "Maruti's Tyre");
		Car genericCar1 = new Car(1, marutiWheel);
		System.out.println(genericCar1);
	}

}

ServiceFactory :

package com.gs.ilp.logic;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ServiceFactory {

	public static Object getInstance(Class classType) {

		try {

			File fXmlFile = new File("D:/factotybinding.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);

			// optional, but recommended
			// read this -
			// http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
			doc.getDocumentElement().normalize();

			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

			NodeList nList = doc.getElementsByTagName("bean");

			System.out.println("----------------------------");

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);

				System.out.println("\nCurrent Element :" + nNode.getNodeName());

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					if (classType.getName().equalsIgnoreCase(eElement.getAttribute("class"))) {

						System.out.println("bean id : " + eElement.getAttribute("id"));
						System.out.println("class  : " + eElement.getAttribute("class"));

						Constructor c = classType.getDeclaredConstructor();
						c.setAccessible(true); // solution
						Object obj = c.newInstance();

						// Wheel wheel = (Wheel)
						// Class.forName(eElement.getAttribute("class")).newInstance();

						NodeList nListd = (NodeList) eElement.getElementsByTagName("property");
						for (int temp1 = 0; temp1 < nListd.getLength(); temp1++) {
							Node nNode1 = nListd.item(temp1);
							if (nNode1.getNodeType() == Node.ELEMENT_NODE) {

								Element eElement1 = (Element) nNode1;

								String field = String.valueOf(eElement1.getAttribute("name"));
								String value = String.valueOf(eElement1.getAttribute("value"));

								System.out.println("name :" + field);

								Field[] fields = classType.getDeclaredFields();
								String fieldReturned = checkAndReturnFieldName(fields, field);
								fieldReturned = fieldReturned.substring(0, 1).toUpperCase()
										+ fieldReturned.substring(1);
								Class type = checkAndReturnFieldType(fields, field);

								try {
									Method method = obj.getClass().getMethod("set" + fieldReturned, type);
									if (isNumeric(value)) {
										int val = Integer.parseInt(value);
										method.invoke(obj, val);
									} else {

										method.invoke(obj, value);
									}
								} catch (Exception ex) {
									ex.printStackTrace();
								}

							}
						}

						return obj;
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String checkAndReturnFieldName(Field[] fields, String field) {
		for (Field f : fields) {
			if (String.valueOf(f.getName()).equals(field)) {
				return field;
			}
		}
		return null;
	}

	private static Class checkAndReturnFieldType(Field[] fields, String field) {
		for (Field f : fields) {
			if (String.valueOf(f.getName()).equals(field)) {
				return f.getType();
			}
		}
		return null;
	}

	public static boolean isNumeric(String str) {
		for (char c : str.toCharArray()) {
			if (!Character.isDigit(c))
				return false;
		}
		return true;
	}
}
package com.gs.ilp.logic;

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

		Wheel fordWheel = (Wheel) ServiceFactory.getInstance(FordWheel.class);

		Car genericCar = new Car(1, fordWheel);
		System.out.println(genericCar);

		Wheel marutiWheel = (Wheel) ServiceFactory.getInstance(MarutiWheel.class);
		Car genericCar1 = new Car(1, marutiWheel);
		System.out.println(genericCar1);
	}

}

Last updated