1. 程式人生 > >SSH框架分模塊開發

SSH框架分模塊開發

hit led XML schema tab 原本 lap cat 開啟

------------------siwuxie095

SSH 框架分模塊開發

1、在 Spring 核心配置文件中配置多個內容,容易造成

配置混亂,不利於維護

分模塊開發主要針對 Spring 核心配置文件

2、把 Spring 核心配置文件中的一部分配置放到單獨

配置文件中,再在 Spring 核心配置文件中引入單獨的配

置文件即可

3、一般情況下,建議把 Action 對象的配置放到單獨的

配置文件中

「因為其它的配置基本不變,只有 Action 對象的配置在

不斷重復」

如:

user.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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

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">

<!-- 配置 Action 對象 -->

<bean id="userAction" class="com.siwuxie095.action.UserAction">

<property name="userService" ref="userService"></property>

</bean>

<!-- 配置 Service 對象 -->

<bean id="userService" class="com.siwuxie095.service.UserService">

<property name="userDao" ref="userDaoImpl"></property>

</bean>

<!-- 配置 Dao 實現類對象 -->

<bean id="userDaoImpl" class="com.siwuxie095.dao.impl.UserDaoImpl">

<property name="hibernateTemplate" ref="hibernateTemplate"></property>

</bean>

<!-- 配置 HibernateTemplate 對象 -->

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">

<!-- 註入 SessionFactory 對象 -->

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!--

關於 HibernateTemplate 對象的配置,其實也可以放在核心配置文件中,

因為它也是基本不變的

其實關於分模塊開發,把任何一部分拿出去放到單獨的配置文件中都可以,

只是建議把 Action 對象的配置放到單獨的配置文件中,因為只有 Action

對象的配置是不斷重復的,而其它的配置卻是基本不變的

-->

</beans>

applicationContext.xml 中引入 user.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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

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">

<!-- (1) -->

<!-- 配置 C3P0 連接池 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="com.mysql.jdbc.Driver"/>

<!--

jdbc:mysql:///test_db jdbc:mysql://localhost:3306/test_db 的簡寫

-->

<property name="jdbcUrl" value="jdbc:mysql:///test_db"/>

<property name="user" value="root"/>

<property name="password" value="8888"/>

</bean>

<!-- SessionFactory 對象的創建交給 Spring 進行管理 -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

<!--

數據庫配置原本是在 Hibernate 核心配置文件中配置的,

現在 Hibernate 核心配置文件不存在了,所以在這裏註

dataSource

LocalSessionFactoryBean 中有相關屬性,所以可以

註入

-->

<property name="dataSource" ref="dataSource"></property>

<!-- 配置 Hibernate 基本信息 -->

<property name="hibernateProperties">

<props>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.format_sql">true</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<!--

原來的配置:

<prop key="hibernate.current_session_context_class">thread</prop>

SSH 框架整合中會報錯,要麽將這個配置刪了,要麽改成如下配置

參考鏈接:http://blog.csdn.net/maoyuanming0806/article/details/61417995

-->

<prop key="hibernate.current_session_context_class">

org.springframework.orm.hibernate5.SpringSessionContext

</prop>

</props>

</property>

<!-- 引入映射配置文件 -->

<property name="mappingResources">

<list>

<value>com/siwuxie095/entity/User.hbm.xml</value>

<!-- <value>....</value> -->

</list>

</property>

</bean>

<!-- (2) -->

<!--

引入 src 下的 user.xml 配置文件

其實 classpath 可以省略不寫,即:

<import resource="user.xml"/>

建議寫上

-->

<import resource="classpath:user.xml"/>

<!-- (3) -->

<!-- 配置事務管理器 HibernateTransactionManager -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate5.HibernateTransactionManager">

<!--註入 SessionFactory 對象 -->

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

<!-- 開啟事務註解 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

引入部分如下:

技術分享

【made by siwuxie095】

SSH框架分模塊開發