Join waitlist
JavaCommunity question

@Repository vs @Service vs @Component

Asked by Bhavana Shetty 25 days ago 2 answers 7 votes

I answered "no difference, all are @Component" and it was marked wrong. what's the difference then?

2 answers

Farhan Ali25 days agoMost voted7

You're mostly right, all three get picked up by component scanning. The real difference is @Repository: Spring translates database specific exceptions into its DataAccessException hierarchy for beans marked with it. @Service has no extra behaviour, it's just to mark the service layer.

Akash Pandey25 days ago2

next question they ask is @Controller vs @RestController

Related questions

Go deeper

Want the full explanation, not just an answer?

Structured lessons on DSA, core CS, Java and interview skills, each ending in a quiz so you know it stuck.