s2sh集成配置

--------------------Struts2--------------------

Struts.xml的配置如下:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

 

<struts>

<constant name="struts.devMode" value="true" />

<!-- 默认的视图主题 -->

    <constant name="struts.ui.theme" value="simple" />

    <constant name="struts.objectFactory" value="spring" />

    <package name="test" extends="struts-default">

            <action name="login" method="login">

<!--这里的classssh中是由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" />

                                <!--初始化时获取的连接数,取值应在minPoolSizemaxPoolSize之间。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 ####

注入hibernatesessionFactory,LocalSessionFactoryBean里面的哪个参数上,由property属性决定:configLocation,它是LocalSessionFactoryBean类的属性,对应的该类的代码在248行: public void setConfigLocation(Resource configLocation)            { this.configLocations = new Resource[] {configLocation};} 所以 propertyname的值是 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.xmlweb.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.xmlapplicationContext-actions.xmlapplicationContext-beans.xml。然后生成BeanFactory对象放入到ServletContext对象中。  可以在spring.jar中找到专门来读取这个配置文件的类: org.springframework.web.context. ContextLoaderListener    对应的相关代码可以在其中查看得到。

              -->

  <listener>

                                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

 

  <!-- CharacterEncodingFilterSpring为我们提供的字符编码过滤器,

                 当然我们可以自己写一个相关的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


转载于:https://my.oschina.net/u/1433614/blog/195357

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值