Tuesday, 24 May 2022

Spring Part 02 - Dependency Injection (Primitives & String)

Spring - Dependency Injection

Dependency Injection using primitives & String

Sample Program for Dependency Injection without using 'Spring Framework'

package com.ah.di.setterdemo;

public class Test {
public static void main(String[] args) {
Car car = new Car();
car.setCarName("Ford");
car.displayCarInfo();
}
}

package com.ah.di.setterdemo;

public class Car {

private String carName;

public void setCarName(String carName) {
this.carName = carName;
}

public void displayCarInfo(){
System.out.println("Displaying car information"+
"\n car name : "+carName);
}
}

Maven Dependency:

<dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-context</artifactId>

  <version>5.3.16</version>

</dependency>

Sample Program for Dependency Injection using 'Spring Framework' 


beans_di_setter.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.ah.di.setterdemo.Car">
<property name="carName" value="Ford"/>
<property name="mileage" value="28"/>
</bean>
</beans>
package com.ah.di.setterdemo;

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_setter.xml");
Car carObj = context.getBean("car", Car.class);
carObj.displayCarInfo();
}
}
package com.ah.di.setterdemo;

public class Car {

private String carName;
private int mileage;

public void setMileage(int mileage) {
this.mileage = mileage;
}

public void setCarName(String carName) {
this.carName = carName;
}

public void displayCarInfo(){
System.out.println("Displaying car information"+
"\n car name : "+carName+
"\n mileage : "+mileage);
}
}

Watch Demo

Sunday, 22 May 2022

Spring Part 01 - IOC

Spring - Inversion of Control 

Sample Program to create object and call method without using 'Spring Framework'

package com.ah.ioc;

public class Test {
public static void main(String[] args) {
SamsungMobile sm = new SamsungMobile();
sm.display();
}
}
package com.ah.ioc;

public class SamsungMobile implements Mobile{
public SamsungMobile(){
System.out.println("Constructor of SamsungMobile class");
}

public void display(){
System.out.println("display() method in SamsungMobile class");
}
}


Maven Dependency:

<dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-context</artifactId>

  <version>5.3.16</version>

</dependency>


Sample Program to create object and call method using 'Spring Framework' 

package com.ah.ioc;

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_ioc.xml");
SamsungMobile samsungMobile = context.getBean("sm", SamsungMobile.class);
samsungMobile.display();
}

} 

package com.ah.ioc;

public class SamsungMobile implements Mobile{
public SamsungMobile(){
System.out.println("Constructor of SamsungMobile class");
}

public void display(){
System.out.println("display() method in SamsungMobile class");
}
}
beans_ioc.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sm" class="com.ah.ioc.SamsungMobile"/>
</beans>

Sample Program using interface 'Spring Framework'

package com.ah.ioc;

public interface Mobile {
public void display();
}
package com.ah.ioc;

public class SamsungMobile implements Mobile{
public SamsungMobile(){
System.out.println("Constructor of SamsungMobile class");
}

public void display(){
System.out.println("display() method in SamsungMobile class");
}
}
package com.ah.ioc;

public class NokiaMobile implements Mobile{
public NokiaMobile(){
System.out.println("Constructor of NokiaMobile class");
}

public void display(){
System.out.println("display() method in NokiaMobile class");
}
}
package com.ah.ioc;

public class ABCMobile implements Mobile{
public ABCMobile(){
System.out.println("Constructor of ABCMobile class");
}

public void display(){
System.out.println("display() method in ABCMobile class");
}
}
package com.ah.ioc;

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_ioc.xml");
Mobile mobile = context.getBean("mobile", Mobile.class);
mobile.display();
}
}
beans_ioc.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mobile" class="com.ah.ioc.ABCMobile"/>
</beans>

Watch Demo




Spring

Spring:

Part 1-  IOC: https://automation-home.blogspot.com/2022/05/spring-ioc.html 

Part 2-  Dependency Injection 

(Setter Injection : primitives & String) :

https://automation-home.blogspot.com/2022/05/spring-dependency-injection-primitives.html

Part 3-  Constructor Injection 

https://automation-home.blogspot.com/2022/06/spring-constructor-injection.html

Part 4- Dependency Injection 

(Setter Injection : class) :

https://automation-home.blogspot.com/2022/06/spring-setter-injection-class.html

Part 5- Autowiring

https://automation-home.blogspot.com/2022/06/Spring-Autowiring.html

Part 6- @Qualifier

https://automation-home.blogspot.com/2022/06/spring-part-06-qualifier.html

Part 7- @Component

https://automation-home.blogspot.com/2022/06/spring-part-07-component.html

Part 8- @ComponentScan & @Configuration  

https://automation-home.blogspot.com/2022/07/spring-part-08-componentscan.html

Part 9- @Bean

https://automation-home.blogspot.com/2022/07/spring-part-09-bean.html

Note: Will start with Spring core/basic annotations and move towards Spring Boot topic in upcoming demos
- Prerequisite: Basic knowledge on Java programming language
- Intended for Absolute begineers of Spring/Spring Boot Framework.