Using net beans 8.02 and java 8
1. create java project
2. create 3 class files
---------------------------------------------------------------------------------------------------
2.1 Car File
public class Car {
private String make;
private String model;
private int year;
private Engine engine;
/*Dependency Injection by Constructor*/
public Car(String make,String model,int year,Engine engine){
this.engine=engine;
this.model=model;
this.make=make;
this.year=year;
}
public void setMake(String make){
this.make=make;
}
public String getMake(){
return make;
}
public void setModel(String model){
this.model=model;
}
public String getModel(){
return model;
}
public void setYear(int year){
this.year=year;
}
public int getYear(){
return year;
}
public void setEngine(Engine engine){
this.engine=engine;
}
public Engine getEngine(){
return engine;
}
public void showCarDetails(){
System.out.println("Car Details:");
System.out.println("Make="+getMake());
System.out.println("Model="+getModel());
System.out.println("Year="+getYear());
engine.showEngineDetails();
}
}
---------------------------------------------------------------------------------------------------
2.2 Engne.java
public class Engine {
private String engine;
private String maxPower;
private String engineLocation;
/*Dependency Injection by Constructor*/
public Engine(String engine,String maxPower, String engineLocation){
this.engine=engine;
this.maxPower=maxPower;
this.engineLocation=engineLocation;
}
public void setEngine(String engine){
this.engine=engine;
}
public String getEngine(){
return engine;
}
public void setMaxPower(String maxPower){
this.maxPower=maxPower;
}
public String getMaxPower(){
return maxPower;
}
public void setEngineLocation(String engineLocation){
this.engineLocation=engineLocation;
}
public String getEngineLocation(){
return engineLocation;
}
public void showEngineDetails(){
System.out.println("****Engine Details****");
System.out.println("Engine="+getEngine());
System.out.println("Maximum Power="+getMaxPower());
System.out.println("Engine Location="+getEngineLocation());
}
}
-----------------------------------------------------------------------------------
2.3 MainApp.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String args[]){
ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
Car car=(Car)ctx.getBean("myCar");
car.showCarDetails();
}
}
--------------------------------------------------------------------------
create folder in root called resources
create a file ApplicationContext.xml in the resource folder
------------------------------------------------------------------------
ApplicationContext.xml file
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean id="myCar" class="Car">
<!-- Injecting primitive values as Dependencies-->
<constructor-arg name="make" value="Maruti Suzuki"/>
<constructor-arg name="model" value="Wagon R"/>
<constructor-arg name="year" value="2012"/>
<!-- Injecting reference to other bean as Dependency-->
<constructor-arg name="engine" ref="myEngine"/>
</bean>
<bean id="myEngine" class="Engine">
<!-- Injecting primitive values as Dependencies-->
<constructor-arg name="engine" value="3664 cc"/>
<constructor-arg name="maxPower" value="304HP"/>
<constructor-arg name="engineLocation" value="front"/>
</bean>
</beans>
-----------------------------------------------------------------------------------
include below library files
05/11/2022 10:57 PM 52,124 commons-logging-1.1.1-api.jar
05/11/2022 10:57 PM 556,590 org.springframework.beans-3.0.6.release.jar
05/11/2022 10:57 PM 382,184 org.springframework.core-3.0.6.release.jar
05/11/2022 10:57 PM 169,752 org.springframework.expression-3.0.6.release.jar
05/11/2022 10:58 PM 53,082 spring-asm-3.0.6.RELEASE.jar
05/11/2022 10:58 PM 670,258 spring-context-3.0.6.RELEASE.jar
----------------------------------------------------------------------------
Run main file
Comments
Post a Comment