Skip to content

Commit c174946

Browse files
committed
Code for Alessio Stalla's evaluation article (Different Types of Bean Injection in Spring)
1 parent e99619d commit c174946

7 files changed

Lines changed: 201 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.typesofbeaninjection;
2+
3+
import com.baeldung.typesofbeaninjection.domain.Engine;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
@ComponentScan("com.baeldung.typesofbeaninjection")
10+
public class Config {
11+
12+
@Bean
13+
public Engine engine() {
14+
return new Engine("V8", 5);
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.typesofbeaninjection.domain;
2+
3+
public class Car {
4+
private Engine engine;
5+
6+
public Car() {}
7+
8+
public Car(Engine engine) {
9+
this.engine = engine;
10+
}
11+
12+
public Engine getEngine() {
13+
return engine;
14+
}
15+
16+
public void setEngine(Engine engine) {
17+
this.engine = engine;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return String.format("Car with %s engine", engine);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.typesofbeaninjection.domain;
2+
3+
public class Engine {
4+
private final String type;
5+
private final int volume;
6+
7+
public Engine(String type, int volume) {
8+
this.type = type;
9+
this.volume = volume;
10+
}
11+
12+
public String getType() {
13+
return type;
14+
}
15+
16+
public int getVolume() {
17+
return volume;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return String.format("%s %d", type, volume);
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.typesofbeaninjection.domain.autowired.constructor;
2+
3+
import com.baeldung.typesofbeaninjection.domain.Engine;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component("car-autowired-by-constructor")
8+
public class Car {
9+
private Engine engine;
10+
11+
public Car() {}
12+
13+
@Autowired
14+
public Car(Engine engine) {
15+
this.engine = engine;
16+
}
17+
18+
public Engine getEngine() {
19+
return engine;
20+
}
21+
22+
public void setEngine(Engine engine) {
23+
this.engine = engine;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return String.format("Car with %s engine", engine);
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.typesofbeaninjection.domain.autowired.properties;
2+
3+
import com.baeldung.typesofbeaninjection.domain.Engine;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component("car-autowired-by-properties")
8+
public class Car {
9+
private Engine engine;
10+
11+
public Car() {}
12+
13+
public Car(Engine engine) {
14+
this.engine = engine;
15+
}
16+
17+
public Engine getEngine() {
18+
return engine;
19+
}
20+
21+
@Autowired
22+
public void setEngine(Engine engine) {
23+
this.engine = engine;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return String.format("Car with %s engine", engine);
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://www.springframework.org/schema/beans"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
6+
<bean name="engine" class="com.baeldung.typesofbeaninjection.domain.Engine">
7+
<constructor-arg value="V8" />
8+
<constructor-arg value="5" />
9+
</bean>
10+
11+
<bean name="alices-car" class="com.baeldung.typesofbeaninjection.domain.Car">
12+
<constructor-arg ref="engine" />
13+
</bean>
14+
15+
<bean name="bobs-car" class="com.baeldung.typesofbeaninjection.domain.Car">
16+
<property name="engine" ref="engine" />
17+
</bean>
18+
19+
</beans>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.typesofbeaninjection;
2+
3+
import com.baeldung.typesofbeaninjection.domain.Car;
4+
import com.baeldung.typesofbeaninjection.domain.Engine;
5+
import org.junit.Test;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.support.ClassPathXmlApplicationContext;
9+
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotNull;
12+
13+
public class TypeOfBeanInjectionUnitTest {
14+
15+
@Test
16+
public void whenConstructionInjectionInXML_thenCarHasEngine() {
17+
ApplicationContext applicationContext
18+
= new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml");
19+
Car car = applicationContext.getBean("alices-car", Car.class);
20+
21+
checkEngine(car.getEngine());
22+
}
23+
24+
@Test
25+
public void whenPropertyInjectionInXML_thenCarHasEngine() {
26+
ApplicationContext applicationContext
27+
= new ClassPathXmlApplicationContext("/typesofbeaninjection-context.xml");
28+
Car car = applicationContext.getBean("bobs-car", Car.class);
29+
30+
checkEngine(car.getEngine());
31+
}
32+
33+
@Test
34+
public void whenConstructionInjectionAnnotations_thenCarHasEngine() {
35+
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
36+
com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car car
37+
= applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.constructor.Car.class);
38+
39+
checkEngine(car.getEngine());
40+
}
41+
42+
@Test
43+
public void whenPropertyInjectionAnnotations_thenCarHasEngine() {
44+
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
45+
com.baeldung.typesofbeaninjection.domain.autowired.properties.Car car
46+
= applicationContext.getBean(com.baeldung.typesofbeaninjection.domain.autowired.properties.Car.class);
47+
48+
checkEngine(car.getEngine());
49+
}
50+
51+
private void checkEngine(Engine engine) {
52+
assertNotNull(engine);
53+
assertEquals("V8", engine.getType());
54+
assertEquals(5, engine.getVolume());
55+
}
56+
57+
58+
}

0 commit comments

Comments
 (0)