Architectural Integration Strategy
Persistence Layer Configuration
The persistence layer is handled by MyBatis. The process begins with a configuration file, typically named
SqlMapConfig.xml. To integrate this with Spring, a specific Spring configuration file (e.g.,
applicationContext-dao.xml) is created to manage the DAO beans.
The primary configuration steps involve:
- Setting up the data source connection pool.
- Configuring
SqlSessionFactory as a singleton bean within the Spring container, injecting the data source and the MyBatis configuration location.
- Scanning for Mapper interfaces using
MapperScannerConfigurer to automatically generate proxy objects and register them in the container.
Service Layer Configuration
The Service layer requires two main configurations:
- Transaction Management: Defining a transaction manager that utilizes the data source configured in the persistence layer.
- Component Scanning: Scanning the package containing service implementations to register them as Spring beans, typically using annotations like
@Service.
Presentation Layer Configuration
For the presentation (Spring MVC) layer, the configuration must:
- Enable annotation-driven MVC support via
<mvc:annotation-driven/>.
- Configure a view resolver (such as
InternalResourceViewResolver) to map logical view names to physical JSP resources.
- Scan the controller package to register annotated controllers.
Web Application Descriptor (web.xml)
The
web.xml file serves as the entry point for the application. It must:
- Initialize the Spring root context via a
ContextLoaderListener.
- Register the Spring MVC
DispatcherServlet as the front controller.
- Configure a character encoding filter to handle POST request encoding issues.
Project Structure and Configuration Files
Since this is a web archive (WAR) project, all configuration files must reside within the web project structure. For better maintainability, the Spring configuration can be modularized into multiple files (e.g.,
applicationContext-dao.xml,
applicationContext-service.xml,
applicationContext-transaction.xml) or consolidated into a single
applicationContext.xml. This guide employs the modular approach.
MyBatis Configuration
The core MyBatis configuration file is required, though it may remain minimal if properties are handled elsewhere.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- Global settings or aliases can be defined here -->
</configuration>
Persistence Layer Spring Configuration
First, define the database properties in
db.properties:
database.url=jdbc:mysql://localhost:3306/my_database
database.driver=com.mysql.jdbc.Driver
database.user=admin
database.pass=admin123
Next, configure the Spring context for the DAO layer (
applicationContext-dao.xml). This example uses a Druid connection pool.
<?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">
<!-- Load database properties -->
<context:property-placeholder location="classpath:config/db.properties" />
<!-- Druid Connection Pool Configuration -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.pass}" />
<property name="driverClassName" value="${database.driver}" />
<property name="maxActive" value="20" />
<property name="minIdle" value="5" />
</bean>
<!-- MyBatis SqlSessionFactory Integration -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
<!-- Mapper Interface Scanner -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.persistence" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
Service Layer Configuration
The service configuration (
applicationContext-service.xml) focuses on component scanning to detect service implementations.
<?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">
<!-- Scan Service Components -->
<context:component-scan base-package="com.example.service" />
</beans>
Transaction Management Configuration
Transaction boundaries are defined using AOP in
applicationContext-transaction.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Transaction Manager -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Transaction Advice -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- Write operations -->
<tx:method name="persist*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<!-- Read operations -->
<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
<tx:method name="fetch*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- AOP Configuration -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.example.service.*.*(..))" />
</aop:config>
</beans>
Spring MVC Configuration
The
springmvc.xml file configures the web layer, including view resolution and static resource handling.
<?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 Controllers -->
<context:component-scan base-package="com.example.controller" />
<!-- Enable MVC Annotations -->
<mvc:annotation-driven />
<!-- View Resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Static Resource Mapping -->
<mvc:resources location="/static/css/" mapping="/styles/**" />
<mvc:resources location="/static/js/" mapping="/scripts/**" />
</beans>
Web Application Configuration
Finally, the
web.xml ties everything together.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>SSM Integration Demo</display-name>
<!-- Spring Context Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Character Encoding Filter -->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>frontController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Servlet Mapping -->
<servlet-mapping>
<servlet-name>frontController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>