SSH集成配置文件参考

本文详细介绍了如何在Spring MVC框架中配置ContextLoaderListener、使用applicationContext.xml进行依赖注入,以及如何设置Struts2核心过滤器和OpenSessionInViewFilter。同时,详细解释了如何在applicationContext.xml中配置数据源、SessionFactory、Dao、Service、Action和事务管理器,并进行了AOP配置。

1、web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
  <!--配置ContextLoaderListener:默认在WEB-INF下查找applicationContext.xml  -->	
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 通过 context-param节点指定查找的位置  contextConfigLocation 是固定写法-->	
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- sturts2核心过滤器 -->
  <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>
  <!-- OpenSessionInViewFilter -->
  <filter>
  	<filter-name>osivFilter</filter-name>
  	<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
	<filter-name>osivFilter</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2、applicationContext.xml配置文件如下:

<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!-- 配置dataSource -->
  <bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource">
    <!-- driverClassName -->
	<property name = "driverClassName" value = "com.mysql.jdbc.Driver"></property>
    <!-- url -->
	<property name = "url" value = "jdbc:mysql://localhost:3306/ssh"></property>
    <!-- username -->
	<property name = "username" value = "root"></property>
    <!-- password -->
	<property name = "password" value = "root"></property>
  </bean>
  <!-- 配置sessionFactory-->
  <bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<!-- 注入dataSource -->
	<property name = "dataSource" ref = "dateSource"></property>
	<!-- hibernate映射文件 -->
	<property name = "mappingResources">
		<list>
			<value>com/sxt/ssh/entity/Userinfo.hbm.xml</value>
			<value>com/sxt/ssh/entity/Prodduct.hbm.xml</value>
		</list>
	</property>
	<!-- hibernate属性 -->
	<property name = "hibernateProperties">
		<props>
			<prop key = "hibernate.show_sql">true</prop>
			<prop key = "hibernate.format_sql">true</prop>
			<prop key = "hibernate.dialect">MySQL5Dialect</prop>
		</props>
	</property>
  </bean>
  <!-- 配置Dao,并注入sessionFactory -->
  <bean id = "userinfoDao" class = "com.sxt.ssh.dao.impl.UserinfoDaoImpl">
	<property name = "sessionFactory" ref = "sessionFactory"></property>
  </bean>
  <!-- 配置service,并注入Dao-->
  <bean id = "userinfoService" class = "com.sxt.ssh.service.impl.UserinfoServiceImpl">
	<property name = "userinfoDao" ref = "userinfoDao"></property>
  </bean>
  <!-- 配置Action, 并注入service -->
  <bean id = "userinfoAction" class = "com.sxt.ssh.action.UserinfoAction" scope = "prototype">
	<property name = "userinfoService" ref = "userinfoService"></property>
  </bean>
  <!-- 配置事务管理器,注入sessionFactory -->
  <bean id = "transactionManager" class =			                        "org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name = "sessionFactory" ref = "sessionFactory"></property>
  </bean>
  <!-- 事务通知 -->
  <tx:advice id = "txAdvice" transaction-manager = "transactionManager">
	<tx:attributes>
		<tx:method name = "add*" propagation = "REQUIRED"></tx:method>
		<tx:method name = "update*" propagation = "REQUIRED"></tx:method>
		<tx:method name = "delete*" propagation = "REQUIRED"></tx:method>
		<tx:method name = "*" propagation = "SUPPORTS"></tx:method>
	</tx:attributes>
  </tx:advice>
  <aop:config>
  <!-- 定义切入点 -->
	<aop:pointcut expression = "excution(* com.sxt.ssh.service.impl.UserinfoService.*(..))" id = "mypointcut"></aop:pointcut>
  <!-- 通知者 -->
    <aop:advisor advice-ref = "txAdvice" pointcut-ref = "mypointcut"><aop:advisor>
  </aop:config>
</bean>



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值