Sping与hibernate和Struts的整合及使用
来源:互联网 发布:淘宝三国杀激活码 编辑:程序博客网 时间:2024/06/05 02:23
Sping与hibernate和Struts的整合及使用
1. Spring 整合 Hibernate 整合什么 ?
1). 有 IOC 容器来管理 Hibernate 的 SessionFactory
2). 让 Hibernate 使用上 Spring 的声明式事务
1). 有 IOC 容器来管理 Hibernate 的 SessionFactory
2). 让 Hibernate 使用上 Spring 的声明式事务
2. 整合步骤:
1). 加入 hibernate
①. jar 包
②. 添加 hibernate 的配置文件: hibernate.cfg.xml
③. 编写了持久化类对应的 .hbm.xml 文件。
2). 加入 Spring
①. jar 包
②. 加入 Spring 的配置文件
3). 整合.
3. 编写代码
1. Spring 如何在 WEB 应用中使用 ?
1). 需要额外加入的 jar 包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
2). Spring 的配置文件, 没有什么不同
1). 加入 hibernate
①. jar 包
②. 添加 hibernate 的配置文件: hibernate.cfg.xml
③. 编写了持久化类对应的 .hbm.xml 文件。
2). 加入 Spring
①. jar 包
②. 加入 Spring 的配置文件
3). 整合.
3. 编写代码
1. Spring 如何在 WEB 应用中使用 ?
1). 需要额外加入的 jar 包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
2). Spring 的配置文件, 没有什么不同
3). 如何创建 IOC 容器 ?
①. 非 WEB 应用在 main 方法中直接创建
②. 应该在 WEB 应用被服务器加载时就创建 IOC 容器:
在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器.
③. 在 WEB 应用的其他组件中如何来访问 IOC 容器呢 ?
在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器后, 可以把其放在ServletContext(即 application 域)的一个属性中.
①. 非 WEB 应用在 main 方法中直接创建
②. 应该在 WEB 应用被服务器加载时就创建 IOC 容器:
在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器.
③. 在 WEB 应用的其他组件中如何来访问 IOC 容器呢 ?
在 ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器后, 可以把其放在ServletContext(即 application 域)的一个属性中.
④. 实际上, Spring 配置文件的名字和位置应该也是可配置的! 将其配置到当前 WEB 应用的初始化参数中较为合适.
4). 在 WEB 环境下使用 Sprin
①. 需要额外加入的 jar 包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
②. Spring 的配置文件, 和非 WEB 环境没有什么不同
③. 需要在 web.xml 文件中加入如下配置:
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2. Spring 如何整合 Struts2 ?
1). 整合目标 ? 使 IOC 容器来管理 Struts2 的 Action!
2). 如何进行整合 ?
①. 正常加入 Struts2
②. 在 Spring 的 IOC 容器中配置 Struts2 的 Action
注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype
<bean id="personAction"
class="com.atguigu.spring.struts2.actions.PersonAction"
scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
③. 配置 Struts2 的配置文件: action 节点的 class 属性需要指向 IOC 容器中该 bean 的 id
<action name="person-save" class="personAction">
<result>/success.jsp</result>
</action>
④. 加入 struts2-spring-plugin-2.3.15.3.jar
3). 整合原理: 通过添加 struts2-spring-plugin-2.3.15.3.jar 以后, Struts2 会先从 IOC 容器中
获取 Action 的实例.
if (appContext.containsBean(beanName)) {
o = appContext.getBean(beanName);
} else {
Class beanClazz = getClassInstance(beanName);
o = buildBean(beanClazz, extraContext);
}
4). 在 WEB 环境下使用 Sprin
①. 需要额外加入的 jar 包:
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
②. Spring 的配置文件, 和非 WEB 环境没有什么不同
③. 需要在 web.xml 文件中加入如下配置:
<!-- 配置 Spring 配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2. Spring 如何整合 Struts2 ?
1). 整合目标 ? 使 IOC 容器来管理 Struts2 的 Action!
2). 如何进行整合 ?
①. 正常加入 Struts2
②. 在 Spring 的 IOC 容器中配置 Struts2 的 Action
注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype
<bean id="personAction"
class="com.atguigu.spring.struts2.actions.PersonAction"
scope="prototype">
<property name="personService" ref="personService"></property>
</bean>
③. 配置 Struts2 的配置文件: action 节点的 class 属性需要指向 IOC 容器中该 bean 的 id
<action name="person-save" class="personAction">
<result>/success.jsp</result>
</action>
④. 加入 struts2-spring-plugin-2.3.15.3.jar
3). 整合原理: 通过添加 struts2-spring-plugin-2.3.15.3.jar 以后, Struts2 会先从 IOC 容器中
获取 Action 的实例.
if (appContext.containsBean(beanName)) {
o = appContext.getBean(beanName);
} else {
Class beanClazz = getClassInstance(beanName);
o = buildBean(beanClazz, extraContext);
}
0 0
- Sping与hibernate和Struts的整合及使用
- Sping进阶之Spring和Hibernate的整合
- hibernate struts和sping 原理说明[转]
- sping +struts + hibernate 配置文件
- Struts与Hibernate整合
- Struts与Hibernate的整合策略
- Struts和Hibernate的整合策略
- struts、sping、ibatis整合记录
- 使用MYECLIPSE整合SPING+HIBERNATE经常遇到两个异常的解决办法
- 实例解析Struts 2.1与Sping、Hibernate集成
- 实例解析Struts 2.1与Sping、Hibernate集成
- 实例解析Struts 2.1与Sping、Hibernate集成
- 实例解析Struts 2.1与Sping、Hibernate集成
- sping+struts+hibernate+ajax配置
- Struts 2与Hibernate整合
- Spring + Sping MVC +Hibernate 整合
- 使用spring整合hibernate、struts
- struts+hibernate+spring的整合
- IOS内购流程(图片)
- Python爬虫抓取动态数据
- 同步、异步、阻塞与非阻塞
- 我的第一篇博客
- C++中explicit关键字的使用
- Sping与hibernate和Struts的整合及使用
- 词汇表
- 一文读懂机器学习,大数据/自然语言处理/算法全有了……
- 深入理解指针、数组
- Android进阶(二十四)Android UI---界面开发推荐颜色
- Vue.js——60分钟快速入门
- 静态成员函数
- block没那么难 block和变量的内存管理(二)
- 最长回文子串