how annotation @Repository in java spring work?

how annotation @Repository in java spring work?

Spring Repository is responsible for importing the DAOs into the DI container and also it makes the unchecked exceptions into Spring DataAccessException. The Spring Repository annotation is meta annotated with the @Component annotation so that the repository classes will be taken up for component scanning.

Teams implementing traditional Java EE patterns such as Data Access
Object may also apply this stereotype to DAO classes, though care
should be taken to understand the distinction between Data Access
Object and DDD-style repositories before doing so. This annotation is
a general-purpose stereotype and individual teams may narrow their
semantics and use as appropriate.

A class thus annotated is eligible for Spring DataAccessException
translation when used in conjunction with a
PersistenceExceptionTranslationPostProcessor. The annotated class is
also clarified as to its role in the overall application architecture
for the purpose of tooling, aspects, etc.

Source: JavaDoc

but in your case you are also extending the JpaRepository of Spring Data JPA. Spring Data automatically provides implementations of common CRUD operations. The JpaRepository extends the interface CrudRepository which has the methods declared for all basic crud operations.

public interface EquipmentRepository extends JpaRepository<Account, Long> { … }

Defining this interface serves two purposes:

  • First, by extending JpaRepository we get a bunch of generic CRUD
    methods into our type that allows saving Equipments, deleting them and
    so on.
  • Second, this will allow the Spring Data JPA repository infrastructure
    to scan the classpath for this interface and create a Spring bean for
    it.

The @EnableJpaRepositories scans all packages below com.acme.repositories for interfaces extending JpaRepository and creates a Spring bean for it that is backed by an implementation of SimpleJpaRepository (spring data provides default imlpementations of CRUD repository through this class).

So that is why even when you havent defined the method , you are able to do crud operations through this setup.

Refer : https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.repositories

how annotation @Repository in java spring work?

Leave a Reply

Your email address will not be published. Required fields are marked *