...
Ce chapitre n'a pas la prétention d'être une formation à Spring. Pour plus d'informations vous pouvez vous reporter à la documentation de springsource ( HTML / PDF )http://www. springframework.org/node/155. Ne sont abordés ici que quelques éléments qui permettent de mieux comprendre certains éléments des fichiers de configuration de esup-commons.
...
Tout au long de ce chapitre nous allons nous appuyer sur un exemple (configuration du gestionnaire d'exceptions) :
| Bloc de code |
|---|
<bean id="exceptionServiceFactory" class="org.esupportail.formationcommons.services.exceptionHandling.CachingEmailExceptionServiceFactoryImpl" parent="abstractApplicationAwareBean"> <property <property name="doNotSendExceptionReportsToDeveloperssmtpService" valueref="falsesmtpService" /> <property name="smtpServicerecipientEmail" refvalue="smtpService"${exceptionHandling.email}" /> <property <property name="authenticationService" ref="authenticationService"/> name="exceptionViews" > <map> <entry key="java.lang.Exception" value="go_exception" /> </map> </property> <property name="recipientEmaillogLevel" value="webmaster@domain.edu"${exceptionHandling.logLevel}" /> <property name="cacheManager" ref="cacheManager" /> <property name="cacheName" value="" /> </bean> |
Les fichiers de configuration
...
Le fichier de configuration principal (/properties/applicationContext.xml) est déclaré dans le *web.xml *sous forme d'un paramètre de l'application :
...
On voit ici un autre aspect important de Spring qui est l'utilisation quasi systématique des interfaces. La classe CachingEmailExceptionServiceFactoryImpl (qui correspond au bean exceptionServiceFactory et contient la définition de cette propriété authenticationService) a un attribut authenticationService de type AuthenticationService. AuthenticationService est une interface. Le bean authenticationService doit être d'une classe qui implémente cette interface. Ceci permet d'avoir plusieurs implémentations possibles de cette interface et de choisir, simplement en modifiant un fichier de configuration, laquelle on utilise. Cette approche est particulièrement intéressante. Elle permet, par exemple, de très facilement tester une couche de l'application en branchant des implémentations de tests des autres couches avec lesquelles le bean doit interagir.
Injection d'une map
| Bloc de code |
|---|
<property name="exceptionViews" >
<map>
<entry key="java.lang.Exception" value="go_exception" />
</map>
</property>
|
Injection d'une liste
| Bloc de code |
|---|
<property name="servers">
<list>
<ref bean="smtpServer1" />
<ref bean="smtpServer2" />
</list>
</property>
|
...