Struts+Spring+Hibernate (一) 集成原理和搭建环境
- 简介
- 前几天抢了几次小米,可惜呀,也没有抢到啊,经过考虑买了一个联想的手机多支持国货嘛,小米手机为什么这么抢手,供不应求,你是不是也想买小米手机呢?我们拿小米手机的生产过程来说一下SSH搭建的流程。
- 在小米手机生产中,我们假设是分为了n个环节组成,我们需要做的是让整个生产流程提高效率,从最低生产环节规律来看,只有让每个环节达到最佳实践才能保证整个过程高效,如果将Java
Web开发看作一个生产过程,怎么才能让这个过程提高效率,需要提高每个层开发的效率,而SSH三个框架就是在Web开发中多少先驱们用智慧和汗水走出来的三个最有效开发环节。。
- 同小米手机一样,利用SSH来开发同样也是为了提高生产效率,节约成本(工资+时间+其它),因此你若从事Java的Web开发很有必要了解SSH这三个框架。
- 对SSH的简单认识
- SSH是一些jar包组成的里面封装了特定的Java代码。一个框架即是为了解决某一领域问题的最佳方法,官方术语叫最佳实践,既然SSH是解决某一领域问题的好的方法,那么它也就不是一个完整的成品只是在一定程度上完成了开发流程,让我们开发简单,是我们开发中使用的一种工具。
- 目前市面上,类似于SSH的框架还有很多,如雨后春笋般层出不穷,如果每一种都掌握了也不可能,没有那么多精力和体力,从众多框架中可以看出,他们的实现思路都很相似只要了解了SSH的设计思路,其它的也就都触类旁通了。
- Spring只是作为一个bridge把Struts和Hibernate连接起来了,java
Web中也可以不引入Spring一样可以开发,不过呢加入了之后会使得他们结合的更紧密,层与层之间耦合性更低,为了贯彻高内聚低耦合的指导方针建议还是加上Spring为好。
- SSH集成原理
- 从下面这张图可以看出一些端倪
- 在Spring、Struts集成中,关键是把Struts的Action交给了Spring代理管理,在代理里面加入了注入把表示层需要用到的业务逻辑类都提前进行注入,而不需要我们自己用new的方式来定位和查找,将查找定位这一过程交给了Spring的IOC容器管理,使得业务对象很灵活可以在配置文件里面配置。
- Spring又是怎么和Hibernate集成呢,Spring的AOP容器可以管理Hibernate的SessionFactory创建,以及提供了管理Transaction、Connection等,它还解决了load延迟问题,弥补了Hibernate的不足。
- Myeclipse10中搭建步骤
- 第一步:
- 引入Struts、Spring、Hibernate各自的jar包,参见博客:引入有哪些包?
- 第二步:配置文件Struts-config.xml、ApplicationContext-common.xml、Web.xml
- Struts-config.xml这个文件中type属性用于指定Spring代理类,该类提供注入业务类
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<!--
type属性指定了Spring实现的Action代理类
-->
<action path="/login"
type="org.springframework.web.struts.DelegatingActionProxy"
name="loginForm"
scope="request"
>
<forward name="success" path="/item.do" redirect="true"/>
<forward name="ipError" path="/ipError.jsp" redirect="true"></forward>
</action>
</action-mappings>
<message-resources parameter="MessageResources" />
</struts-config>
- ApplicationContext-common.xml该文件是Spring与Hibernate集成的配置。
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 哪些类哪些方法使用事务-->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.bjpowernode.drp.service.*.*(..))"/>
<aop:advisor advice-ref="txAdive" pointcut-ref="allManagerMethod"/>
</aop:config>
<tx:advice id="txAdive" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
- Web.xml,Spring提供了一些AOP服务,例如设置字符编码、声明事务、OpenSessionInView等
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 创建BeanFactory -->
<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提供的过滤器,编码问题-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB18030</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Spring提供的过滤器,解决了Hibernate的load的lazy问题 -->
<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>
</web-app>
- 经过上面的配置SSH框架就算搭建完成了,我们可以看到主要是用了各种配置文件,在Java里面配置文件是很多的,为了让程序更灵活,引入了大量的配置文件。
- 这篇博客只是SSH种基础内容,后面还需要大量的实践来深入学习,有不对的希望与大家共同学习和探讨,下篇博客会结合一个例子,来演示一下SSH怎么来开发项目。
原文地址:https://www.cnblogs.com/lilongsheng1125/p/4978564.html