我必须对所有常见的休眠方法(例如保存,更新,查找等)使用公共的dao类。为此,我创建了一个BaseDao实现,并且所有childDao实现都对该类进行了扩展。但是当应用程序运行时,它抛出{{1 }}。
我使用了NoUniqueBeanDefinitionException
注释来解决此问题,但没有用。可能以我的经验较少,请为该问题提供解决方案。
@Qualifier
和扩展BaseDao的第二个childDao是
@RestController
public class DeviceRestController {
@Autowired
EncryptionUtils encryptionUtils;
@Autowired
private Environment env;
@Autowired
MasterDeviceServiceImpl masterDevice;
....}
@Service
@Transactional
public class MasterDeviceServiceImpl extends BaseServiceImpl implements MasterDeviceService {
@Autowired
MasterDeviceDao masterDeviceDao;
public List<BatchDetails> getAllBatchDetails() {
return masterDeviceDao.getAllBatchDetails();
}
public void addMasterDeviceDetails(MasterDevices masterDevice) {
masterDeviceDao.addMasterDeviceDetails(masterDevice);
}
@Service
public interface MasterDeviceService {
public List<BatchDetails> getAllBatchDetails();
public void addMasterDeviceDetails(MasterDevices masterDevice);
public List<MasterDevices> getAllMasterDevicesDetails();
public List<MasterDevices> getMasterDevicesDetails(String deviceKey);
}
public interface MasterDeviceDao {
public List<BatchDetails> getAllBatchDetails();
public void addMasterDeviceDetails(MasterDevices masterDevice);
public List<MasterDevices> getAllMasterDevicesDetails();
public List<MasterDevices> getMasterDevicesDetails(String deviceKey);
}
@Repository
public class MasterDeviceDaoImpl extends BaseDaoImpl implements MasterDeviceDao {
@Autowired
private HibernateTemplate template;
@Autowired
SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public List<BatchDetails> getAllBatchDetails() {
List<BatchDetails> batchDetails = template.loadAll(BatchDetails.class);
return batchDetails;
}
基类实现是
@RestController
@PropertySource({ "classpath:childCar.properties" })
public class GeneralRestController {
@Autowired
private Environment env;
@Autowired
EncryptionUtils encryptionUtils;
@Autowired
DateTimeUtils dateTimeUtils;
@Autowired
CountryService countryService;
@Autowired
UsersService usersService;
.....}
@Service
public interface UsersService {
public List<Countries> getAllCountries();
public List<Countries> getAllCountriesbBydate(Timestamp date);
public Users getUserDetails(int id);
.........}
public interface UsersDao {
public List<Countries> getAllCountries();
public List<Countries> getAllCountriesbBydate(Timestamp date);
public Users getUserDetails(int id);
.............}
@Service
@Transactional
public class UsersServiceImpl extends BaseServiceImpl implements UsersService{
@Autowired
UsersDaoImpl usersDao;
public List<Countries> getAllCountries() {
return usersDao.getAllCountries();
}
public List<Countries> getAllCountriesbBydate(Timestamp date) {
return usersDao.getAllCountriesbBydate(date);
}
..............}
@Repository
public class UsersDaoImpl extends BaseDaoImpl implements UsersDao {
@Autowired
private HibernateTemplate template;
@Autowired
SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public List<Countries> getAllCountries() {
return template.loadAll(Countries.class);
}
.....}
例外是
@Repository
public abstract class BaseDaoImpl implements BaseDao {
@Autowired
private HibernateTemplate template;
@Autowired
SessionFactory sessionFactory;
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public <T> void updateObject(T entity) {
getSession().update(entity);
}
.........}
public interface BaseDao {
public <T> void updateObject(T entity);
public <T> List<?> getObject(Class<?> s, int id);
public void saveObject(Object obj);
}
@Service
@Transactional
public abstract class BaseServiceImpl implements BaseService {
@Autowired
BaseDao baseDao;
public <T> void updateObject(T entity) {
baseDao.updateObject(entity);
}
public <T> List<?> getObject(Class s, int id) {
return baseDao.getObject(s, id);
}
public void saveObject(Object obj) {
baseDao.saveObject(obj);
}
}
@Service
public interface BaseService {
public <T> void updateObject(T entity);
public <T> List<?> getObject(Class s, int id);
public void saveObject(Object obj);
}