所有都在不同的包中。 我使用的是spring-boot 1.4。 自动装配的Dao接口如何直接调用DaoImpl方法? 在java中,接口可以引用子对象,但是iterface.childMethod()是不可能的。 我在想的是,因为我正在进行依赖注入,不知怎的,我在服务层中收到了daoImpl的对象。 任何人都可以解释发生了什么的整个概念吗?
ISocietyAccountMasterDao
public interface ISocietyAccountMasterDao extends IGenericRepository<SocietyAccountMaster> {
List<SocietyAccountMaster> getAllSocietyAccounts(String societyId, Long accountTypeId);
}
SocietyAccountMasterDaoImpl
public class SocietyAccountMasterDaoImpl extends AbstractDao<String, SocietyAccountMaster>
implements ISocietyAccountMasterDao {
private final Logger logger = LogManager.getLogger(LoggingAspect.MANUAL);
@Override
public List<SocietyAccountMaster> getAllSocietyAccounts(String societyId, Long accountTypeId) {
Criteria cr = getEntityCriteria();
try {
cr.add(Restrictions.eq("societyId", societyId));
if (!StringUtils.isEmpty(accountTypeId)) {
cr.add(Restrictions.eq("accountType.id", accountTypeId));
}
return cr.list();
} catch (Exception e) {
logger.error("Error While Society Accounts", e);
throw new BindException(BindStatus.FAILURE, BindStatus.FAILURE_MSG);
}
}
ISocietyAccountingService
public interface ISocietyAccountingService {
List<SocietyAccountMasterDto> getAllSocietyAccounts(String societyId);
}
SocietyAccountingServiceImpl
@Service("societyAccountingService")
@Transactional
public class SocietyAccountingServiceImpl implements ISocietyAccountingService {
@Override
public List<SocietyAccountMasterDto> getAllSocietyAccounts(String societyId) {
List<SocietyAccountMasterDto> responses = new ArrayList<SocietyAccountMasterDto>();
List<SocietyAccountMaster> dbSocietyAccountMasters = societyAccountMasterDao.getAllSocietyAccounts(societyId,
null);
for (SocietyAccountMaster dbSocietyAccountMaster : dbSocietyAccountMasters) {
SocietyAccountMasterDto response = new SocietyAccountMasterDto();
response.setNickName(dbSocietyAccountMaster.getNickName());
response.setBankName(dbSocietyAccountMaster.getBankName());
response.setBalance(dbSocietyAccountMaster.getBalance());
responses.add(response);
}
return responses;
}
}
答案 0 :(得分:1)
@Component
- 表示bean是自动扫描组件。这意味着当spring注入autowire时,spring将使用beanName搜索bean。
@Repository
- 表示持久层中的DAO组件可用作自动扫描组件。
如果看到@Repository
annonation的实现,如下所示:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String value() default "";
}
此注释类使用@Component
进行注释,这使其可用于自动布线。这是因为Spring自动检测它,因此DAO impl可以通过自动接线服务。