How to write service layer in Spring ?

How to write service layer in Spring ?

I have heard that there would be a service interface , ServiceImpl class and Serviceutil class.

Can anyone please post a sample example code how this is written in Spring ?

Thanks
cofactorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cofactorAuthor Commented:
comments please
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
There is nothing specific about a service layer in Spring compared to anything else, other than keeping a clean separation between that service layer and your data access layer below, and again the service layer to a presentation layer above. But again, this isn't really specific to Spring, it is just that it is then easier to inject the required dependencies into each component.
0
cofactorAuthor Commented:
I still dont understand what happens when we mark a class with @Service annotation.

What Spring will do to this class ?  Could you please explain with an example . I'm still confused.
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Sorry it has taken me a while to get back to this one.

I still dont understand what happens when we mark a class with @Service annotation.
Ok, well that is a somewhat different question.

@Service simply marks a class to be a candidate for Spring to instantiate a service layer object. However, it does nothing just by itself. If you have indicated to Spring that it should scan the classpath for components, then it will find your class marked with @Service and instantiate it as a bean, ie. it will do basically that same thing as a...    <bean class="my.service.Class" .... /> and all the associate auto-wiring if necessary. So it and it's friends, @Repository, @Controller, @Component, etc are (in conjunction with @Autowired) a way to setup all your Spring beans without having to write copious amounts of boilerplate XML.

So how do you tell Spring to go and look for these annotations? Well, you just include a <context:component-scan .... > element in your application context XML. I think there may be other elements in Spring which also include the scanning functionality, but even if there are, it won't hurt to have both. So when you include the above element, Spring registers an implementation of a BeanFactoryPostProcessor which does the magic of scanning the classpath for classes with the required annotations and creating the beans.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.