Accessing Service Layer from custom Controller in Spring MVC

≡放荡痞女 提交于 2019-12-24 10:02:57

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/11034545/accessing-service-layer-from-custom-controller-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!