BAEL-2051 Spring Autowiring of Generic Types (#4921)

This commit is contained in:
xamcross 2018-08-26 15:22:07 +03:00 committed by pauljervis
parent 3d820aaea4
commit 7bb5fb6176
6 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package com.baeldung.dependencyinjectiontypes.annotation;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.METHOD,
ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface CarQualifier {
}

View File

@ -0,0 +1,39 @@
package com.baeldung.dependencyinjectiontypes.app;
import com.baeldung.dependencyinjectiontypes.annotation.CarQualifier;
import com.baeldung.dependencyinjectiontypes.model.Car;
import com.baeldung.dependencyinjectiontypes.model.CarHandler;
import com.baeldung.dependencyinjectiontypes.model.Motorcycle;
import com.baeldung.dependencyinjectiontypes.model.Vehicle;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.baeldung.dependencyinjectiontypes.model")
public class CustomConfiguration {
@Bean
@CarQualifier
public Car getMercedes() {
return new Car("E280", "Mercedes", "Diesel");
}
public static void main(String[] args) throws NoSuchFieldException {
ConfigurableApplicationContext context = SpringApplication.run(CustomConfiguration.class, args);
CarHandler carHandler = context.getBean(CarHandler.class);
carHandler.getVehicles().forEach(System.out::println);
}
@Bean
@CarQualifier
public Car getBmw() {
return new Car("M5", "BMW", "Petrol");
}
@Bean
public Motorcycle getSuzuki() {
return new Motorcycle("Yamaguchi", "Suzuki", true);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.dependencyinjectiontypes.model;
public class Car extends Vehicle {
private String engineType;
public Car(String name, String manufacturer, String engineType) {
super(name, manufacturer);
this.engineType = engineType;
}
public String getEngineType() {
return engineType;
}
public void setEngineType(String engineType) {
this.engineType = engineType;
}
@Override
public String toString() {
return "Car{" +
"engineType='" + engineType + '\'' +
'}';
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.dependencyinjectiontypes.model;
import com.baeldung.dependencyinjectiontypes.annotation.CarQualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class CarHandler {
@Autowired
@CarQualifier
private List<Vehicle> vehicles;
public List<Vehicle> getVehicles() throws NoSuchFieldException {
ResolvableType vehiclesType = ResolvableType.forField(getClass().getDeclaredField("vehicles"));
System.out.println(vehiclesType);
ResolvableType type = vehiclesType.getGeneric();
System.out.println(type);
Class<?> aClass = type.resolve();
System.out.println(aClass);
return this.vehicles;
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.dependencyinjectiontypes.model;
public class Motorcycle extends Vehicle {
private boolean twoWheeler;
public Motorcycle(String name, String manufacturer, boolean twoWheeler) {
super(name, manufacturer);
this.twoWheeler = true;
}
public boolean isTwoWheeler() {
return twoWheeler;
}
public void setTwoWheeler(boolean twoWheeler) {
this.twoWheeler = twoWheeler;
}
@Override
public String toString() {
return "Motorcycle{" +
"twoWheeler=" + twoWheeler +
'}';
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.dependencyinjectiontypes.model;
public abstract class Vehicle {
private String name;
private String manufacturer;
public Vehicle(String name, String manufacturer) {
this.name = name;
this.manufacturer = manufacturer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
}