Reputation: 6389
I want to add customize behavior to my repository for example I want to add a method that save and log my entity this is my repository:
@Repository
public interface BookRepository extends JpaRepository<Book, Long>, BookRepositoryCustom {
Book findByTitle(String bookName);
}
and this is my custom repo:
public interface BookRepositoryCustom {
void saveAndLog(Book book);
}
and this is the implementation:
@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);
}
}
and this is my main method:
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);
}
every thing seems to be ok but app hase crash and do not work. I wonder if I should tell the bootspring about my custom Repository or it will be scaned automatically.
Upvotes: 0
Views: 80
Reputation: 301
Write your implementation class with the name BookRepositoryImpl and not BookRepositoryCustomImpl :
@Component
public class BookRepositoryImpl implements BookRepositoryCustom {
.......
}
You can refer the documentation also :
Upvotes: 1