1 year ago
#273322

Ravi
Spring framework customize ServiceLocatorFactoryBean with complex type
I am trying to customize the way we use service locator pattern to have more complex type
@Configuration
public class VehicleConfig {
@Bean
public FactoryBean<?> factoryBean() {
final ServiceLocatorFactoryBean bean = new ServiceLocatorFactoryBean();
bean.setServiceLocatorInterface(ServiceRegistry.class);
return bean;
}
}
public interface AdapterService<T> {
public void process(T request);
}
public interface ServiceRegistry {
public <T> AdapterService<T> getService(String serviceName);
}
@RestController
@RequestMapping("/vehicle")
public class VehicleController {
@Autowired
private ServiceRegistry serviceRegistry;
@PostMapping
public void processStudentDetails(@RequestBody Vehicle vehicle) {
serviceRegistry.getService(vehicle.getVehicleType()).process(vehicle);
}
}
@Service("Bike")
public class BikeService implements AdapterService<Vehicle> {
@Override
public void process(Vehicle request) {
System.out.println("inside bike service - " + request.toString());
}
}
public class Vehicle implements Serializable {
private String vehicleName;
private String vehicleType;
}
In the above code i want to have more complex vehicle type
serviceRegistry.getService(vehicle.getVehicleType()).process(vehicle);
What is the best way to do it using Spring framework?
java
spring
service-locator
spring-framework-beans
0 Answers
Your Answer