spring与struts2、hibernate的集成

一、spring 与struts2的集成

 

spring与struts2集成有多种方式,这里只介绍简单的通过插件(strust2-spring-plugin.jar)进行集成

 

1、引入strust2-spring-plugin包


2、添加spring的配置文件

 

spring配置文件Applicatoncontext.xml默认添加位置在classpath下,如果需要更改名称或路径,需在web.xml中进行配置

 

    <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

 

3、struts.xml配置

 

spring与struts集成后Action也由spring容器管理,struts.xml中Action的class属性改成spring中配置的Bean

 

	<package name="user" namespace="/user" extends="default">
		<action name="*" class="user.userAction">
			<result name="edit" type="freemarker">
				/WEB-INF/user/user_edit.ftl
			</result>
		</action>
	</package>

 

4、spring配置文件详解

请查看后面

 


二、spring与hibernate的集成

 

spring与hibernate的集成后,hibernate.cfg.xm中与数据库的连接,OMP映射关系,及sessionfactory管理,全部由spring来控制

 

spring配置文件详解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <context:component-scan base-package="org.springframework.samples.petclinic.web"/>
	
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/forum"></property>  
        <property name="username" value="root"></property>  
        <property name="password" value=""></property> 
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">  
        	<ref bean="dataSource" />  
    	</property>  
    	<property name="hibernateProperties">  
        	<props>  
            	<prop key="hibernate.dialect">  
                	org.hibernate.dialect.MySQLDialect  
            	</prop>  
        	</props>  
    	</property>
    	<property name="packagesToScan">
   			<list>
    			<value>com.*</value>
   			</list>
  	</property>  
 <!--    	采用配置文件映射关系
 		<property name="mappingResources">  
        	<list>  
            	<value>com/forum/user/entity/User.hbm.xml</value>  
        	</list>  
    	</property>
 -->
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
	<!-- 配置事务管理 
	<bean id="transationService" class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<property name="transactionManager">
			<ref bean="transactionManager"/>
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="*">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>
	-->
	 <!-- 第一种自动代理,代理业务对象 声明事务
 	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  		<property name="beanNames">  
  	 		<list>  
    		<value>user.userManager</value>  
   			</list>  
  		</property>  
  		<property name="interceptorNames">  
   		<list>  
    		<value>transationService</value>  
   		</list>  
  		</property>  
 	</bean>
 	 -->  
 	<!-- 第二种采取定义切面的方式, 声明事务-->
 	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
 	 	<tx:attributes>
 	 		<tx:method name="*"/>
 	 	</tx:attributes>
 	</tx:advice>  
	
	<aop:config>
		<aop:pointcut id="txManager" expression="execution(* com.forum.*.*manager.*.*(..))" />
		<aop:advisor advice-ref="transactionAdvice" pointcut-ref="txManager"/>
	</aop:config>
	
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>
	
	<bean id="user.userDao" class="com.forum.user.dao.impl.UserDaoImpl">
		<property name="hibernateTemplate">
			<ref bean="hibernateTemplate"/>
		</property>
	</bean>
	
	<bean id="user.userManager" class="com.forum.user.manager.impl.UserManagerImpl">
		<property name="userDao">
			<ref bean="user.userDao"/>
		</property>
	</bean>
	
	<bean id="user.userAction" class="com.forum.user.action.UserAction">
		<property name="userManager">
			<ref bean="user.userManager"/>
		</property>
	</bean>

</beans>

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