S2SH整合配置篇

web.xml
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

	<!-- 用于初始化Spring容器的Listener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

	<!-- 定义整合SiteMesh必须的ActionContextCleanUp Filter -->
	<filter>
		<filter-name>struts-cleanup</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
	</filter>
	<!-- 定义整合SiteMesh和核心 Filter -->
	<filter>
		<filter-name>sitemesh</filter-name>
		<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
	</filter>
	<!-- 定义Struts2的FilterDispathcer的Filter -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
	<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>img</servlet-name>
        <servlet-class>org.yeeku.web.AuthImg</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>img</servlet-name>
	    <url-pattern>/authImg</url-pattern>
    </servlet-mapping>

</web-app>

 

applicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

	<!-- 定义数据源Bean,使用C3P0数据源实现 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver"/>
		<!-- 指定连接数据库的URL -->
		<property name="jdbcUrl" value="jdbc:mysql://localhost/reg_login"/>
		<!-- 指定连接数据库的用户名 -->
		<property name="user" value="root"/>
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="root"/>
		<!-- 指定连接数据库连接池的最大连接数 -->
		<property name="maxPoolSize" value="20"/>
		<!-- 指定连接数据库连接池的最小连接数 -->
		<property name="minPoolSize" value="1"/>
		<!-- 指定连接数据库连接池的初始化连接数 -->
		<property name="initialPoolSize" value="1"/>
		<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
		<property name="maxIdleTime" value="20"/>
	</bean>

    <!--定义了Hibernate的SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
				<value>User.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.jdbc.batch_size">20</prop> 
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>


    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
	    <!--  事务拦截器bean需要依赖注入一个事务管理器 -->
        <property name="transactionManager" ref="transactionManager"/>
    	<property name="transactionAttributes">
		    <!--  下面定义事务传播属性-->
		    <props>
			    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
		    	<prop key="*">PROPAGATION_REQUIRED</prop>
		    </props>
	    </property>
	</bean>

    <!-- 定义BeanNameAutoProxyCreator-->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	    <!--  指定对满足哪些bean name的bean自动生成业务代理 -->
	    <property name="beanNames">
            <!--  下面是所有需要自动创建事务代理的bean-->
            <list>
                <value>mgr</value>
            </list>
            <!--  此处可增加其他需要自动创建事务代理的bean-->
	    </property>
        <!--  下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
        <property name="interceptorNames">
            <list>
                <!-- 此处可增加其他新的Interceptor -->
                <value>transactionInterceptor</value> 
            </list>
        </property>
    </bean>

    <bean id="mgr" class="org.yeeku.service.impl.UserManagerImpl">
        <property name="userDao" ref="userDao"/>
    </bean>

	<bean id="userDao" class="org.yeeku.dao.hibernate.UserDaoHibernate">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>

</beans>

 struts整合spring有两种方式,一种方式是让spring来管理action,即在配置action的class属性时不指定该action的实现类而是指向spring容器中的beanid

第二种整合方式是利用spring的自动装配,当spring在创建action的实例时,利用spring的自动装配策略,将对应的业务逻辑组件注入action实例

你可能感兴趣的:(spring,Hibernate,bean,servlet,配置管理)