Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:26:13+00:00 2026-06-15T11:26:13+00:00

My controller looks like this : @RequestMapping(value = /User, method = RequestMethod.GET) public @ResponseBody

  • 0

My controller looks like this :

@RequestMapping(value = "/User", method = RequestMethod.GET)
public @ResponseBody String getUser(@RequestParam long id) {
User user = userService.get(id);
return user.name;
}

Where service looks like this

@Transactional(readOnly = true)
public User getUser(long id) {
  return userRepository.get(id);
}

I hear the mantra transactions on service layer, transaction on service layer; but would it be so bad to inject the repository directly into controller. After first making the repository transactional – with propagation required so it will create a transaction if not already present ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:26:13+00:00Added an answer on June 15, 2026 at 11:26 am

    This depends on the size of your project and the complexity within it. I feel services work excellent when multiple repositories need to interact.

    For example, a BankAccountService needs to use the AccountRepository to debit/credit an account, while it also needs to use the LedgerRepository to record the transaction.

    If your simply performing crud operations, I would not hesitate to put the repository in the controller. If your operations are more advanced I would take the service approach. Address your simplest use case and then refactor when more complexity is introduced. Doing a big design up front often violates the “You Ain’t Gonna Need It” principle.

    Also services are handy when different controllers need to perform the same operation. So if the logic you are creating is controller specific and you do not need to reuse the code, placing the repository in the controller makes sense. If you will need to duplicate the logic within other controllers a service may be a better option, since it will be reusable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Spring Controller class looks like this @SuppressWarnings(rawtypes) @RequestMapping(value = /cityGridView, method = RequestMethod.GET)
I have a simple Controller that looks like this:- @Controller @RequestMapping(value = /groups) public
I have a method in my Controller which looks like this: @RequestMapping(value = /test,
I'm using Spring MVC and jquery autocomplete, my spring controller looks like this @RequestMapping(value
I have a controller that looks like this: public class PageController : Controller {
Not sure what's going on here... My controller methods looks like this: [HttpGet] public
This is what my controller action looks like [HttpPost] [OutputCache(Location = OutputCacheLocation.None)] public ActionResult
I have a basic Spring MVC controller that looks like this: @Controller public void
Using Spring/SpringMVC 3.0.5 I've defined a method in my controller like this: @RequestMapping(params =
My controller looks like this: public class PortefeuilleController : Controller { public ActionResult Create()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.