Spring 3 - 基于另一个对象属性的运行时动态自动装配 [英] Spring 3 - Dynamic Autowiring at runtime based on another object attribute
问题描述
中自动连接。
例如,
class Vehicle {
private name;
私有类型;
..
..
..
}
@Service
class VehicleServiceImpl实现VehicleService {
//这个DAO
有两个实现//如果Vehicle.type ==CAR,注入CarDAO
//如果Vehicle.type ==TRAIN,则注入TrainDAO
@自动连线
私人车辆
}
@Repository
class CarDAO实现VehicleDAO {
}
@Repository
类TrainDAO实施车辆代码{
}
如果我的车是一辆车我需要自动连线CarDAO,如果是火车,我需要自动连线TrainDAO
在春季3.1中实现这一点的最好方法是什么?
我希望使用上下文属性占位符或@Qualifier注释,但这两种都是基于某些属性进行查找的限制。
我不知道如何在运行时基于另一个对象的属性来执行此操作。
我的解决方案将如下所示:
一个方法isResponsibleFor在VehicleDao界面中:
界面VehicleDao {
public boolean isResponsibleFor(Vehicle vehicle);
}
示例实现:
@Repository
class CarDAO实现了VehicleDAO {
public boolean isResponsibleFor(Vehicle vehicle){
returnCAR.equals(vehicle.getType() );
}
}
然后autowire a 车辆服务:
public class VehicleServiceImpl实现VehicleService {
@Autowired
private List& VehicleDao> vehicleDaos;
私人车辆DaoForVehicle(车辆车辆){
foreach(VehicleDao vehicleDao:vehicleDaos){
if(vehicleDao.isResponsibleFor(vehicle){
return vehicleDao;
$
throw new UnsupportedOperationException(unsupported vehicleType);
}
@Transactional
public void save车辆){
daoForVehicle(车辆).save(车辆);
}
}
这样做的好处是,您在以后添加新的车辆类型时不需要修改服务 - 您只需要添加一个新的VehicleDao实现。
I am working on a Spring 3.1 MVC application and for one of my scenarios, I had to write two implementations of a DAO. I would like to know how to autowire this in a service layer based on another object's attribute.
For example,
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 {
}
If my Vehicle is a Car, I need to autowire CarDAO and if it's a train, I need to autowire TrainDAO
What is the best way to implement this in spring 3.1.
I was hoping to use either context property place holders or @Qualifier annotation but both these are kind of restricted to lookup based on some property. I am not sure how to do this at runtime based on the property of another object.
My solution would be as follows:
a method isResponsibleFor in the VehicleDao interface:
interface VehicleDao {
public boolean isResponsibleFor(Vehicle vehicle);
}
example implementation:
@Repository
class CarDAO implements VehicleDAO {
public boolean isResponsibleFor(Vehicle vehicle) {
return "CAR".equals(vehicle.getType());
}
}
then autowire a list of all VehicleDao-implementations in the VehicleService:
public class VehicleServiceImpl implements VehicleService {
@Autowired
private List<VehicleDao> vehicleDaos;
private VehicleDao daoForVehicle(Vehicle vehicle) {
foreach(VehicleDao vehicleDao : vehicleDaos) {
if(vehicleDao.isResponsibleFor(vehicle) {
return vehicleDao;
}
}
throw new UnsupportedOperationException("unsupported vehicleType");
}
@Transactional
public void save(Vehicle vehicle) {
daoForVehicle(vehicle).save(vehicle);
}
}
This has the advantage that you don't need to modify the service when you are adding a new vehicleType at a later time - you just need to add a new VehicleDao-implementation.
这篇关于Spring 3 - 基于另一个对象属性的运行时动态自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!