Tuesday, 28 June 2022

Spring Part 05 - Autowiring

Spring - Autowiring

Maven Dependency:

<dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-context</artifactId>

  <version>5.3.16</version>

</dependency>


Sample Program for Autowiring using 'Spring Framework' 

beans_di.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 
xmlns="http://www.springframework.org/schema/beans"
       
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       
xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd"
>

    <bean 
id="engineObj" class="com.ah.di.Engine"/>

    <bean 
id="car" class="com.ah.di.Car" autowire="byType"/>

</beans>


package com.ah.di;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class
Test {
   
public static void main(String[] args) {
        ApplicationContext context =
               
new ClassPathXmlApplicationContext("beans_di.xml");
       
Car car = context.getBean("car",Car.class);
       
car.checkCarEngine();
   
}
}


package com.ah.di;
public class
Car {
   
private Engine engine;
    public void
setEngine(Engine engine) {         this.engine = engine;     }
   
public void checkCarEngine(){
       
if(engine!=null){             engine.testEngine();         }else{             System.out.println("Engine is dead");         }
    }
}

package com.ah.di;
public class
Engine {
   
public Engine(){         System.out.println("Engine Constructor..");     }
   
public void testEngine(){         System.out.println("engine is working" );     }
}



beans_di.xml

<?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"
       
xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd"
>

    <
context:annotation-config/>

    <bean 
id="engine" class="com.ah.di.Engine">
        <property 
name="engineModel" value="Model-1"/>
    </bean>

    <bean 
id="carBattery" class="com.ah.di.Battery"/>

    <bean 
id="car" class="com.ah.di.Car" />

</beans>


package com.ah.di;
import
org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class
Test {
   
public static void main(String[] args) {         ApplicationContext context =                 new ClassPathXmlApplicationContext("beans_di.xml");
       
Car car = context.getBean("car",Car.class);         car.checkCarEngine();     } }


package com.ah.di;
import
org.springframework.beans.factory.annotation.Autowired;
public class
Car {
   
@Autowired     private Engine engine;
   
@Autowired     private Battery battery;
    public void
checkCarEngine(){         if(engine!=null){             engine.testEngine();         }else{             System.out.println("Engine is dead");         }
       
if(battery!=null){             battery.displayBatteryStatus();         }else{             System.out.println("Battery is dead");         }     }
}

package com.ah.di;
public class
Engine {
   
private String engineModel;
    public void
setEngineModel(String engineModel) {         this.engineModel = engineModel;     }
   
public void testEngine(){         System.out.println("engine is working" );         System.out.println("engineModel :: "+engineModel );     }
}


                                                                 Watch Demo


No comments:

Post a Comment