Swifty
Swifty

Reputation: 159

Spring @Repository @Document is not necessary?

I have a spring boot application and connected to Mongo DB. I know all most all of documents or blogs said the sample code should like this:

@Repository
public interface ProductRepository extends MongoRepository<Product, String> {

}

@Document
public class Product {
    private String id;
    private String name;
    private int price;
}

But I found even if I remove @Repository and @Document annotations. The application still can start without error. Spring still can know ProductRepository is spring bean and also can CRUD Product collection.

So does these not necessary to add @Repository and @Document? Or is there any difference add or not add?

Upvotes: 3

Views: 1379

Answers (2)

Nikolas
Nikolas

Reputation: 44368

The annotation @Repository registers a class as a Spring bean which makes it autowirable. Spring Data doesn't use annotations but provides functionality through extending reposotory classes such as JpaRepository or MongoRepository.

Upvotes: 2

zlaval
zlaval

Reputation: 2027

Not necessery.

Spring can found it, because you extends the MongoRepository interface, and add Product as it type.

@Repository is useful anyway, for example if you create a custom repository.

@Document is also, if you want to specify custom property values, for example collection name..

Upvotes: 2

Related Questions