Revert "Christopher Franklin Different Types of Bean Injection in Spring"
This reverts commit d76d39fb87892a38da04105836facefbc7face5c.
This commit is contained in:
parent
d76d39fb87
commit
4f7d19e86a
@ -1,16 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package com.baeldung.beaninjectiontypes;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
public class ExampleService {
|
|
||||||
|
|
||||||
public String getExampleText() {
|
|
||||||
return "Example";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user