我想在我的存储库中添加自定义行为,例如我想添加一个保存和记录我的实体的方法,这是我的存储库:
@Repository
public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom {
Book findByTitle(String bookName);
}
这是我的自定义回购:
public interface BookRepositoryCustom {
void saveAndLog(Book book);
}
这是实施:
@Repository
public class BookRepositoryCustomImpl implements BookRepositoryCustom {
private static Logger logger = LoggerFactory.getLogger(BookRepositoryCustomImpl.class);
@Autowired
private EntityManager entityManager;
@Override
@Transactional
public void saveAndLog(Book book) {
logger.debug("saving the book entiry "+book.toString());
entityManager.persist(book);
}
}
这是我的主要方法:
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringDataTestApplication.class, args);
BookRepository repo = context.getBean(BookRepository.class);
Book book = new Book();
book.setTitle("dynamic book");
repo.saveAndLog(book);
}
每件事似乎都没问题,但应用程序会崩溃并且无法正常工作。我想知道是否应该告诉靴子关于我的自定义存储库,否则它会被自动扫描。
答案 0 :(得分:1)
使用名称BookRepositoryImpl而不是BookRepositoryCustomImpl编写实现类:
@Component
public class BookRepositoryImpl implements BookRepositoryCustom {
.......
}
您也可以参考文档: