学习三大框架Struts2 、 Hibernate 、 Spring时,涉及到三大框架的配置文件以及整合。今天就来详细写写三大框架配置文件的详细内容。
一 Spring的applicationContext.xml中的配置:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
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">
<!-- 配置由Spring IoC容器托管对象对应的被注解的类所在的包 -->
<context:component-scanbase-package="com.leipengzj"/>
<!-- 配置通过自动生成代理类实现AOP功能 -->
<aop:aspectj-autoproxy/>
<!--DateSource 使用c3p0连接池-->
<beanid="dateSource"class="com.mchange.v2.c3p0.ComboPooledDateSource"destroy-method="close">
<propertyname="driverClass"value="com.mysql.jdbc.Driver"></property>
<propertyname="jdbcUrl"value="jdbc:mysql:/3306/leiepngzj_table"></property>
<propertyname="user"value="root"></property>
<propertyname="password"value="root"></property>
</bean>
<!-- SessionFactory 配置 配置Spring提供的支持注解ORM映射的Hibernate会话工厂-->
<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation">
<propertyname="dateSource"ref="dateSource"></property>
<propertyname="congifLocation"value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置spring基于注解的声明式事务管理 -->
<beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 通过setter注入Hibernate会话工厂 -->
<propertyname="sessionFactory"ref="sessionFactory"></property>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
</beans>
二 Hibernate.cfg.xml配置:
<?xmlversion='1.0'encoding='UTF-8'?>
<!DOCTYPEhibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 数据库连接信息 ,其他的写到了spring配置文件中去 -->
<propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 其他配置 -->
<propertyname="show_sql">true</property>
<propertyname="hbm2ddl.auto">update</property>
<!-- 导入映射配置 -->
<mappingresource="../../.hbm.xml"/>
<mappingresource="../../.hbm.xml"/>
<mappingresource="../../.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三 struts.xml中的配置文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE strutsPUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 配置为开发模式 -->
<constantname="struts.devMode"value="true"></constant>
<!-- 配置扩展名为action -->
<constantname="struts.action.extension"value="action"></constant>
<!-- 当struts配置文件修改时,系统是否自动重新加载该文件,默认false,开发阶段最好打开 -->
<constantname="struts.configuration.xml.reload"value="true"></constant>
<!-- 全局视图 -->
<packagename="LunTanSystem"extends="struts-default">
<global-results>
<resultname="error">/error.jsp</result>
<resultname="ajax">/ajax.jsp</result>
<resultname="noLogin">/login.jsp</result>
</global-results>
<!-- 当与action整合后,class属性写的就是Spring中bean的属性 -->
<actionname="test"class="testAction">
<resultname="success">/test.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>
<!-- 配置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>
<!--配置Spring的监听器,用于初始化ApplicationContext.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoader</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>