Мне пришлось использовать общий класс dao для всех распространенных методов гибернации, таких как сохранение, обновление, поиск и т. Д. Для этого я создал реализацию BaseDao, и этот класс расширяется всеми реализациями childDao. Но когда приложение запускается, оно выдает NoUniqueBeanDefinitionException
,Я использовал аннотацию @Qualifier
, чтобы устранить эту проблему, она не работала. Возможно, из-за моего опыта использования этой аннотации, пожалуйста, предоставьте решение этой проблемы.
@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;
}
и второй childDao, который расширяетBaseDao равен
@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);
}
, исключение составляет
Dec 11, 2018 9:49:26 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deviceRestController': Unsatisfied dependency expressed through field 'masterDevice'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'masterDeviceServiceImpl': Unsatisfied dependency expressed through field 'baseDao'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.childCar.dao.BaseDao' available: expected single matching bean but found 2: masterDeviceDaoImpl,usersDaoImpl
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4743)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5207)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'masterDeviceServiceImpl': Unsatisfied dependency expressed through field 'baseDao'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.childCar.dao.BaseDao' available: expected single matching bean but found 2: masterDeviceDaoImpl,usersDaoImpl
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
... 24 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.childCar.dao.BaseDao' available: expected single matching bean but found 2: masterDeviceDaoImpl,usersDaoImpl
at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:215)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1113)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
... 37 more