Constructor and setter based Dependency injection code example

This commit is contained in:
Vasudha Venkatesan
2020-02-17 22:46:46 +05:30
committed by akuksin
parent f1aa143b96
commit 3a929134ba
10 changed files with 294 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
package com.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.constructorinjection.Cake;
@SpringBootApplication(scanBasePackages= {"com.example.constructorinjection","com.example.dependency"})
public class ExampleApplicationCI {
private static Logger LOGGER=LoggerFactory.getLogger(ExampleApplicationCI.class);
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(ExampleApplicationCI.class, args);
Cake obj = context.getBean(Cake.class);
LOGGER.info("Cake : "+ obj.toString());
}
}

View File

@@ -0,0 +1,22 @@
package com.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.setterinjection.Cake;
@SpringBootApplication(scanBasePackages= {"com.example.setterinjection","com.example.dependency"})
public class ExampleApplicationSI {
private static Logger LOGGER=LoggerFactory.getLogger(ExampleApplicationSI.class);
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(ExampleApplicationSI.class, args);
Cake obj = context.getBean(Cake.class);
LOGGER.info("Cake : "+ obj.toString());
}
}

View File

@@ -0,0 +1,38 @@
package com.example.constructorinjection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.example.dependency.Flavor;
@Component
public class Cake {
private static Logger LOGGER = LoggerFactory.getLogger(Cake.class);
private Flavor flavor;
public Cake(Flavor flavor) throws IllegalAccessException {
//check if the required dependency is not null
if (flavor != null) {
this.flavor = flavor;
LOGGER.info("Flavor from Constructor Injection : " + flavor);
} else {
throw new IllegalArgumentException("Cake cannot be created with null flavor object");
}
}
public Flavor getFlavor() {
return flavor;
}
@Override
public String toString() {
return "Cake [flavor=" + flavor + "]";
}
}

View File

@@ -0,0 +1,25 @@
package com.example.dependency;
import org.springframework.stereotype.Component;
@Component
public class Flavor {
String flavorType = " Chocolate ";
String color = " White ";
public String getFlavorType() {
return flavorType;
}
public String getColor() {
return color;
}
@Override
public String toString() {
return "Flavor [flavorType=" + flavorType + ", color=" + color + "]";
}
}

View File

@@ -0,0 +1,24 @@
package com.example.dependency;
import org.springframework.stereotype.Component;
@Component
public class Topping {
private String toppingName;
public String getToppingName() {
return toppingName;
}
public void setToppingName(String toppingName) {
this.toppingName = toppingName;
}
@Override
public String toString() {
return "Topping [toppingName=" + toppingName + "]";
}
}

View File

@@ -0,0 +1,50 @@
package com.example.setterinjection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.dependency.Flavor;
import com.example.dependency.Topping;
@Component
public class Cake {
private Logger LOGGER = LoggerFactory.getLogger(Cake.class);
private Flavor flavor;
@Autowired
private Topping toppings;
public Cake() {
LOGGER.info("Flavor from setter Injection : " + this.flavor);
}
@Autowired
public void setFlavor(Flavor flavor) {
LOGGER.info("Initialising flavor object using setter injection");
this.flavor = flavor;
}
public Flavor getFlavor() {
return flavor;
}
public Topping getToppings() {
return toppings;
}
public void setToppings(Topping toppings) {
this.toppings = toppings;
}
@Override
public String toString() {
return "Cake [LOGGER=" + LOGGER + ", flavor=" + flavor + ", toppings=" + toppings + "]";
}
}

View File

@@ -0,0 +1,2 @@
spring.main.web-application-type=none
spring.main.banner-mode=off

View File

@@ -0,0 +1,25 @@
package com.example.test.constructorinjection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.example.ExampleApplicationCI;
import com.example.constructorinjection.Cake;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ExampleApplicationCI.class)
public class TestCakeClassConstructorInjection {
@Autowired
Cake cake;
@Test
public void testConstructorInjection() {
String testColor=cake.getFlavor().getColor();
Assert.assertEquals(testColor, " White ");
}
}

View File

@@ -0,0 +1,32 @@
package com.example.test.setterinjection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.example.ExampleApplicationSI;
import com.example.setterinjection.Cake;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ExampleApplicationSI.class)
public class TestCakeClassSetterInjection {
@Autowired
Cake cake;
@Test
public void testSetterInjection() {
String testColor = cake.getFlavor().getColor();
Assert.assertEquals(testColor, " White ");
String toppingsName=cake.getToppings().getToppingName();
//check if the dependency is not null
if(toppingsName!=null) {
Assert.assertEquals(toppingsName, "gems");
}
}
}