Thursday, 30 June 2022

Spring Part 07 - @Component

  Spring - @Component

Maven Dependency:

<dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-context</artifactId>

  <version>5.3.16</version>

</dependency>


Sample Program for @Component 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"
       
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:component-scan base-package="com.ah.di"/>

</beans>



<context:component-scan base-package="com.ah.di"/>


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");
       
/*for(String bean : 
context.getBeanDefinitionNames()){
            System.out.println(bean);
        }*/                
var carObj = context.getBean("car",Car.class);          carObj.displayCarInfo();     } }

package com.ah.di;
import
org.springframework.stereotype.Component;
@Component public class Car {
   
public void displayCarInfo(){         System.out.println("car class display method
");
    }
}

package com.ah.di;
import
org.springframework.stereotype.Component;
@Component public class Engine {
   
public void testEngine(){         System.out.println("Engine is working");     } }


                                                                      Watch Demo


Wednesday, 29 June 2022

Spring Part 06 - @Qualifier

  Spring - Qualifier

Maven Dependency:

<dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-context</artifactId>

  <version>5.3.16</version>

</dependency>


Sample Program for @Qualifier 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"
       
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="engineModel1" class="com.ah.di.Engine">
        <property 
name="engineModel" value="Model-1"/>
    </bean>

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

    <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 import or.springframework.beans.factory.annotation.Qualifier;
public class
Car {
   
@Autowired     @Qualifier("engineModel2")     private 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 void testEngine(){         System.out.println("engine is working" );     } }

                                                                           Watch Demo



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


Sunday, 26 June 2022

Spring Part 04 - Setter Injection (Class)

Spring - Dependency Injection

Dependency Injection using class

Sample Program for Dependency Injection 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="car" class="com.ah.di.Car">
        <property 
name="engine">
            <bean 
class="com.ah.di.Engine"/>
        </property>
    </bean>

</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 void testEngine(){
        System.
out.println("engine is working" );
   
}
}


                                                                         Watch Demo





Spring Part 03 - Constructor Injection

 Spring - Construction Injection

Maven Dependency:

<dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-context</artifactId>

  <version>5.3.16</version>

</dependency>


Sample Program for Construction Injection 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="commonEngine" class="com.ah.di.Engine"/>

    <bean
id="car1" class="com.ah.di.Car">
        <constructor-arg
name="engine" ref="commonEngine"/>
    </bean>

    <bean
id="car2" class="com.ah.di.Car">
        <constructor-arg
name="engine" ref="commonEngine"/>
    </bean>
</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) {

   
/*     Engine engine = new Engine();
        Car car = new Car(engine);
        car.callEngine();*/

/*        Car car = new Car(new Engine());
        car.callEngine();*/

        
ApplicationContext context = new ClassPathXmlApplicationContext("beans_di.xml");
        
Car carObj1 = context.getBean("car1",Car.class);
        
carObj1.callEngine();

        
Car carObj2 = context.getBean("car2",Car.class);
        
carObj2.callEngine();
    
}
}


package com.ah.di;
public class
Engine {
   
public Engine() {
        System.
out.println("Engine constructor.");
   
}
   
public void engineInfo(){
        System.
out.println("engineInfo method of Engine class called.");
   
}
}

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


                                                                  Watch Demo