Skip to content

Commit fde8d56

Browse files
committed
add DI using java code without using xml configurations
1 parent 0b9946c commit fde8d56

File tree

10 files changed

+165
-0
lines changed

10 files changed

+165
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
car1.model = Tesla model 3
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.spring;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
import com.utilities.vehicle.Vehicle;
6+
7+
/*
8+
* For learning purposes:
9+
* 1. I use constructor injection using annotations to create bicycle bean
10+
* 2. I use setter injection using java code configuration to create car bean
11+
* but this is not best practice in this situation
12+
*/
13+
14+
public class App {
15+
16+
public static void main(String[] args) {
17+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
18+
JavaConfig.class);
19+
20+
Vehicle bicycle = applicationContext.getBean("bicycle", Vehicle.class);
21+
Vehicle car = applicationContext.getBean("car", Vehicle.class);
22+
23+
System.out.println("===============");
24+
25+
System.out.println(bicycle.getFullDescription());
26+
System.out.println(bicycle.getMechanicRole());
27+
28+
System.out.println("===============");
29+
30+
System.out.println(car.getFullDescription());
31+
System.out.println(car.getMechanicRole());
32+
33+
System.out.println("===============");
34+
35+
applicationContext.close();
36+
37+
}
38+
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.PropertySource;
7+
8+
import com.utilities.mechanic.CarMechanic;
9+
import com.utilities.mechanic.Mechanic;
10+
import com.utilities.vehicle.Car;
11+
import com.utilities.vehicle.Vehicle;
12+
13+
@Configuration
14+
@ComponentScan("com.utilities.mechanic")
15+
@ComponentScan("com.utilities.vehicle")
16+
@PropertySource("classpath:cars.properties")
17+
public class JavaConfig {
18+
19+
@Bean
20+
public Mechanic carMechanic() {
21+
return new CarMechanic();
22+
}
23+
24+
@Bean
25+
public Vehicle car() {
26+
Car carBean = new Car();
27+
carBean.setMechanic(carMechanic());
28+
return carBean;
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.utilities.mechanic;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class BicycleMechanic implements Mechanic {
7+
8+
@Override
9+
public String getRole() {
10+
return "I'm a bicycle mechanic";
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.utilities.mechanic;
2+
3+
public class CarMechanic implements Mechanic {
4+
5+
@Override
6+
public String getRole() {
7+
return "I'm a car mechanic";
8+
}
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.utilities.mechanic;
2+
3+
public interface Mechanic {
4+
String getRole();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.utilities.vehicle;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.stereotype.Component;
7+
8+
import com.utilities.mechanic.Mechanic;
9+
10+
@Component
11+
public class Bicycle implements Vehicle {
12+
13+
private double size;
14+
private Mechanic mechanic;
15+
16+
public Bicycle(@Value("15") double size, @Autowired @Qualifier("bicycleMechanic") Mechanic mechanic) {
17+
this.size = size;
18+
this.mechanic = mechanic;
19+
}
20+
21+
@Override
22+
public String getFullDescription() {
23+
return String.format("Bicycle size: %f", size);
24+
}
25+
26+
@Override
27+
public String getMechanicRole() {
28+
return mechanic.getRole();
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.utilities.vehicle;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
5+
import com.utilities.mechanic.Mechanic;
6+
7+
public class Car implements Vehicle {
8+
9+
@Value("${car1.model}")
10+
String model;
11+
Mechanic mechanic;
12+
13+
public void setMechanic(Mechanic mechanic) {
14+
this.mechanic = mechanic;
15+
}
16+
17+
@Override
18+
public String getFullDescription() {
19+
return String.format("Car Model: %s", model);
20+
}
21+
22+
@Override
23+
public String getMechanicRole() {
24+
return mechanic.getRole();
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.utilities.vehicle;
2+
3+
public interface Vehicle {
4+
String getFullDescription();
5+
6+
String getMechanicRole();
7+
}

0 commit comments

Comments
 (0)