|
|
<table>
|
|
|
<thead>
|
|
|
<tr>
|
|
|
<th width="200px"><h6>[:rewind: Exo Précédent](/Exo05-Init-et-Destroy/1%5D-Code-Java)</h6></th>
|
|
|
<th colspan="4" width="600px"><h1 align="center">06 - Spring - Scope et Context</h1></th>
|
|
|
<th width="200px"><h6 align="right">[Exo Suivant :fast_forward:](/Exo07-Declaration-Bean/1%5D-Code-Java)</h6></th>
|
|
|
</tr>
|
|
|
</thead>
|
|
|
|
|
|
<tbody>
|
|
|
<tr>
|
|
|
<td> </td>
|
|
|
<td align="center">[:one:](/Exo06-Scope-et-context/1%5D-Code-Java)<br/>Code Java</td>
|
|
|
<td align="center">:white_check_mark:<br/>XML</td>
|
|
|
<td align="center">[:three:](/Exo06-Scope-et-context/3%5D-Configuration)<br/>Configuration</td>
|
|
|
<td align="center">[:four:](/Exo06-Scope-et-context/4%5D-Spring-Boot)<br/>Spring Boot</td>
|
|
|
<td> </td>
|
|
|
</tr>
|
|
|
</tbody>
|
|
|
|
|
|
<tfoot>
|
|
|
<tr align="right">
|
|
|
<td colspan="6">
|
|
|
<h6>:copyright: 2019 - <a href="mailto:admin@ferretrenaud.fr">FERRET Renaud</a></h6>
|
|
|
</td>
|
|
|
</tr>
|
|
|
</tfoot>
|
|
|
</table>
|
|
|
|
|
|
***
|
|
|
|
|
|
# Modification du fichier XML
|
|
|
L'utilisation de name space n'a de sens que dans le cas des fichiers XML.
|
|
|
|
|
|
Ajustez votre fichier `src\main\resources\spring\mesBeans.xml` afin de pouvoir faire usage du name space *context*.
|
|
|
|
|
|
```xml
|
|
|
<?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:context="http://www.springframework.org/schema/context"
|
|
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
|
|
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
|
|
...
|
|
|
|
|
|
</beans>
|
|
|
```
|
|
|
|
|
|
Vous pouvez maintenant en faire usage, par exemple en remplaçant le chargement du fichier de properties par la ligne :
|
|
|
```xml
|
|
|
...
|
|
|
<context:property-placeholder location="spring/adresses.properties" />
|
|
|
...
|
|
|
```
|
|
|
|
|
|
L'activation des annotations [@javax.annotation.PostConstruct](https://docs.oracle.com/javase/8/docs/api/javax/annotation/PostConstruct.html) et [@javax.annotation.PreDestroy](https://docs.oracle.com/javase/8/docs/api/javax/annotation/PreDestroy.html) peut maintenant s'écrire :
|
|
|
```xml
|
|
|
...
|
|
|
<context:annotation-config />
|
|
|
...
|
|
|
```
|
|
|
|
|
|
# Test
|
|
|
Lancez votre classe Main et regardez ce qui s'affiche.
|
|
|
|
|
|
En cas d'erreur lisez attentivement les messages Spring, n'hésitez pas à scroller.
|
|
|
|
|
|
# Modification du fichier XML pour le scope
|
|
|
Ajouter un attribut XML `scope` à votre bean client et adresse.
|
|
|
|
|
|
Relancez votre code et regardez ce qui se passe selon que vous indiquez `singleton` ou `prototype`.
|
|
|
```xml
|
|
|
...
|
|
|
<bean id="client1" ... scope="prototype">
|
|
|
...
|
|
|
</bean>
|
|
|
...
|
|
|
```
|
|
|
# Bonus
|
|
|
Comment modifier le code de sa méthode main pour prouver que le `scope` est bien respecté ?
|
|
|
|
|
|
***
|
|
|
|
|
|
<table>
|
|
|
<thead>
|
|
|
<tr>
|
|
|
<th width="200px"><h6>[:rewind: Exo Précédent](/Exo05-Init-et-Destroy/1%5D-Code-Java)</h6></th>
|
|
|
<th colspan="4" width="600px"><h1 align="center">06 - Spring - Scope et Context</h1></th>
|
|
|
<th width="200px"><h6 align="right">[Exo Suivant :fast_forward:](/Exo07-Declaration-Bean/1%5D-Code-Java)</h6></th>
|
|
|
</tr>
|
|
|
</thead>
|
|
|
|
|
|
<tbody>
|
|
|
<tr>
|
|
|
<td> </td>
|
|
|
<td align="center">[:one:](/Exo06-Scope-et-context/1%5D-Code-Java)<br/>Code Java</td>
|
|
|
<td align="center">:white_check_mark:<br/>XML</td>
|
|
|
<td align="center">[:three:](/Exo06-Scope-et-context/3%5D-Configuration)<br/>Configuration</td>
|
|
|
<td align="center">[:four:](/Exo06-Scope-et-context/4%5D-Spring-Boot)<br/>Spring Boot</td>
|
|
|
<td> </td>
|
|
|
</tr>
|
|
|
</tbody>
|
|
|
|
|
|
<tfoot>
|
|
|
<tr align="right">
|
|
|
<td colspan="6">
|
|
|
<h6>:copyright: 2019 - <a href="mailto:admin@ferretrenaud.fr">FERRET Renaud</a></h6>
|
|
|
</td>
|
|
|
</tr>
|
|
|
</tfoot>
|
|
|
</table> |