S2SH三大框架的整合(配置文件篇)

 首先看一下分工吧:

          Struts2做的MVC的流程框架,主要完成从客户端访问到选择anction的过程,其中过滤器起到了Controller的作用,action属于model,而jsp则是view页面的展示。

         Spring主要利用Ioc的特长来管理各种对象:action,service,dao,数据访问源,Hibernate的核心对象SessionFactory等,还有就是声明式事务的管理等。

         而Hibernate框架主要是实体层和数据库中表的映射,以及封装JDBC代码,提供相应的方法,供我们选择。这样从前台页面一直到后台数据库,都有了框架帮我们维护,使我们的开发变得简单快捷,效率大大提高。当然了,前提是我们对这些框架能够灵活的运用。

        再者,就是需要三大框架的导入的对应jar包,这里不再列出。以及对应的配置文件,这里需要说明一下,由于Hibernate框架的核心对象SessionFactory交给了Spring框架进行维护,这里就不需要hibernate.cfg.xml配置文件了,我们将这些信息写到Spring核心配置文件中即可。

         好,看一下各个配置文件的编写吧!

          1,web.xml文件的几个重点项:  

1.    

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

3.  <web-app version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"

4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

5.  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

6.  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

7. 

8.   <!-- 服务器启动时,通过监听器初始化Spring的配置环境 

9.      监听器,默认加载文件是:/WEB-INF/classes/applicationContext.xml  

10. --> 

11.  

12. <listener>

13.      <listener-class>

14.           org.springframework.web.context.ContextLoaderListener

15.       </listener-class>

16. </listener>

17. <!--用于指定Spring的配置文件路径,applicationContext*.xml实现多文件加载)-->

18. <context-param>

19.      <param-name>contextConfigLocation</param-name>

20.      <param-value>classpath:applicationContext*.xml</param-value>

21. </context-param>

22.  

23. <!-- OpenSessionInViewFilter过滤器需要配置在Struts2框架过滤器前面,否则不起作用。 -->  

24.  

25. <filter>

26.           <filter-name>openSessionInView</filter-name>

27.           <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

28. </filter>

29.

30. <filter-mapping>

31.      <filter-name>openSessionInView</filter-name>

32.      <url-pattern>/*</url-pattern>

33. </filter-mapping>

34.  

35. <!-- 配置框架的核心调度器 -->  

36.  

37. <filter>

38.      <filter-name>struts2</filter-name>

39.      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

40. </filter>

41.  

42. <filter-mapping>

43.      <filter-name>struts2</filter-name>

44.      <url-pattern>/*</url-pattern>

45. </filter-mapping>

46.

47. <welcome-file-list>

48.      <welcome-file>index.jsp</welcome-file>

49. </welcome-file-list>

50. </web-app>

51.  

     2,Struts2核心配置文件的编写:

1.  <package name="example" namespace="/user" extends="struts-default">  

2.         <action name="userAction_*" class="userAction" method="{1}">  

3.             <result name="success" type="dispatcher">/success.jsp</result>  

4.             <result name="login" type="redirect">/login.jsp</result>  

5.         </action>  

6.     </package>  
      3Spring核心配置文件的编写:

1.                

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

3.  <beans xmlns="http://www.springframework.org/schema/beans"

4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"

5.  xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

6.  xmlns:jee="http://www.springframework.org/schema/jee"

7.  xsi:schemaLocation="

8.       http://www.springframework.org/schema/beans

9.       http://www.springframework.org/schema/beans/spring-beans.xsd

10.      http://www.springframework.org/schema/context

11.      http://www.springframework.org/schema/context/spring-context.xsd

12.      http://www.springframework.org/schema/tx

13.      http://www.springframework.org/schema/tx/spring-tx.xsd

14.      http://www.springframework.org/schema/aop

15.      http://www.springframework.org/schema/aop/spring-aop.xsd

16.      http://www.springframework.org/schema/jee

17.      http://www.springframework.org/schema/jee/spring-jee.xsd

18.      "> 

19. <!--注解事务驱动 -->

20. <tx:annotation-driventransaction-manager="transactionManager"/>   

21.

22. <!--事务管理器 -->

23. <beanid="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

24.      <propertyname="sessionFactory" ref="sessionFactory"/>

25. </bean>

26.

27. <!--SessionFactory -->

28. <beanid="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

29.      <propertyname="dataSource" ref="dataSource"/>

30.      <propertyname="hibernateProperties">

31.          <props>

32.              <propkey="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>

33.              <propkey="hibernate.show_sql">true</prop>

34.              <propkey="hibernate.format_sql">false</prop>

35.          </props>

36.      </property>

37.      <propertyname="mappingLocations">

38.          <value>classpath:cn/itcast/erp/**/vo/*Model.hbm.xml</value>

39.      </property>

40. </bean>

41.

42. <!--DataSource -->

43. <beanid="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

44.      <propertyname="driverClassName" value="com.mysql.jdbc.Driver"/>

45.      <propertyname="url" value="jdbc:mysql://localhost:3306/mytestdb"/>

46.      <propertyname="username" value="root"/>

47.      <propertyname="password" value="123456"/>

48. </bean>

49. </beans>

  

50.  
 4,在Dao层的编写中,这里简单说一下如何获得session对象,去进行相关方法的调用,完成我们的数据操作功能。Dao层首先需要实现我们的HibernateDaoSupport,以便使用此父类中的getHibernateTemplate方法,进行相关方法的调用,先看一个查询方法的实现:

1.     //参数需要final修饰        public User select(final User user) throws Exception {   

2.      //HibernateTemplate模板类不能直接支持Query,Criteria接口操作,所以可以通过回调函数传递Session对象,来使用这两个接口。    

3.      //当调用execute方法时,execute方法会回调参数对象的重写方法(doInHibernate).  

4.      //这里的利用匿名内部类,充当了一个回调函数,就是为了使用类中的Session对象  

5.      return (User)this.getHibernateTemplate().execute(new HibernateCallback(){  

6.          //称之为回调函数,一般由框架负责调用执行。  

7.          public Object doInHibernate(Session session) throws HibernateException,SQLException {  

8.                

9.           //在匿名内部类中引用方法的局部变量,必须是final修饰的。                  

10.               

11.             Query query = session.createQuery("from User u where u.usercode=? and u.userpswd=?");                 

12.               

13.             query.setParameter(0, user.getUsercode());  

14.             query.setParameter(1, user.getUserpswd());  

15.   

16.             return query.uniqueResult();  

17.         }             

18.     });  

19. }  

         对于其它配置文件,这里不再给出,和单自使用框架差不多!当然对于这些配置文件,我们也可以根据实际业务进行划分,是每一个配置文件的功能单一,容量不至于过大,方便我们的管理。

         最后简单说一下,三大框架应用中优化的考虑问题吧!

          

 Struts2:

1, 发布时需要关闭开发模式(DevMode)

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

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

 2,不使用用不到的拦截器,拦截器等各种组件越多,性能越低,奔着一个达到目标使用最少的原则,进行相关开发。

   3,过滤器的过滤规则尽量不要使用/*,这里我们开发可以制定规则,将过滤器的过滤范围降低到最小,方便框架的快速过滤!

         Spring:

       1,尽量不要采用自动装配,使用手动装配指明方向,框架能够更快的寻找到相关的类。

  2,尽量使用延迟加载功能,这样可以减少和数据库的通信,提高性能。

         Hibernate:

  1,所有框架使用较新的版本,可以提供更好的性能支持,毕竟什么东西都是向着更好的方向发展。但是也不要一味的追求最新,如果有bug或什么情况还是不好的,奔着一个在有把握的基础上寻求最新的原则。

       2,使用合理的缓存策略,主要是二级缓存和查询缓存的选择使用,根据实际情况,合理使用。

  3,尽量使用延迟加载功能。

  4,推荐使用uuid的主键生成策略,移植性好,支持大数据量。

  5,推荐使用乐观锁代替悲观锁。

      总而言之,合理使用框架中的一些功能,考虑全面,来提高使用框架的性能。

        当然,这里只是简单的框架搭建,还有很多功能,没有加入,需要我们根据实际的情况,不断的完善配置文件,使其的功能越来越强大,使我们的项目开发完全融入到框架中开发,那样我们才会得心应手。总而言之,需要我们多用,多观察,多总结,多反思……

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值