Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 695 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's the difference between @Component, @Repository & @Service annotations in Spring?

#21
@Component is equivalent to

<bean>

**@Service, @Controller, @Repository = {@Component + some more special functionality}**

That mean Service, The Controller and Repository are functionally the same.

The three annotations are used to separate **"Layers"** in your application,

- Controllers just do stuff like dispatching, forwarding, calling service methods etc.
- Service Hold business Logic, Calculations etc.
- Repository are the DAOs (Data Access Objects), they access the database directly.

Now you may ask why separate them: (I assume you know AOP-Aspect Oriented Programming)

Let's say you want to Monitors the Activity of the DAO Layer only. You will write an Aspect (A class) class that does some logging before and after every method of your DAO is invoked, you are able to do that using AOP as you have three distinct Layers and are not mixed.

So you can do logging of DAO "around", "before" or "after" the DAO methods. You could do that because you had a DAO in the first place. What you just achieved is **Separation of concerns or tasks.**

Imagine if there were only one annotation @Controller, then this component will have dispatching, business logic and accessing database all mixed, so dirty code!

*Above mentioned is one very common scenario, there are many more use cases of why to use three annotations.*


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through