spring總結:(4)SSH框架的配置檔案
阿新 • • 發佈:2019-01-30
1.載入db.properties
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties"></property>
</bean>
2.配置資料來源DataSource
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driver}"></property> <property name="jdbcUrl" value="${url}"></property> <property name="user" value="${name}"></property> <property name="password" value="${pass}"></property> </bean>
3.配置sessionFactory注入dataSource
<bean id="sessionFactoryDataSource" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate相關屬性 --> <property name="hibernateProperties"> <props> <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="show_sql">true</prop> <prop key="format_sql">true</prop> <prop key="hbm2ddl.auto">update</prop> </props> </property> <!-- 配置對映檔案位置 --> <property name="mappingResources"> <list> <value>cn/jzh/entity/User.hbm.xml</value> </list> </property> </bean>
4.配置hibernateTemplate並注入sessionFactory
<bean id="hibernateTemplateSessonFactory" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactoryDataSource"></property>
</bean>
5.配置UserDao並注入hibernateTemplate
<bean id="userDao" class="cn.dao.impl.UserDaoImpl"> <property name="ht" ref="hibernateTemplateSessonFactory"></property> </bean>
6.配置UserService並注入UserDao
<bean id="service" class="cn.service.UserServiceImpl">
<property name="dao" ref="userDao"></property>
</bean>
7.配置UserinfoAction,並注入UserService
<bean id="userAction" class="cn.action.UserAction">
<property name="service" ref="service"></property>
</bean>
最後:配置spring宣告事物
配置hibernteTransactionManager,注入sessionFactory
<bean id="transaction" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryDataSource"></property>
</bean>
配置事務的通知
<tx:advice id="txAdvice" transaction-manager="transaction">
<!-- 事物的傳播的行為 -->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 定義切面 -->
<aop:pointcut expression="execution(* cn.jzh.service.*.*(..))" id="mypointcut"/>
<!-- 將事務通知和切面告知通知者 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
</aop:config>