------------------siwuxie095
SSH 框架分模块开发
1、在Spring 核心配置文件中配置多个内容,容易造成
配置混乱,不利于维护
「分模块开发主要针对Spring 核心配置文件」
2、把Spring 核心配置文件中的一部分配置放到单独的
配置文件中,再在Spring 核心配置文件中引入单独的配
置文件即可
3、一般情况下,建议把Action 对象的配置放到单独的
配置文件中
「因为其它的配置基本不变,只有Action 对象的配置在
不断重复」
如:
user.xml:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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对象 --> <beanid="userAction"class="com.siwuxie095.action.UserAction" scope="prototype"> <propertyname="userService"ref="userService"></property> </bean>
<!--配置 Service对象 --> <beanid="userService"class="com.siwuxie095.service.UserService"> <propertyname="userDao"ref="userDaoImpl"></property> </bean>
<!--配置 Dao实现类对象 --> <beanid="userDaoImpl"class="com.siwuxie095.dao.impl.UserDaoImpl"> <propertyname="hibernateTemplate"ref="hibernateTemplate"></property> </bean>
<!--配置 HibernateTemplate对象 --> <beanid="hibernateTemplate"class="org.springframework.orm.hibernate5.HibernateTemplate"> <!--注入 SessionFactory对象 --> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean>
<!-- 关于 HibernateTemplate对象的配置,其实也可以放在核心配置文件中, 因为它也是基本不变的
其实关于分模块开发,把任何一部分拿出去放到单独的配置文件中都可以, 只是建议把 Action 对象的配置放到单独的配置文件中,因为只有 Action 对象的配置是不断重复的,而其它的配置却是基本不变的 -->
</beans> |
在applicationContext.xml 中引入 user.xml:
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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连接池 --> <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"> <propertyname="driverClass"value="com.mysql.jdbc.Driver"/> <!-- jdbc:mysql:///test_db是 jdbc:mysql://localhost:3306/test_db的简写 --> <propertyname="jdbcUrl"value="jdbc:mysql:///test_db"/> <propertyname="user"value="root"/> <propertyname="password"value="8888"/> </bean>
<!-- SessionFactory对象的创建交给 Spring进行管理 --> <beanid="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 数据库配置原本是在 Hibernate 核心配置文件中配置的, 现在 Hibernate核心配置文件不存在了,所以在这里注 入 dataSource
LocalSessionFactoryBean中有相关属性,所以可以 注入 --> <propertyname="dataSource"ref="dataSource"></property> <!--配置 Hibernate基本信息 --> <propertyname="hibernateProperties"> <props> <propkey="hibernate.show_sql">true</prop> <propkey="hibernate.format_sql">true</prop> <propkey="hibernate.hbm2ddl.auto">update</prop> <propkey="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 --> <propkey="hibernate.current_session_context_class"> org.springframework.orm.hibernate5.SpringSessionContext </prop> </props> </property> <!--引入映射配置文件 --> <propertyname="mappingResources"> <list> <value>com/siwuxie095/entity/User.hbm.xml</value> <!-- <value>....</value> --> </list> </property> </bean>
<!-- (2) --> <!-- 引入 src下的 user.xml配置文件
其实 classpath可以省略不写,即: <import resource="user.xml"/>
建议写上 --> <importresource="classpath:user.xml"/>
<!-- (3) --> <!--配置事务管理器 HibernateTransactionManager --> <beanid="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <!--注入 SessionFactory 对象 --> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean>
<!--开启事务注解 --> <tx:annotation-driventransaction-manager="transactionManager"/>
</beans> |
引入部分如下:
【made by siwuxie095】