From d76d39fb87892a38da04105836facefbc7face5c Mon Sep 17 00:00:00 2001 From: Chris Franklin Date: Fri, 22 Dec 2017 10:59:53 -0500 Subject: [PATCH] Christopher Franklin Different Types of Bean Injection in Spring My first article about the three different types of bean injection supported by the Spring Framework. This code walks through all three methods and has supporting tests. --- .../baeldung/beaninjectiontypes/Config.java | 16 ++++++++ .../beaninjectiontypes/ExampleService.java | 11 +++++ .../ExampleWithConstructorInjection.java | 19 +++++++++ .../ExampleWithPropertyInjection.java | 16 ++++++++ .../ExampleWithSetterInjection.java | 20 +++++++++ .../beaninjectiontypes/BeanInjectionTest.java | 41 +++++++++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java create mode 100644 spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java create mode 100644 spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java new file mode 100644 index 0000000000..d523dc3f1f --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/Config.java @@ -0,0 +1,16 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("com.baeldung.beaninjectiontypes") +public class Config { + + @Bean + public ExampleService exampleService() { + return new ExampleService(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java new file mode 100644 index 0000000000..9112fee577 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleService.java @@ -0,0 +1,11 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.stereotype.Component; + +@Component +public class ExampleService { + + public String getExampleText() { + return "Example"; + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java new file mode 100644 index 0000000000..340d283dbd --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithConstructorInjection.java @@ -0,0 +1,19 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ExampleWithConstructorInjection { + + private ExampleService exampleService; + + @Autowired + public ExampleWithConstructorInjection(ExampleService exampleService) { + this.exampleService = exampleService; + } + + public String verifyInjection() { + return this.exampleService.getExampleText(); + } +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java new file mode 100644 index 0000000000..d9495e5c05 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithPropertyInjection.java @@ -0,0 +1,16 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ExampleWithPropertyInjection { + + @Autowired + private ExampleService exampleService; + + public String verifyInjection() { + return this.exampleService.getExampleText(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java new file mode 100644 index 0000000000..97f1355ec5 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/beaninjectiontypes/ExampleWithSetterInjection.java @@ -0,0 +1,20 @@ +package com.baeldung.beaninjectiontypes; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ExampleWithSetterInjection { + + private ExampleService exampleService; + + @Autowired + public void setExampleService(ExampleService exampleService) { + this.exampleService = exampleService; + } + + public String verifyInjection() { + return this.exampleService.getExampleText(); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java new file mode 100644 index 0000000000..ae609d5df5 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/beaninjectiontypes/BeanInjectionTest.java @@ -0,0 +1,41 @@ +package com.baeldung.beaninjectiontypes; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = Config.class) +public class BeanInjectionTest { + + @Test + public void whenConstructorInjection_ThenBeanValid() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + ExampleWithConstructorInjection exampleBean = context.getBean(ExampleWithConstructorInjection.class); + + assertEquals("Example", exampleBean.verifyInjection()); + } + + @Test + public void whenSetterInjection_ThenBeanValid() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + ExampleWithSetterInjection exampleBean = context.getBean(ExampleWithSetterInjection.class); + + assertEquals("Example", exampleBean.verifyInjection()); + } + + @Test + public void whenProperyInjection_ThenBeanValid() { + ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); + ExampleWithPropertyInjection exampleBean = context.getBean(ExampleWithPropertyInjection.class); + + assertEquals("Example", exampleBean.verifyInjection()); + } + +}