欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring整合ssh的配置文件怎么配置?

程序员文章站 2022-04-30 12:05:21
...

在编写applicationContext.xml文件时,要注意各个bean之间的引用关系
applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	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/aop
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		">
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/empdb">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="java"></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>
				<prop key="hibernate.show_sql">
					true
				</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/po/Dep.hbm.xml</value>
				<value>com/po/Users.hbm.xml</value>
				<value>com/po/Welfare.hbm.xml</value>
				<value>com/po/Empwelfare.hbm.xml</value>
				<value>com/po/Salary.hbm.xml</value>
				<value>com/po/Emp.hbm.xml</value></list>
		</property></bean>
	<bean id="DepDAO" class="com.dao.DepDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="UsersDAO" class="com.dao.UsersDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="WelfareDAO" class="com.dao.WelfareDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="EmpwelfareDAO" class="com.dao.EmpwelfareDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="SalaryDAO" class="com.dao.SalaryDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="EmpDAO" class="com.dao.EmpDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>

	<!-- 注入DaoService -->
	<bean id="daos" class="com.service.DaoService">
		<property name="depDAO" ref="DepDAO"></property>
		<property name="empDAO" ref="EmpDAO"></property>
		<property name="empwelfareDAO" ref="EmpwelfareDAO"></property>
		<property name="salaryDAO" ref="SalaryDAO"></property>
		<property name="usersDAO" ref="UsersDAO"></property>
		<property name="welfareDAO" ref="WelfareDAO"></property>
	</bean>
	<!-- 注入biz -->
	<bean id="depbiz" class="com.biz.imp.DepBiz">
		<property name="daoService" ref="daos"></property>
	</bean>
	<bean id="empbiz" class="com.biz.imp.EmpBiz">
		<property name="daoService" ref="daos"></property>
	</bean>
	<bean id="usersbiz" class="com.biz.imp.UsersBiz">
		<property name="daoService" ref="daos"></property>
	</bean>
	<bean id="welfarebiz" class="com.biz.imp.WelfareBiz">
		<property name="daoService" ref="daos"></property>
	</bean>
	<!-- 注入bizService -->
	<bean id="bizs" class="com.service.BizService">
		<property name="depBiz" ref="depbiz"></property>
		<property name="empBiz" ref="empbiz"></property>
		<property name="usersBiz" ref="usersbiz"></property>
		<property name="welfareBiz" ref="welfarebiz"></property>
	</bean>
	<!-- 注入Action类 -->
	<bean id="EmpAction" class="com.action.EmpAction">
		<property name="bizService" ref="bizs"></property>
	</bean>
	<bean id="UsersAction" class="com.action.UsersAction">
		<property name="bizService" ref="bizs"></property>
	</bean>
	<!-- 注入Hibernate的事务管理器 -->
	<bean id="txmanager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- 注入事务通知属性 -->
	<tx:advice id="txavd" transaction-manager="txmanager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="del*" propagation="REQUIRED"/>
			<tx:method name="find*" propagation="NOT_SUPPORTED"/>
		</tx:attributes>
	</tx:advice>
	<!-- 注入事务的切入点 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.biz.*.* (..))" id="bizpoint"/>
		<aop:advisor advice-ref="txavd" pointcut-ref="bizpoint"/>
	</aop:config>
</beans>

Struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.ui.theme" value="simple"></constant>
	<package name="student" namespace="/" extends="struts-default">
		<!-- {2}Action表示spring中配置的Action的id值 -->
		<action name="*_*" class="{2}Action" method="{1}">
			<result name="ok" type="redirect">${path}</result>
			<result name="fail" type="redirect">fail.jsp</result>
		</action>
	</package>
</struts>    

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">
  <display-name></display-name>	
  <!-- ******************spring容器的启动配置,替代ApplicationContext对象的创建************* -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- ***********************spring配置结束************************** -->
 <!-- ************************配置Hiberante的session已关闭异常的过滤处理******************** -->
 <filter>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- ************************************Hiberante过滤处理结束************************** -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <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></web-app>

最后,一般我们会在src目录下新建一个log4j.properties的日志文件,内容如下(此文件可帮助我们在控制台打印错误信息):
log4j.properties日志文件

#All level less than INFO will be logged
log4j.rootLogger=info, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %m%n