Code for Alessio Stalla's evaluation article (Different Types of Bean Injection in Spring)
This commit is contained in:
parent
e99619dcdc
commit
c174946f85
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baeldung.typesofbeaninjection;
|
||||||
|
|
||||||
|
import com.baeldung.typesofbeaninjection.domain.Engine;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan("com.baeldung.typesofbeaninjection")
|
||||||
|
public class Config {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Engine engine() {
|
||||||
|
return new Engine("V8", 5);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.typesofbeaninjection.domain;
|
||||||
|
|
||||||
|
public class Car {
|
||||||
|
private Engine engine;
|
||||||
|
|
||||||
|
public Car() {}
|
||||||
|
|
||||||
|
public Car(Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Engine getEngine() {
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEngine(Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("Car with %s engine", engine);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.typesofbeaninjection.domain;
|
||||||
|
|
||||||
|
public class Engine {
|
||||||
|
private final String type;
|
||||||
|
private final int volume;
|
||||||
|
|
||||||
|
public Engine(String type, int volume) {
|
||||||
|
this.type = type;
|
||||||
|
this.volume = volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getVolume() {
|
||||||
|
return volume;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("%s %d", type, volume);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.typesofbeaninjection.domain.autowired.constructor;
|
||||||
|
|
||||||
|
import com.baeldung.typesofbeaninjection.domain.Engine;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component("car-autowired-by-constructor")
|
||||||
|
public class Car {
|
||||||
|
private Engine engine;
|
||||||
|
|
||||||
|
public Car() {}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public Car(Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Engine getEngine() {
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEngine(Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("Car with %s engine", engine);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.typesofbeaninjection.domain.autowired.properties;
|
||||||
|
|
||||||
|
import com.baeldung.typesofbeaninjection.domain.Engine;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component("car-autowired-by-properties")
|
||||||
|
public class Car {
|
||||||
|
private Engine engine;
|
||||||
|
|
||||||
|
public Car() {}
|
||||||
|
|
||||||
|
public Car(Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Engine getEngine() {
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public void setEngine(Engine engine) {
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return String.format("Car with %s engine", engine);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://www.springframework.org/schema/beans"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||||
|
|
||||||
|
<bean name="engine" class="com.baeldung.typesofbeaninjection.domain.Engine">
|
||||||
|
<constructor-arg value="V8" />
|
||||||
|
<constructor-arg value="5" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean name="alices-car" class="com.baeldung.typesofbeaninjection.domain.Car">
|
||||||
|
<constructor-arg ref="engine" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
<bean name="bobs-car" class="com.baeldung.typesofbeaninjection.domain.Car">
|
||||||
|
<property name="engine" ref="engine" />
|
||||||
|
</bean>
|
||||||
|
|
||||||
|
</beans>
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.baeldung.typesofbeaninjection;
|
||||||
|
|
||||||
|
import com.baeldung.typesofbeaninjection.domain.Car;
|
||||||
|
import com.baeldung.typesofbeaninjection.domain.Engine;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
public class TypeOfBeanInjectionUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenConstructionInjectionInXML_thenCarHasEngine() {
|
||||||
|
ApplicationContext applicationContext
|
||||||
|
= new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml");
|
||||||
|
Car car = applicationContext.getBean("alices-car", Car.class);
|
||||||
|
|
||||||
|
checkEngine(car.getEngine());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPropertyInjectionInXML_thenCarHasEngine() {
|
||||||
|
ApplicationContext applicationContext
|
||||||
|
= new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml");
|
||||||
|
Car car = applicationContext.getBean("bobs-car", Car.class);
|
||||||
|
|
||||||
|
checkEngine(car.getEngine());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenConstructionInjectionAnnotations_thenCarHasEngine() {
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
|
||||||
|
com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car car
|
||||||
|
= applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car.class);
|
||||||
|
|
||||||
|
checkEngine(car.getEngine());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPropertyInjectionAnnotations_thenCarHasEngine() {
|
||||||
|
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
|
||||||
|
com.baeldung.typesofbeaninjection.domain.autowired.properties.Car car
|
||||||
|
= applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.properties.Car.class);
|
||||||
|
|
||||||
|
checkEngine(car.getEngine());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkEngine(Engine engine) {
|
||||||
|
assertNotNull(engine);
|
||||||
|
assertEquals("V8", engine.getType());
|
||||||
|
assertEquals(5, engine.getVolume());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue