Saturday 2 July 2022

Spring Part 09 - @Bean

 Spring - @Bean

package com.ah.di;

import 
org.springframework.context.annotation.Bean;
import 
org.springframework.context.annotation.ComponentScan;
import 
org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class AppConf {

    
@Bean("car")
    
public Car carBean(){
        Car car = 
new Car();
        return 
car;
    
}
}


package com.ah.di;
import
org.springframework.beans.factory.annotation.Autowired;
public class
Car {
   
@Autowired     private Engine engine;
    public void
displayCarInfo(){         System.out.println("car class display method\n");
        if
(engine==null){             System.out.println("No engine object");         }else{             engine.testEngine();         }     }
}


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


package com.ah.di;
import
org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class
Test {
   
public static void main(String[] args) {         ApplicationContext context =     new AnnotationConfigApplicationContext
(AppConf.class);
    var
carObj = context.getBean("car",Car.class);      carObj.displayCarInfo();
    
//displyAppContextBeanInfo(context);     }
   
public static void displyAppContextBeanInfo(ApplicationContext context ){          for(String bean : 
context.getBeanDefinitionNames()){
            System.out.println(bean);
        }
    }

}

                                                                   Watch Demo


Friday 1 July 2022

Spring Part 08 - @ComponentScan & @Configuration

 Spring - @ComponentScan & @Configuration

In this Part of Spring Framework  we will avoid using Spring configuration xml file, instead we will maintain configuration at java side by using the '@Configuration' annotation.

package com.ah.di.samp;

import 
org.springframework.context.annotation.ComponentScan;
import 
org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
(basePackages = "com.ah.di")
public class AppConf {
}


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
com.ah.di.samp.AppConf; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.
AnnotationConfigApplicationContext;
public class
Test {
   
public static void main(String[] args) {
        ApplicationContext context =
   
new AnnotationConfigApplicationContext
(AppConf.class);
    var
carObj = context.getBean("car",Car.class);      carObj.displayCarInfo();
    
//displyAppContextBeanInfo(context);
   
}     public static void displyAppContextBeanInfo
(ApplicationContext context ){
        
for(String bean : context.
getBeanDefinitionNames()){
            System.
out.println(bean);
       
}     } }


                                                                        Watch Demo



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