I am working on Spring Boot project. I have repository file in my project but it will show me a warning message in repository class Unnecessary @Repository. I am extending a JpaRepository<> with my repository. My Spring version is 4 and JDK version is 17.
Here is my dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Here is my repository
@Repository // Here I get a warning to remove this annotation becasue its unnecessary
public interface CollegeRepo extends JpaRepository<College, Integer>{
}
You are extending JpaRepository<T, ID> interface, it means that spring boot must autoconfigure this repository bean for you, namely, it will be configured a proxy bean of SimpleJpaRepository<T, ID>.
In simple words, we do not just create a bean using @Repository or @Component annotation, we extend the spring-data interface and then our repository bean will be autoconfigured.
@RepositoryYou want to provide your own implementation of how to access the data layer and what should be done. In this case marking your implementation class with @Repository will allow you to have this class managed by spring so that you can autowire necessary fields to access data layer like EntityManager , JdbcTemplate ...etc. Although Component, and @Repository in the most fundamental level just register spring beans there are some slight enhancements using @Repository which might make it neccessary to use and also best practice in the current case.
As per doc1
A class thus annotated with
@Repositoryis eligible for Spring DataAccessException translation when used in conjunction with a PersistenceExceptionTranslationPostProcessor.,
and doc2
PersistenceExceptionTranslationPostProcessor
Bean post-processor that automatically applies persistence exception translation to any bean marked with Spring's @Repository annotation, adding a corresponding PersistenceExceptionTranslationAdvisor to the exposed proxy
Example of above case use with @Repository.
@Repository
public class CustomCarRepositoryImpl implements CustomCarRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<CarEntity> findCarsWithSpeed(Integer speed) {
return entityManager.createQuery("Query to execute")
.setMaxResults(50).getResultList();
}
}
public interface CustomCarRepository {
List<CarEntity> findCarsWithSpeed(Integer speed);
}
Then you can autowire in your other components the CustomCarRepository and access the data layer as you have implemented.
@RepositoryWhen you just declare your interface and you extend from any Spring child interface of Repository from org.springframework.data.repository.
Example
public interface CarRepository extends JpaRepository<CarEntity, Long> {
List<CarEntity> findCarsWithSpeed(Integer speed);
}
In that case Spring Boot will be able to create the repository bean for you automatically from auto configuration.
The only further action needed is if your own interfaces extending from Repository do not exist in the same package or subpackage of where your @Configuration or @SpringBootApplication exists then you would need
@EnableJpaRepositories(basePackages = {"base-package-where-repositories-exist"})@AutoConfigurationPackage(basePackages = {"base-package-where-repositories-exist"})as to help spring boot identify the package it should look for the auto configuration of that repository. ( The later @AutoConfigurationPackage will affect both repositories and other things required for auto configuration like entities scan and more. So it should be used with care in a project and not just for repositories.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With