Search code examples
javaspringspring-mvcspring-roo

Accessing Service Layer from custom Controller in Spring MVC


I am new to Spring and ROO and this Annotation/Aspect hell.

I have an Spring MVC Project created with Spring ROO. I use mongo-db as my persistance layer. I have an entity Report with the domain object, the service, the repository and the controller. I added a custom controller wich workes so far. I want to just access my stored reports with the ReportService.findAllReports(), but I'm not sure how to get access to this service.

Here is a link to my roo generated site http://sauberseite.cloudfoundry.com/

The main objective is to report adresses and then display all adresses in a google map, for which I have my custom controller and where I need to access the service layer


Solution

  • You can directly @Autowired it as follows.

    @Controller
    public class CustomController {
        @Autowired
        ReportService reportService; //this inject's your bean here.
    
        List<Report> getReports() {
            return reportService.findAllReports();
        }
    }
    

    If you don't use annotation @Controller and defined your bean in xml, then you can inject ReportService as a property (just remove @Autowired annotation) and write a setter for it.