pavla novakova
Nov 2nd, 2008, 02:08 PM
Hi,
I'm just experimenting with Spring 2.5 and nice features it offers. I would like to ask you if you could give me some best practice if I want to avoid defining beans (services, daos, controllers) in xml as much as possible and want to use annotations like Service, Repository, Controller, ... and use <context:component-scan /> instead of listing those beans explicitly.
Let say, that I have two application context files (I use spring-mvc for web layer): application-context.xml and dispatcher-servlet.xml. In application-context.xml I have datasource configuration, jpa configurations, transaction management configuration .... and in dispatcher-servlet.xml configuration I have my url mapping configuration ... And I have 3 types of component: Service (they are annotated also with **Transactional), Repository and Controller. In my Controller component I need to call Service components (I want autowire( in constructor) service component into controller component). I tried to add to application-context.xml:
<context:component-scan base-package="somemypackage.dao" />
<context:component-scan base-package="somemypackage.service" />
and to dispatcher-servlet.xml:
<context:component-scan base-package="somemypackage.controllers" />
But since my controller uses service component this is not enough. I need also add to dispatcher-servlet.xml
<context:component-scan base-package="somemypackage.service" />
But this also doesn't work for me because with this configuration I get exception - entity manager is closed. Simply I think it doesn't start transaction or something like that because duplicated service component scan. I had some post about this problem in thread number 61942 (I'm not able to paste direct link, since I have not 5 posts to this forum yet).
Please advise. Thank you very much. Pavla Novakova
I'm just experimenting with Spring 2.5 and nice features it offers. I would like to ask you if you could give me some best practice if I want to avoid defining beans (services, daos, controllers) in xml as much as possible and want to use annotations like Service, Repository, Controller, ... and use <context:component-scan /> instead of listing those beans explicitly.
Let say, that I have two application context files (I use spring-mvc for web layer): application-context.xml and dispatcher-servlet.xml. In application-context.xml I have datasource configuration, jpa configurations, transaction management configuration .... and in dispatcher-servlet.xml configuration I have my url mapping configuration ... And I have 3 types of component: Service (they are annotated also with **Transactional), Repository and Controller. In my Controller component I need to call Service components (I want autowire( in constructor) service component into controller component). I tried to add to application-context.xml:
<context:component-scan base-package="somemypackage.dao" />
<context:component-scan base-package="somemypackage.service" />
and to dispatcher-servlet.xml:
<context:component-scan base-package="somemypackage.controllers" />
But since my controller uses service component this is not enough. I need also add to dispatcher-servlet.xml
<context:component-scan base-package="somemypackage.service" />
But this also doesn't work for me because with this configuration I get exception - entity manager is closed. Simply I think it doesn't start transaction or something like that because duplicated service component scan. I had some post about this problem in thread number 61942 (I'm not able to paste direct link, since I have not 5 posts to this forum yet).
Please advise. Thank you very much. Pavla Novakova