From e9efcb8e70f37e7488aa2371bb3ee62c676996f4 Mon Sep 17 00:00:00 2001 From: Shubhra Date: Sun, 1 Apr 2018 17:23:52 +0530 Subject: [PATCH] Types of Bean Injection in Spring --- .../constructorinjection/Address.java | 22 ++++++++++++ .../constructorinjection/Config.java | 16 +++++++++ .../constructorinjection/User.java | 21 +++++++++++ .../setterinjection/Address.java | 25 +++++++++++++ .../setterinjection/Config.java | 19 ++++++++++ .../setterinjection/User.java | 21 +++++++++++ .../resources/beaninjectiontypes-context.xml | 35 ++++++++++--------- .../ConstructorInjectionTest.java | 32 +++++++++++++++++ .../setterinjection/SetterInjectionTest.java | 32 +++++++++++++++++ 9 files changed, 206 insertions(+), 17 deletions(-) create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/Address.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/User.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/Address.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/User.java create mode 100644 spring-core/src/test/java/com/baeldung/beaninjectiontypes/constructorinjection/ConstructorInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/beaninjectiontypes/setterinjection/SetterInjectionTest.java diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/Address.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/Address.java new file mode 100644 index 0000000000..cb2c929825 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/Address.java @@ -0,0 +1,22 @@ +package com.baeldung.beaninjectiontypes.constructorinjection; + +import org.springframework.stereotype.Component; + +@Component +public class Address { + + private String city; + + private String country; + + public Address(String city, String country) { + this.city = city; + this.country = country; + } + + @Override + public String toString() { + return String.format("%s, %s", city, country); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/Config.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/Config.java new file mode 100644 index 0000000000..8a8e52104e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/Config.java @@ -0,0 +1,16 @@ +package com.baeldung.beaninjectiontypes.constructorinjection; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.beaninjectiontypes.constructorinjection") +public class Config { + + @Bean + public Address address() { + return new Address("New York City", "USA"); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/User.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/User.java new file mode 100644 index 0000000000..208ce922b2 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/constructorinjection/User.java @@ -0,0 +1,21 @@ +package com.baeldung.beaninjectiontypes.constructorinjection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class User { + + private Address address; + + @Autowired + public User(Address address) { + this.address = address; + } + + @Override + public String toString() { + return String.format("User Address: %s", address); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/Address.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/Address.java new file mode 100644 index 0000000000..ac9e330bf5 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/Address.java @@ -0,0 +1,25 @@ +package com.baeldung.beaninjectiontypes.setterinjection; + +import org.springframework.stereotype.Component; + +@Component +public class Address { + + private String city; + + private String country; + + public void setCity(String city) { + this.city = city; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public String toString() { + return String.format("%s, %s", city, country); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/Config.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/Config.java new file mode 100644 index 0000000000..2d530b7ea1 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/Config.java @@ -0,0 +1,19 @@ +package com.baeldung.beaninjectiontypes.setterinjection; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.beaninjectiontypes.setterinjection") +public class Config { + + @Bean + public Address address() { + Address address = new Address(); + address.setCity("Los Angeles"); + address.setCountry("USA"); + return address; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/User.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/User.java new file mode 100644 index 0000000000..90aeb24335 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/setterinjection/User.java @@ -0,0 +1,21 @@ +package com.baeldung.beaninjectiontypes.setterinjection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class User { + + private Address address; + + @Autowired + public void setAddress(Address address) { + this.address = address; + } + + @Override + public String toString() { + return String.format("User Address : %s", address); + } + +} diff --git a/spring-core/src/main/resources/beaninjectiontypes-context.xml b/spring-core/src/main/resources/beaninjectiontypes-context.xml index dfdea41cdc..4332c58014 100644 --- a/spring-core/src/main/resources/beaninjectiontypes-context.xml +++ b/spring-core/src/main/resources/beaninjectiontypes-context.xml @@ -1,22 +1,23 @@ - - + - - - + + + - - - + + + - - - - - + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/constructorinjection/ConstructorInjectionTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/constructorinjection/ConstructorInjectionTest.java new file mode 100644 index 0000000000..e643de4310 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/constructorinjection/ConstructorInjectionTest.java @@ -0,0 +1,32 @@ +package com.baeldung.beaninjectiontypes.constructorinjection; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class ConstructorInjectionTest { + + @Test + public void givenXMLConfiguration_WhenConstructorArgPassed_ThenDependencyValid() { + ApplicationContext context = new ClassPathXmlApplicationContext("beaninjectiontypes-context.xml"); + User user = (User) context.getBean("userWithCI"); + + String expectedOutput = "User Address: California, USA"; + + assertTrue(expectedOutput.equals(user.toString())); + } + + @Test + public void givenPOJOConfiguration_WhenConstructorArgPassed_ThenDependencyValid() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + User user = (User) context.getBean(User.class); + + String expectedOutput = "User Address: New York City, USA"; + + assertTrue(expectedOutput.equals(user.toString())); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/setterinjection/SetterInjectionTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/setterinjection/SetterInjectionTest.java new file mode 100644 index 0000000000..bcaab9da44 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/setterinjection/SetterInjectionTest.java @@ -0,0 +1,32 @@ +package com.baeldung.beaninjectiontypes.setterinjection; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SetterInjectionTest { + + @Test + public void givenXMLConfiguration_WhenSetterArgPassed_ThenDependencyValid() { + ApplicationContext context = new ClassPathXmlApplicationContext("beaninjectiontypes-context.xml"); + User user = (User) context.getBean("userWithSI"); + + String expectedOutput = "User Address : Washington D.C., USA"; + + assertTrue(expectedOutput.equals(user.toString())); + } + + @Test + public void givenPOJOConfiguration_WhenSetterArgPassed_ThenDependencyValid() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + User user = (User) context.getBean(User.class); + + String expectedOutput = "User Address : Los Angeles, USA"; + + assertTrue(expectedOutput.equals(user.toString())); + } + +}