整合原理
- Struct2與Spring整合:將Action對象交與Spring容器負(fù)責(zé)創(chuàng)建
- Hibernate與Spring整合:將SessionFactory交與Spring容器來維護,Spring負(fù)責(zé)Session維護及aop事務(wù)
導(dǎo)包
-
hibernate
- hibernate/lib/required
- hibernate/lib/jpa(java persist api java的持久化規(guī)范 接口)
- jdbc包 ---- mysql-connector-java
-
Struts2
struts-blank.war/WEB-INF/lib/*
-
struts整合spring插件包 ---- struts2-spring-plugin
注意:這個包一旦導(dǎo)入,那么struts2在啟動時就會尋找spring容器.找不到將會拋出異常
-
Spring
- 基本:4+2 ---- core|beans|context|expression|logging|log4j
- 整合web:web包 ---- spring-web
- 整合aop:4個 ---- spring-aop|spring-aspect|aop聯(lián)盟|aopweaving
- 整合Hibernate和事務(wù):4個 ---- spring-jdbc|spring-tx|c3p0|spring-orm
- 整合junit4測試:test包 ---- spring-test
標(biāo)簽庫 ---- jstl
配置Spring
創(chuàng)建配置文件并導(dǎo)入約束(4個)
beans|context|aop|tx-
配置Spring隨項目啟動
web.xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
配置Structs2
配置struts2主配置文件struts.xml
-
配置struts2核心過濾器到web.xml
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
struts2與spring整合
-
配置常量
<!--將action的創(chuàng)建交與Spring容器--> <constant name="struts.objectFactory" value="spring"/> <!--Spring負(fù)責(zé)裝配Action的依賴,默認(rèn)打開--> <constant name="struts.objectFactory.spring.autoWire" value="name"/>
-
整合
-
struts2自己創(chuàng)建action,spring負(fù)責(zé)組裝依賴屬性
//struts.xml <action name="demo" class="com.ssh.demo.view.DemoAction"> <result name="success">/index.jsp</result> </action> //applicationContext <bean name="action" class="com.ssh.demo.view.DemoAction"/> <bean name="userService" class="com.ssh.demo.service.UserServiceImpl"/>
-
spring負(fù)責(zé)創(chuàng)建action以及組裝
//struts <!--class上填寫spring中Action對象的name屬性值,完全由Spring管理Action的生命周期豹储, 包括Action的創(chuàng)建。備注需要手動組裝依賴屬性--> <action name="demo" class="demoAction"> <result name="success">/index.jsp</result> </action> //applicationContext.xml <bean name="userService" class="com.ssh.demo.service.UserServiceImpl"/> <!--Action對象作用域一定是多例的,這樣才符合struts2框架--> <bean name="demoAction" class="com.ssh.demo.view.DemoAction" scope="prototype"> <property name="userService" ref="userService"/> </bean>
-
Hibernate配置
導(dǎo)入實體類或orm元數(shù)據(jù)
-
配置主配置文件
<session-factory> <!-- 數(shù)據(jù)庫驅(qū)動 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 數(shù)據(jù)庫url --> <property name="hibernate.connection.url">jdbc:mysql://39.106.1.213:3306/hb? characterEncoding=utf-8</property> <!-- 數(shù)據(jù)庫連接用戶名 --> <property name="hibernate.connection.username">xxx</property> <!-- 數(shù)據(jù)庫連接密碼 --> <property name="hibernate.connection.password">xxx</property> <!-- 數(shù)據(jù)庫方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 將hibernate生成的sql語句打印到控制臺 --> <property name="hibernate.show_sql">true</property> <!-- 將hibernate生成的sql語句格式化(語法縮進) --> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 引入orm元數(shù)據(jù) 路徑書寫: 填寫src下的路徑 --> <mapping class="com.ssh.demo.entity.User"/> </session-factory>
Spring整合hibernate
將sessionFactory對象交給spring容器管理\
- 配置方案一:仍然使用外部的hibernate.cfg.xml配置信息
<!--將SessionFactory配置到Spring-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
- 配置方案二:不使用hibernate.cfg.xml配置文件,在spring中配置hibernate配置信息
<!--將SessionFactory配置到Spring-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="hibernateProperties" >
<props>
<!--必選配置-->
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://39.106.1.213:3306/hb? characterEncoding=utf-8</prop>
<prop key="hibernate.connection.username">xxx</prop>
<prop key="hibernate.connection.password">xxx</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!--可選配置-->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!--xml配置:引入orm元數(shù)據(jù),指定orm元數(shù)據(jù)所在包路徑,spring會自動讀取包中所有配置-->
<property name="mappingDirectoryLocations" value="classpath:com/ssh/demo/entity"/>
<!--注解配置:spring會自動讀取包中注解的配置-->
<property name="packagesToScan">
<list>
<value>com.ssh.demo.entity</value>
</list>
</property>
</bean>
Spring整合c3p0連接池
-
配置db.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://39.106.1.213:3306/hb?characterEncoding=utf-8 jdbc.user=xxx jdbc.password=xxx
-
引入連接池到spring中
<!--配置c3bO連接池--> <context:property-placeholder location="classpath:db.properties"/> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean>
-
將連接池注入給SessionFactory
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 將連接池注入到SessionFactory中,hibernate通過連接池獲得連接--> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties" > <props> <!--必選配置--> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!--可選配置--> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!--引入orm元數(shù)據(jù),指定orm元數(shù)據(jù)所在包路徑,spring會自動讀取包中所有配置--> <!--property name="mappingDirectoryLocations" value="classpath:com/ssh/demo/entity"/>--> <property name="packagesToScan"> <list> <value>com.ssh.demo.entity</value> </list> </property> </bean>
Spring整合hibernate事務(wù)管理
-
Dao類創(chuàng)建:繼承HibernateDaoSupport
UserDaoImpl extends HibernateDaoSupport
-
hibernate模板的操作
getHibernateTemplate()
-
spring中配置dao
<bean name="userDao" class="com.ssh.demo.dao.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean name="userService" class="com.ssh.demo.service.UserServiceImpl"> <property name="userDao" ref="userDao"/> </bean> <!--Action對象作用域一定是多例的懂讯,這樣才符合struts2框架--> <bean name="demoAction" class="com.ssh.demo.view.DemoAction" scope="prototype"> <property name="userService" ref="userService"/> </bean>
Spring的aop操作
-
將核心事務(wù)管理器配置到spring
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
-
配置事務(wù)
-
xml
-
配置通知
<!-- 配置事務(wù)通知 --> <tx:advice transaction-manager="transactionManager" id="txadvice"> <tx:attributes> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice>
-
配置織入
<!-- 配置織入--> <aop:config> <!-- 配置切點表達(dá)式--> <aop:pointcut id="txPc" expression="execution(* com.ssh.demo.service.*.*(..))"/> <!-- 配置切面:通知+切點--> <!-- advice-ref:通知名稱--> <!-- pointcut-ref:切點名稱--> <aop:advisor advice-ref="txadvice" pointcut-ref="txPc"/> </aop:config>
-
-
注解
-
開啟注解事務(wù)
<tx:annotation-driven transaction-manager="transactionManager"/>
-
Service類中使用注解(類或方法)
@Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = false)
-
-
擴大session作用范圍(配置filter)
? 為了避免使用懶加載時出現(xiàn)no-session問題.需要擴大session的作用范圍
? 任何Filter一定要在strutsFilter之前調(diào)用
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>