Hello à tous je débute sur Spring. J'ai besoin de votre avis sur la configuration de mon application avec Spring MVC + Hibernate + MySQL.
Mon application fonctionne apparemment, mais je suis confus sur la configuration.
Pour Spring MVC
j'ai utilisé la configuration springapp-servlet.xml suivant:
Pour la couche service qui accède à hibernate j'ai créé une autre configuration xml: service-context.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean name="/welcome.htm" class="org.free.tutorial.irnbru.web.controller.WelcomeController"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/> <context:component-scan base-package="org.free.tutorial.irnbru"></context:component-scan> <context:annotation-config/> </beans>
le code de mon service est le suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="masource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost/newstranslation"/> <property name="username" value="root"/> <property name="password" value="hyeres"/> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="masource"></property> <property name="annotatedClasses"> <list> <value>org.free.tutorial.irnbru.domain.Source</value> <value>org.free.tutorial.irnbru.domain.Language</value> <value>org.free.tutorial.irnbru.domain.ContentType</value> <value>org.free.tutorial.irnbru.domain.Article</value> <value>org.free.tutorial.irnbru.domain.Author</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionfactory"></property> </bean> </beans>
le code de mon controlleur est le suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.free.tutorial.irnbru.service; import java.util.List; import org.free.tutorial.irnbru.domain.Source; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service("SourceServiceBean") @Transactional public class SourceServiceImpl implements SourceService { @Autowired private SessionFactory factory; @SuppressWarnings("unchecked") @Override public List<Source> findall() { // TODO Auto-generated method stub return factory.getCurrentSession().createQuery("from Source").list(); } }
Ma question est la suivante pourquoi il est nécessaire de mettre les balises
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 package org.free.tutorial.irnbru.web.controller; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.free.tutorial.irnbru.domain.Source; import org.free.tutorial.irnbru.service.SourceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class WelcomeController implements Controller { @Autowired private SourceService service; private List<Source> sources; public SourceService getService() { return service; } public void setService(SourceService service) { this.service = service; } @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { setSources(service.findall()); return new ModelAndView("welcome","sources",getSources()); } public void setSources(List<Source> sources) { this.sources = sources; } public List<Source> getSources() { return sources; } }
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
<context:component-scan base-package="org.free.tutorial.irnbru"></context:component-scan>
<context:annotation-config/>
dans le fichier de configuration du Dispatcher de Spring MVC (springapp-servlet.xml) et on ne peut pas les laisser dans le fichier de configuration de la couche service (service-context.xml)?
Par exemple Si je laissais les balises de context dans la configuration service-context.xml alors mon service ne serait pas injecté dans mon contrôleur par l'injection automatique par annotation...
J'ai donc corrigé en le mettant dans springapp-servlet.xml , mais j'ai pensé que je pouvais laisser la balise <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/> dans la configuration service-context.xml de mon service. Mais lors que je lance l'application une exception est lancée
C'est comme si les balises de configuration de Spring:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 SEVERE: Servlet.service() for servlet springapp threw exception org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/>
<context:component-scan base-package="org.free.tutorial.irnbru"></context:component-scan>
<context:annotation-config/>
dans le fichier service-context.xml ne sont pas accessible à partir de la configuration Spring MVC. Pourtant ses beans eux sont accessibles
En effet DispatcherServlet a un WebApplicationContext qui hérite de tous les beans définis dans le WebApplicationContext racine (parent donc) défini par mon fichier service-context.xml
. Par contre il n'hériterait pas des balises de configuration "tx", "context" ???
Merci pour votre aide car je ne sais pas si je m'y suis bien pris.
IRNBRU
Partager