关于SSH框架xml配置版集成
1.首先是关于Spring与Hibernate两个框架的集成(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 使用c3p0配置基本参数 -->
<!--注意这里还可以使用其它数据源来配,例如dbcp,proxool(推荐),或者直接引入hibernate.cfg.xml(但是不推荐)-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value="bdqn"></property>
<property name="password" value="accp"></property>
<property name="maxPoolSize" value="100"></property>
<property name="minPoolSize" value="10"></property>
<property name="maxIdleTime" value="10"></property>
<property name="checkoutTimeout" value="2000"></property>
</bean>
<!--这里也可以使用jdbc.properties配置文件的方式来配置数据源,读取配置文件的时候可以使用
<context:property-placeholder location="classpath:jdbc.properties"/>
或者使用一个bean来管理
<bean id="dataSource" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
-->
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate属性的一些参数 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl">update</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<!-- 映射实体类 -->(推荐通过扫包的方式,当然也可一个文件一个文件的加)
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/po/</value>
</list>
</property>
</bean>
<!-- 注入baen (这里的bean是dao,biz之类的,具体看你自己的项目)-->
<bean id="employeeDao" class="daoimpl.EmployeeDaoImpl" p:sessionFactory-ref="sessionFactory"></bean>
<bean id="employeeBiz" class="bizimpl.EmployeeBizImpl" p:employeeDao-ref="employeeDao"></bean>
<!-- 配置事务管理器 -->(为了事务统一管理,这里要配置一个事务管理器)
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
</bean>
<!-- 配置声明式事务 -->(为哪些方法添加事务,以及配置相关的事务属性,事务属性以单一文件一一解释)
<!--其实这里就是一个环绕增加-->
<tx:advice id="txMgr" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="get*" propagation="SUPPORTS"/>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
</tx:attributes>
</tx:advice>
<!--织入--->
<aop:config>
<!--定义切点-->
<aop:pointcut expression="execution(public * bizimpl..*(..))" id="bizMethod"/>
<!--为哪些方法增加事务管理,注意这里使用的是(<aop:advisor>)-->
<aop:advisor advice-ref="txMgr" pointcut-ref="bizMethod"/>
</aop:config>
</beans>
2.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!--上下文参数,引入Spring配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置监听,作用是当你启动服务器的时候,就会启动监听,从而读取Spring文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置过滤器,这里的作用是配合hibernate的延迟加载,因为hibernate默认是延迟加载,要保证整个请求与响应的过程都打开和关闭session-->
<!--这里之所以要配置在struts2过滤器的前面,作用是能够是这个过滤器的范围更大一点-->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!--struts2的过滤器-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--欢迎页面-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.配置struts2
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--注意因为在集成SSH的时候,Struts2里面有一个struts-spring-plugin的插件,这样使得struts2的配置更加简单了,这里默认是直接按名字匹配,启动struts2的时候,它会访问spring的上下文,拿到名字所匹配的bean,下面红色部分,是改成用type来匹配-->
<!-- <constant name="struts.objectFactory.spring.autoWire" value="type"/> -->
<!--由于业务比较复杂,可能你需要在spring配置文件中配置bean,这时候这里的action的类就不能写类名,而是写配置在spring那里面的bean的id的名字-->
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="action.EmployeeAction" method="login">
<result>/index.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>