Java Hour 56 Spring 和 Hibernate 的集成
上一章节我们完成了一个简单的Spring 的试验品,这章要让Spring 上战场了,不要慌,步骤都是一样的。
Spring 对 Hibernate 的支持是很多方面的,第一个战场是SessionFactory.
集成前: 静态工厂方法
private static SessionFactory sessionFactory; static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { // Alternatively, we could look up in JNDI here return sessionFactory; }
干掉hibernate.cfg.xml
hibernate 映射有两种方式,对应的bean 就是两种不同的方式。
可以参考官方文档妥妥的映射:
修改静态工厂方法:
public static SessionFactory getSessionFactory() { ApplicationContext ctx = new FileSystemXmlApplicationContext("beans.xml"); SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory"); return sessionFactory; }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="weather" class="mike.weather.core.WeatherBusiness"></bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost/hibernate</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="packagesToScan"> <list> <value>mike.weather.model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect </prop> </props> </property> </bean> </beans>
静态工厂方法返回一个ApplicationContext
这里涉及到这个ApplicationContext 的位置问题,一般需要一个静态的反复直接返回这个ApplicationContext 的实例即可。
public static void main(final String[] args) { ApplicationContext context = ApplicationContextUtils.getApplicationContext(); WeatherBusiness weatherBusiness = context.getBean(WeatherBusiness.class); weatherBusiness.getWeather(); }
public static ApplicationContext getApplicationContext() { return ctx; }
Note 莫名其妙就成功了
程序居然真run 起来了,不可思议。顺利的让楼主我都莫名其妙。
Java Hour 56 Spring 和 Hibernate 的集成的更多相关文章
- 菜鸟学习Spring——60s学会Spring与Hibernate的集成
一.概述. Spring与Hibernate的集成在企业应用中是很常用的做法通过Spring和Hibernate的结合能提高我们代码的灵活性和开发效率,下面我就一步一步的给大家讲述Spring如何和H ...
- spring和hibernate的集成
集成关系图: 项目目录树: User.java package com.donghai.bean; public class User { private String id; private Str ...
- java中从Spring、Hibernate和Struts框架的action、service和dao三层结构异常处理体系设计
Spring的事务实现采用基于AOP的拦截器来实现,如果没有在事务配置的时候注明回滚的checked exception,那么只有在发生了unchecked exception的时候,才会进行事务回滚 ...
- Spring与Hibernate集成中的Session问题
主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方法获得session进行操作 public class Test extends Hibernat ...
- Spring+Struts+Hibernate 简介(转)
http://blog.csdn.net/slnqnd/article/details/1772910/ Struts2.0 +Hibernate 3.2 +Spring 2.0 一. ...
- Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务
一.概念 分布式事务分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.简言之,同时操作多个数据库保持事务的统一,达到跨库事务的效果. JTA ...
- Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能
由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...
- 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)
前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...
- Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目
原文:Java web 项目搭建 Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring ...
随机推荐
- WebSocket 是什么原理?为什么可以实现持久连接?
https://www.zhihu.com/question/20215561 作者:Ovear链接:https://www.zhihu.com/question/20215561/answer/ ...
- C语言时间函数
#include "time.h" #include "stdio.h" #include "stdlib.h" int main() { ...
- WPF 任务栏图标闪烁提醒
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...
- 【原创】angularjs1.3.0源码解析之directive
# Angular指令编译原理 前言 angular之所以使用起来很方便,是因为通常我们只需要在html里面引入一个或多个(自定义或内置的)指令就可以完成一个特定的功能(这也是angular推荐的方式 ...
- 在Sublime Text3 开发Node.js遇到的一个小问题
原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 以前的Sublime Text 2包管理出现问题了,不能安装新包,让人开发很捉急,今天装了个3,这个问题解决了 那我们就 ...
- sql重复记录查询
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId fro ...
- POJ 3041 Asteroids
最小点覆盖数==最大匹配数 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12678 Accepted: ...
- 用Prime31实现Google Play In-App-Blling
Android开发者想在海外赚钱,接入Google Play是不二选择,然而一堆英文文档对于像我这样的英语四级都木有过的可谓是苦恼之极.近段时间因工作需要研究了Unity接入Google Play的整 ...
- CameraFacingBillboard
原地址:http://www.cnblogs.com/88999660/ 描述 这个脚本使得它被连接到配合本身相机的对象.这对于要经常面对镜头,并以同样的方式了,因为它是有用的广告牌. 用法 将这个脚 ...
- 用Lucene检索数据库
http://blog.sina.com.cn/s/blog_82ac67c101012r9z.html package com.javabean; import java.io.File;impor ...