上一章节我们完成了一个简单的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 就是两种不同的方式。

可以参考官方文档妥妥的映射:

http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html

修改静态工厂方法:

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 的集成的更多相关文章

  1. 菜鸟学习Spring——60s学会Spring与Hibernate的集成

    一.概述. Spring与Hibernate的集成在企业应用中是很常用的做法通过Spring和Hibernate的结合能提高我们代码的灵活性和开发效率,下面我就一步一步的给大家讲述Spring如何和H ...

  2. spring和hibernate的集成

    集成关系图: 项目目录树: User.java package com.donghai.bean; public class User { private String id; private Str ...

  3. java中从Spring、Hibernate和Struts框架的action、service和dao三层结构异常处理体系设计

    Spring的事务实现采用基于AOP的拦截器来实现,如果没有在事务配置的时候注明回滚的checked exception,那么只有在发生了unchecked exception的时候,才会进行事务回滚 ...

  4. Spring与Hibernate集成中的Session问题

    主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方法获得session进行操作 public class Test extends Hibernat ...

  5. Spring+Struts+Hibernate 简介(转)

    http://blog.csdn.net/slnqnd/article/details/1772910/ Struts2.0 +Hibernate 3.2 +Spring 2.0 一.         ...

  6. Spring3.0+Hibernate+Atomikos集成构建JTA的分布式事务--解决多数据源跨库事务

    一.概念 分布式事务分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点之上.简言之,同时操作多个数据库保持事务的统一,达到跨库事务的效果. JTA ...

  7. Spring第12篇—— Spring对Hibernate的SessionFactory的集成功能

    由于Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心 ...

  8. 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)

    前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...

  9. Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目

    原文:Java web 项目搭建 Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring ...

随机推荐

  1. JDBCTemplate基础学习

    JDBCTemplate:spring提供的用于操作数据库的模板,类似DbUtils.使用时必须设置数据源(DataSource):数据源如DBCP.C3P0等 一.JDBCAPI简单使用Demo 1 ...

  2. hdu 1176 免费馅饼(动态规划)

    AC code: #include<stdio.h> #include<string.h> #define max(a,b) (a>b?a:b) #define maxo ...

  3. Linux下修改计算机名

    SuSe操作系统: 1.  修改/etc/HOSTNAME 文件  ,其内容为计算机名. 输入命令:vi /etc/HOSTNAME 使用键盘上的 x 键一个一个删除所有内容 ,然后使用键盘上的 i ...

  4. 手机wifi密码的保存位置

    subjects:  adj. 受制于...的, 被统治的; n. 主题,学科, 国民 the subjects had to kneel down before the king. kneel -& ...

  5. linux的free命令

    free 查看内存使用情况,默认以kb为单位 Mem: total=used+free, 其中buffers和cached是已经使用的内存, 对程序的buffers和cached的理解: os 在内存 ...

  6. Linux下远程cp命令scp

    2014-2.19  PS1.在用此命令cpLinux与Linux之间的数据时发现有些服务器上默认没有安装scp但用yum -y install scp提示么有这样的包 后来发现原来scp工具的安装包 ...

  7. Spring常用annotation标签

    @Service @Scope @Transactional @Autowired @Qualifier @PostConstruct @PreDestroy

  8. 繁华模拟赛 Vicent坐电梯

    /*n<=5000­这样就不能用O(n)的转移了,而是要用O(1)的转移.­注意我们每次的转移都来自一个连续的区间,而且我们是求和­区间求和?­前缀和!­令sum[step][i]表示f[ste ...

  9. 获取IOS 设备基本信息

    原地址:http://www.cnblogs.com/U-tansuo/p/ios_basis_info.html 1.获取设备类型  (Iphone/ipad 几?) #import "s ...

  10. haartraining生成.xml过程

    继续向大神学习http://www.cnblogs.com/tornadomeet/archive/2012/03/28/2420936.html