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.
This commit is contained in:
Chris Franklin
2017-12-22 10:59:53 -05:00
parent 057b41029a
commit d76d39fb87
6 changed files with 123 additions and 0 deletions
@@ -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();
}
}
@@ -0,0 +1,11 @@
package com.baeldung.beaninjectiontypes;
import org.springframework.stereotype.Component;
@Component
public class ExampleService {
public String getExampleText() {
return "Example";
}
}
@@ -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();
}
}
@@ -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();
}
}
@@ -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();
}
}
@@ -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());
}
}