Code example for field injection and other changes

This commit is contained in:
Vasudha Venkatesan
2020-03-10 10:55:35 +05:30
committed by akuksin
parent 670c4c845a
commit 4b959cb797
14 changed files with 205 additions and 43 deletions

View File

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

View File

@@ -0,0 +1,19 @@
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.fieldinjection.IceCream;
@SpringBootApplication(scanBasePackages = { "com.example.dependency", "com.example.fieldinjection" })
public class ExampleApplicationFI {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(ExampleApplicationSI.class, args);
IceCream obj = context.getBean(IceCream.class);
System.out.println("IceCream : " + obj.toString());
}
}

View File

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

View File

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

View File

@@ -0,0 +1,32 @@
package com.example.constructorinjection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.dependency.Bread;
import com.example.dependency.Topping;
@Component
public class Sandwich {
Topping toppings;
Bread breadType;
Sandwich(Topping toppings) {
this.toppings = toppings;
}
@Autowired
Sandwich(Topping toppings, Bread breadType) {
this.toppings = toppings;
this.breadType = breadType;
}
public Topping getToppings() {
return toppings;
}
public Bread getBreadType() {
return breadType;
}
}

View File

@@ -0,0 +1,17 @@
package com.example.dependency;
import org.springframework.stereotype.Component;
@Component
public class Bread {
String breadType;
public String getBreadType() {
return breadType;
}
void setBreadType(String breadType) {
this.breadType = breadType;
}
}

View File

@@ -5,8 +5,8 @@ import org.springframework.stereotype.Component;
@Component @Component
public class Flavor { public class Flavor {
String flavorType = " Chocolate "; String flavorType;
String color = " White "; String color;
public String getFlavorType() { public String getFlavorType() {
return flavorType; return flavorType;
@@ -21,5 +21,4 @@ public class Flavor {
return "Flavor [flavorType=" + flavorType + ", color=" + color + "]"; return "Flavor [flavorType=" + flavorType + ", color=" + color + "]";
} }
} }

View File

@@ -0,0 +1,26 @@
package com.example.dependency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Pizza {
@Autowired
Topping toppings;
Pizza(Topping toppings) {
this.toppings = toppings;
}
@Autowired
public Topping getToppings() {
System.out.println("Using field injection - " + this.toppings);
return toppings;
}
public void setToppings(Topping toppings) {
this.toppings = toppings;
}
}

View File

@@ -5,13 +5,13 @@ import org.springframework.stereotype.Component;
@Component @Component
public class Topping { public class Topping {
private String toppingName; String toppingName;
public String getToppingName() { public String getToppingName() {
return toppingName; return toppingName;
} }
public void setToppingName(String toppingName) { void setToppingName(String toppingName) {
this.toppingName = toppingName; this.toppingName = toppingName;
} }
@@ -20,5 +20,4 @@ public class Topping {
return "Topping [toppingName=" + toppingName + "]"; return "Topping [toppingName=" + toppingName + "]";
} }
} }

View File

@@ -0,0 +1,27 @@
package com.example.fieldinjection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.dependency.Topping;
@Component
public class IceCream {
@Autowired
Topping toppings;
public Topping getToppings() {
return toppings;
}
void setToppings(Topping toppings) {
this.toppings = toppings;
}
@Override
public String toString() {
return "IceCream [toppings=" + toppings + "]";
}
}

View File

@@ -0,0 +1,27 @@
package com.example.setterinjection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.dependency.Topping;
@Component
public class Cookie {
private Topping toppings;
@Autowired
void setTopping(Topping toppings) {
this.toppings = toppings;
}
public Topping getTopping() {
return toppings;
}
@Override
public String toString() {
return "Cookie [toppings=" + toppings + "]";
}
}

View File

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

View File

@@ -3,7 +3,7 @@ package com.example.test.constructorinjection;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.mockito.InjectMocks;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -14,12 +14,15 @@ import com.example.constructorinjection.Cake;
@SpringBootTest(classes = ExampleApplicationCI.class) @SpringBootTest(classes = ExampleApplicationCI.class)
public class TestCakeClassConstructorInjection { public class TestCakeClassConstructorInjection {
@Autowired // @Mock
// Flavor flavor;
@InjectMocks
Cake cake; Cake cake;
@Test @Test
public void testConstructorInjection() { public void testConstructorInjection() {
String testColor = cake.getFlavor().getColor(); System.out.println(cake.toString());
Assert.assertEquals(testColor, " White "); Assert.assertNotNull(cake.getFlavor());
} }
} }

View File

@@ -0,0 +1,25 @@
package com.example.test.fieldinjection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.example.ExampleApplicationSI;
import com.example.fieldinjection.IceCream;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ExampleApplicationSI.class)
public class TestCookieClassFieldInjection {
@InjectMocks
IceCream iceCream;
@Test
public void testSetterInjection() {
System.out.println(iceCream.toString());
Assert.assertNotNull(iceCream.getToppings());
}
}