我正在开发一个Spring 3.1 MVC应用程序,对于我的一个场景,我不得不编写两个DAO实现.我想知道如何基于另一个对象的属性在服务层中自动装配它.
例如,
class Vehicle {
private name;
private type;
..
..
..
}
@Service
class VehicleServiceImpl implements VehicleService {
// There are two implementations to this DAO
// if Vehicle.type == "CAR", inject CarDAO
// if Vehicle.type == "TRAIN", inject TrainDAO
@Autowired
private VehicleDAO vehicleDAO ;
}
@Repository
class CarDAO implements VehicleDAO {
}
@Repository
class TrainDAO implements VehicleDAO {
}
Run Code Online (Sandbox Code Playgroud)
如果我的车辆是汽车,我需要自动装载CarDAO,如果是火车,我需要自动装载TrainDAO
在3.1版本中实现这一点的最佳方法是什么?
我希望使用上下文属性占位符或@Qualifier注释,但这些都是基于某些属性限制查找.我不知道如何在运行时基于另一个对象的属性执行此操作.