Skip to content

Commit 7bb5fb6

Browse files
xamcrosspauljervis
authored andcommitted
BAEL-2051 Spring Autowiring of Generic Types (eugenp#4921)
1 parent 3d820aa commit 7bb5fb6

6 files changed

Lines changed: 158 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.dependencyinjectiontypes.annotation;
2+
3+
import org.springframework.beans.factory.annotation.Qualifier;
4+
5+
import java.lang.annotation.ElementType;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Target({ElementType.FIELD, ElementType.METHOD,
11+
ElementType.TYPE, ElementType.PARAMETER})
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Qualifier
14+
public @interface CarQualifier {
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.dependencyinjectiontypes.app;
2+
3+
import com.baeldung.dependencyinjectiontypes.annotation.CarQualifier;
4+
import com.baeldung.dependencyinjectiontypes.model.Car;
5+
import com.baeldung.dependencyinjectiontypes.model.CarHandler;
6+
import com.baeldung.dependencyinjectiontypes.model.Motorcycle;
7+
import com.baeldung.dependencyinjectiontypes.model.Vehicle;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.ComponentScan;
13+
14+
@SpringBootApplication
15+
@ComponentScan("com.baeldung.dependencyinjectiontypes.model")
16+
public class CustomConfiguration {
17+
@Bean
18+
@CarQualifier
19+
public Car getMercedes() {
20+
return new Car("E280", "Mercedes", "Diesel");
21+
}
22+
23+
public static void main(String[] args) throws NoSuchFieldException {
24+
ConfigurableApplicationContext context = SpringApplication.run(CustomConfiguration.class, args);
25+
CarHandler carHandler = context.getBean(CarHandler.class);
26+
carHandler.getVehicles().forEach(System.out::println);
27+
}
28+
29+
@Bean
30+
@CarQualifier
31+
public Car getBmw() {
32+
return new Car("M5", "BMW", "Petrol");
33+
}
34+
35+
@Bean
36+
public Motorcycle getSuzuki() {
37+
return new Motorcycle("Yamaguchi", "Suzuki", true);
38+
}
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.dependencyinjectiontypes.model;
2+
3+
public class Car extends Vehicle {
4+
private String engineType;
5+
6+
public Car(String name, String manufacturer, String engineType) {
7+
super(name, manufacturer);
8+
this.engineType = engineType;
9+
}
10+
11+
public String getEngineType() {
12+
return engineType;
13+
}
14+
15+
public void setEngineType(String engineType) {
16+
this.engineType = engineType;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "Car{" +
22+
"engineType='" + engineType + '\'' +
23+
'}';
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.dependencyinjectiontypes.model;
2+
3+
import com.baeldung.dependencyinjectiontypes.annotation.CarQualifier;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.core.ResolvableType;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.List;
9+
10+
@Component
11+
public class CarHandler {
12+
13+
@Autowired
14+
@CarQualifier
15+
private List<Vehicle> vehicles;
16+
17+
public List<Vehicle> getVehicles() throws NoSuchFieldException {
18+
ResolvableType vehiclesType = ResolvableType.forField(getClass().getDeclaredField("vehicles"));
19+
System.out.println(vehiclesType);
20+
ResolvableType type = vehiclesType.getGeneric();
21+
System.out.println(type);
22+
Class<?> aClass = type.resolve();
23+
System.out.println(aClass);
24+
return this.vehicles;
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.dependencyinjectiontypes.model;
2+
3+
public class Motorcycle extends Vehicle {
4+
private boolean twoWheeler;
5+
6+
public Motorcycle(String name, String manufacturer, boolean twoWheeler) {
7+
super(name, manufacturer);
8+
this.twoWheeler = true;
9+
}
10+
11+
public boolean isTwoWheeler() {
12+
return twoWheeler;
13+
}
14+
15+
public void setTwoWheeler(boolean twoWheeler) {
16+
this.twoWheeler = twoWheeler;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "Motorcycle{" +
22+
"twoWheeler=" + twoWheeler +
23+
'}';
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.dependencyinjectiontypes.model;
2+
3+
public abstract class Vehicle {
4+
private String name;
5+
private String manufacturer;
6+
7+
8+
public Vehicle(String name, String manufacturer) {
9+
this.name = name;
10+
this.manufacturer = manufacturer;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public String getManufacturer() {
22+
return manufacturer;
23+
}
24+
25+
public void setManufacturer(String manufacturer) {
26+
this.manufacturer = manufacturer;
27+
}
28+
}

0 commit comments

Comments
 (0)