--------------------Struts2--------------------web
Struts.xml的配置以下:spring
<?xml version="1.0" encoding="UTF-8" ?>sql
<!DOCTYPE struts PUBLICexpress
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"apache
"http://struts.apache.org/dtds/struts-2.0.dtd">session
<struts>oracle
<constant name="struts.devMode" value="true" />app
<!-- 默认的视图主题 -->ssh
<constant name="struts.ui.theme" value="simple" />jsp
<constant name="struts.objectFactory" value="spring" />
<package name="test" extends="struts-default">
<action name="login" method="login">
<!--这里的class在ssh中是由Spring配置的baen,不是真实存在的类。若是不是ssh,只是配置struts,则是真实存在的类,如com.neusoft.test.action.userAction -->
<result name="success">/main.jsp</result>
<result name="fail">/fail.jsp</result>
</action>
</package>
</struts>
Struts.xml是由web.xml配置文件读取,下面的是读取的配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!--<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->
<!--struts2使用这两个类均可以-->
<!--struts2会默认去类路径下去读struts.xml配置文件,也就是src/下,通常将struts.xml配置文件放在src/下
若是将struts.xml配置文件放在WEB-INF/**/,则须要添加下面的配置,用来指定struts.xml的路径-->
<!--
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../**/struts.xml</param-value>
</init-param>
-->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
--------------------Spring--------------------
Spring的配置文件是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-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--
咱们的action对象,在此以前是struts容器给咱们建立的, 而不是Spring IoC给咱们建立的,即不是从IOC容器中拿出来, 若是要利用Spring IoC容器生成该Actiond对象并取出来, 咱们就必须须要获得BeanFactory对象,这样的话虽然能够在这类里实现, 可是产生了对BeanFactory依赖!! 因此在"applicationContext.xml"文件中,仅凭以下配置:
<bean id="loginAction" class="com.neusoft.web.actions.LoginAction">
<property name="userManager" ref="userManager"/></bean>
是不起任何做用的!也注入不进来。 因此要修改配置, 将id 属性修改成name
* spring 默认是建立单实例的,在这里,咱们能够经过scope="prototype"来建立多实例。
-->
<bean name="userAction" class="com.neusoft.test.action.UserAction">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost :1521:orcl" />
<property name="user" value="hr" />
<property name="password" value="orcl" />
<!--初始化时获取的链接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="1" />
<!--链接池中保留的最小链接数。-->
<property name="minPoolSize" value="1" />
<!--链接池中保留的最大链接数。Default: 15 -->
<property name="maxPoolSize" value="30" />
<!--最大空闲时间,60秒内未使用则链接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60" />
<!--当链接池中的链接耗尽的时候c3p0一次同时获取的链接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--每60秒检查全部链接池中的空闲链接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="60" />
</bean>
<!--
#### 配置sessionFactory ####
注入hibernate的sessionFactory,到LocalSessionFactoryBean里面的哪一个参数上,由property属性决定:configLocation,它是LocalSessionFactoryBean类的属性,对应的该类的代码在248行: public void setConfigLocation(Resource configLocation) { this.configLocations = new Resource[] {configLocation};} 因此 property的name的值是 “configLocation”。要把hibernate的配置文件hibernate.cfg.xml注入进来,即LocalSessionFactoryBean类的属性中。那么如何找到这个hibernate的配置文件,咱们需使用spring给咱们实现的一个协议classpath:******,这样spring就会到classpath路径下搜索咱们这个配置文件,而后就能帮助咱们建立出sessionFactory
-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!--
#### 配置事务管理器 #### 事务管理器是由Spring
的HibernateTransactionManager类来封装实现的,只要配置进来便可。首先要将sessionFactory注入进来,见代码:HibernateTransactionManager类的: 第:135行:private SessionFactory sessionFactory; 第:179行: public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory;} 这样就能够把刚才容器帮咱们实现的sessionFactory实例注入进来了。 <ref bean="sessionFactory"/>这个ref引用的就是25行的id.
-->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" read-only="false" />
<tx:method name="del*" propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" propagation="REQUIRED" read-only="false" />
<tx:method name="find*" read-only="true" />
<!--
对如上开头命名的方法之外的方法的事务处理应该以下配置: <tx:method name="*" read-only="true"/> 配置为只读事务,若是是只读事务,会提升性能,即当你更新了某一个对象的时候, 再也不作脏数据检查了,性能上有必定的优化。
-->
</tx:attributes>
</tx:advice>
<aop:config>
<!-- 其实这里就是在指定(或者说定义)切入点 -->
<aop:pointcut id="allManagerMethod"
expression="execution(* com.neusoft.test.service..*.*(..))" />
<!-- 其实就是定义并指定在该切入点使用什么切面(也就是建言) -->
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config>
</beans>
applicationContext.xml由web.xml配置读取,下面的是读取的配置:
<!-- 注意:Spring的配置文件applicationContext.xml的位置能够不是固定的。
因为本项目中咱们的applicationContext.xml是放在了src classpath根目录下,
因此咱们应该以下配置它,固然也能够放在WEB-INF下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml </param-value>
<!--<param-value> /WEB-INF/applicationContext.xml</param-value>-->
</context-param>
-->
<!-- 本项目中,我根据配置文件中内容的不一样,拆分为3个文件,
都是以"applicationContext-"开头,因此这里,我以*来匹配他们。
classpath:只会到你的class路径中查找文件;
classpath*:不只包含class路径,还包括jar文件中(class路径)进行查找
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-*.xml</param-value>
</context-param>
<!-- 接下来还须要配置Spring 的上下文加载监听器, 该监听器负责读取咱们的配置变量contextConfigLocation, 获得咱们的配置文件 applicationContext-common.xml、applicationContext-actions.xml、applicationContext-beans.xml。而后生成BeanFactory对象放入到ServletContext对象中。 能够在spring.jar中找到专门来读取这个配置文件的类: org.springframework.web.context. ContextLoaderListener 对应的相关代码能够在其中查看获得。
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 该CharacterEncodingFilter是Spring为咱们提供的字符编码过滤器,
固然咱们能够本身写一个相关的Filter链进来,也可使用Spring提供的。
-->
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
--------------------Hibernate--------------------
Hibernate的配置文件是hibernate.cfg.xml,在ssh中使用时配置以下:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--指定映射文件-->
<mapping resource="com/neusoft/test/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
在ssh中使用时,hibernate.cfg.xml是由Spring配置文件中的sessionFactory读取,以下:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value><!-- hibernate.cfg.xml 放在src下-->
<value>/WEB-INF/hibernate.cfg.xml</value><!-- hibernate.cfg.xml 放在WEB-INF下-->
</property>
</bean>
在ssh中使用时,还须要在web.xml配置以下:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Hibernate单独使用时, hibernate.cfg.xml的配置以下:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost :1521:orcl</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.connection.password">orcl</property>
<property name="hibernate.connection.pool.size">20 </property>
<property name="hibernate.show_sql">true </property>
<property name="jdbc.fetch_size">50 </property>
<property name="jdbc.batch_size">23 </property>
<property name="jdbc.use_scrollable_resultset">false </property>
<property name="Connection.useUnicode">true </property>
<!--指定映射文件-->
<mapping resource="com/neusoft/test/entity/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
Hibernate单独使用时,Hibernate.cfg.xml路径问题:
Hibernate.cfg.xml路径默认的是在src下,使用时以下
SessionFactory sessionFactory= new Configuration().configure().buildSessionFactory();
//默认访问src/Hibernate.cfg.xml
SessionFactory sessionFactory= new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
//上面俩个写法是同样的,都是访问src/Hibernate.cfg.xml
SessionFactory sessionFactory= new Configuration().configure(/cfg/hibernate.cfg.xml).buildSessionFactory();
//此方法是访问src/cfg/Hibernate.cfg.xml
Session session = sessionFactory.openSession();
放在WEB-INF下 ,使用时以下:
使用绝对路径
File file = new File("E:/Documents and Settings/Administrator/Workspaces/MyEclipse 8.6/hibernate/WebRoot/WEB-INF/hibernate.cfg.xml");
SessionFactory sessionFactory= new Configuration().configure(file).buildSessionFactory();
相对路径,下面的方法还没调出来
SessionFactory sessionFactory = new Configuration().configure("WEB-INF/hibernate.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
总结:hibernate单独使用时,Hibernate.cfg.xml最好放在src下