- pom.xml
Le fichier pom.xml centralise toutes les dépendances nécessaires au projet. Les dépendances ont été regroupées par domaine fonctionnel pour une meilleure lisibilité.
<dependencies>
<!-- Tests unitaires -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring : modules principaux -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!-- Programmation orientée aspect -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- Accès aux données : pilote MySQL et pool C3P0 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- MyBatis et intégration avec Spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!-- Journalisation SLF4J -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<!-- Plugin de pagination de MyBatis -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!-- Couche web : Spring MVC et Servlet -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- Sérialisation JSON avec Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Téléversement de fichiers -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<finalName>gestion</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<contextPath>/</contextPath>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
- web.xml
Le descripteur de déploiement web.xml initialise le conteneur Spring, confiugre le contrôleur frontal de Spring MVC et applique le filtre d'encodage UTF-8. Dans cet exemple, les requêtes sont interceptées via le suffixe *.action.
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- Emplacement du fichier de configuration Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Contrôleur frontal de Spring MVC -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<!-- Chargement du servlet au démarrage -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- Requêtes prises en charge -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- Filtre d'encodage UTF-8 pour les requêtes POST -->
<filter>
<filter-name>encodageCaracteres</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodageCaracteres</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Fichiers de configuration du dossier resources
Les fichiers suivants sont placés dans src/main/resources et définissent respectivement le contexte Spring, la couche MVC, MyBatis, ainsi que les paramètres de connexion et de journalisation.
3. spring.xml
Le fichier spring.xml déclare le scan des composants (hors contrôleurs), la source de données C3P0, le gestionnaire de transactions, les règles AOP et la fabrique de sessions MyBatis.
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!-- Scan des composants du socle applicatif, hors contrôleurs MVC -->
<context:component-scan base-package="com.acme.gestion">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- Chargement des propriétés de connexion -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- Source de données C3P0 -->
<bean id="sourceDonnees" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- Gestionnaire de transactions -->
<bean id="gestionnaireTransactions"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="sourceDonnees"/>
</bean>
<!-- Conseil transactionnel appliqué aux méthodes des services -->
<tx:advice id="conseilTransactions" transaction-manager="gestionnaireTransactions">
<tx:attributes>
<tx:method name="ajouter*" propagation="REQUIRED"/>
<tx:method name="creer*" propagation="REQUIRED"/>
<tx:method name="modifier*" propagation="REQUIRED"/>
<tx:method name="supprimer*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- Activation de l'AOP par annotations -->
<aop:aspectj-autoproxy/>
<!-- Application des transactions à la couche service -->
<aop:config>
<aop:pointcut id="pointcutServices"
expression="execution(* com.acme.gestion.services..*.*(..))"/>
<aop:advisor advice-ref="conseilTransactions" pointcut-ref="pointcutServices"/>
</aop:config>
<!-- Fabrique de sessions SQL MyBatis -->
<bean id="fabriqueSessionsSql" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="sourceDonnees"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/acme/gestion/repositories/*.xml"/>
</bean>
<!-- Scanner des interfaces mapper -->
<bean id="scannerMappers" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.acme.gestion.repositories"/>
<property name="sqlSessionFactoryBeanName" value="fabriqueSessionsSql"/>
</bean>
</beans>
4. mvc.xml
Le fichier mvc.xml configure la couche Spring MVC : scan des contrôleurs, convertisseurs de messages JSON, gestion des ressources statiques, résolveur de vues JSP et téléversement de fichiers.
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scan de la couche contrôleur -->
<context:component-scan base-package="com.acme.gestion.controllers"/>
<!-- Pilotage par annotations et conversion des messages -->
<mvc:annotation-driven>
<mvc:message-converters>
<!-- Réponses de type chaîne de caractères -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<!-- Conversion des objets en JSON -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- Délégation des ressources statiques au serveur web -->
<mvc:default-servlet-handler/>
<!--
Exemple de configuration d'intercepteur :
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/utilisateur/connexion.action"/>
<bean class="com.acme.gestion.intercepteurs.MonIntercepteur"/>
</mvc:interceptor>
</mvc:interceptors>
-->
<!-- Résolveur de vues JSP -->
<bean id="resolveurVues"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="contentType" value="text/html"/>
<property name="prefix" value="/WEB-INF/vues/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Gestion du téléversement de fichiers -->
<bean id="televersementMultipart"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600"/>
<property name="maxInMemorySize" value="4096"/>
</bean>
</beans>
5. mybatis.xml
Le fichier mybatis.xml définit les alias de types pour les objets métier et enregistre le plugin de pagination PageHelper.
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<typeAliases>
<package name="com.acme.gestion.model"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
</configuration>
6. database.properties
Ce fichier contient les paramètres de connexion à la base de données MySQL.
jdbc.driver =com.mysql.cj.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/gestion?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
jdbc.username =root
jdbc.password =
7. log4j.properties
Le fichier log4j.properties configure la journalisation avec une sortie console. Les instructions concernant la sortie fichier sont volnotairement commentées.
# Niveau global de journalisation : DEBUG, appender console active
log4j.rootLogger=DEBUG,console
# Appender console
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%t] %p [%c] - <%m>%n
# Appender fichier (désactivé)
#log4j.appender.file = org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=./log/logFile.log
#log4j.appender.file.MaxFileSize=10mb
#log4j.appender.file.Threshold=DEBUG
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
# Journalisation des requêtes SQL
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG