Skip to main content

Inversion of Control - Dependency Injection

 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

Popular posts from this blog

WSO2 API - M client development

  Write a Client Application Using the SDK ¶ Follow the steps in the  Quick Start Guide , to deploy the sample API, subscribe and generate keys. Info Access Token Once the keys are generated, copy the access token. You can use this token to invoke APIs that you subscribe to using the same application. Go to the Developer Portal. Select your API and  download the SDK for Java . In this example, you would have downloaded the  PizzaShackAPI_1.0.0_java.zip  file. This file name includes the API name, version, and language of the SDK. Unzip the  PizzaShackAPI_1.0.0_java.zip  file. Expand to see the folder structure of the unzipped file... Build the SDK using maven  . When it’s done, you can include this SDK as a dependency in your software project. Details of this maven dependency are included in the README.md file. Expand to view Maven dependency < dependency > < groupId > org.wso2 </ groupId > < artifactId ...

How to install the Citrix client on Ubuntu 22.04

 https://kangaroobyte.com/how-to-install-the-citrix-client-on-ubuntu-22-04-20220516-265/   Install the tar.gz instead You’ll need to be familiar with the terminal and basic commands. You will not need root access to install this. From the Citrix Workspace download page in the ‘tarball package’ section download the latest x86_64 tar.gz. Open a terminal Get ready to install it: Go to the directory you downloaded it to: cd Downloads (or whatever directory you downloaded it to). Make a temporary directory: mkdir icaclient Go to that directory: cd icaclient Extract the tar.gz: tar -zxvf ../icaclient*.tar.gz Start the setup: Run the setup: ./setupwfc In the setup answer 1 to install, accept the default install location then no to GStreamer. Once it takes you back to the menu you can select 3 to quit. You now need to add some additional SSL certificates: cd ~/ICAClient/linuxx64/keystore/cacerts ln -s /usr/share/ca-certificates/mozilla/* . c_rehash .