Search code examples
javaspringintellij-ideainterfacespring-annotations

Does annotating a repository interface as @Component have any cons?


I have this interface:

public interface liteRepository extends CrudRepository<liteEntity, Long>, JpaSpecificationExecutor<liteEntity> {...}

It works, all is well.

However, intellij does not register this class as a spring component. If I annotate this interface with @Component, then intellij recognizes this as a spring bean and I can @Autowire this repository in my integration tests.

My code still works after annotation, but I'm not confident that I am not messing with things that I should not be messing with.

Question:
Is there any harm in adding the @Component annotation to this interface?


Solution

  • The only thing that @Component annotation means is that the class is eligible for becoming a Spring bean during Spring's component-scan.

    So, if you want it to be a Spring bean and you did not define it as a Spring bean anywhere else, you can safely add the @Component annotation.

    Of course, this will only work if you have the actual component scan configured somewhere(for, example <context:component-scan base-package="..."> in some Spring config file), which I am assuming you already heave, since the bean is properly getting autowired after you add the annotation.