多语言展示
当前在线:355今日阅读:23今日分享:25

Spring Hibernate配置教程

Spring整合Hibernate有什么好处?1、由IOC容器来管理Hibernate的SessionFactory2、让Hibernate使用上Spring的声明式事务
工具/原料
1

电脑

2

手机

方法/步骤
1

操作步骤如下: 1、bundle>=com.abc.def.lib.common:第三方jar包的统一管理,RCP中常用的jar包都在这边打包。当然,可以根据具体需要进行拆分;

2

2、bundle>=com.abc.def.properties:配置文件管理bundle,其可以动态读取Eclipse RCP的根目录下properties目录中的*.properties文件。新的业务Bundle也可以往此bundle中注册自己的properties文件。目录'%ECLIPSE_RCP_HOME%/properties/' 中的properties文件可供手动修改,如hibernate的数据库连接信息等;

3

3、bundle>=com.abc.def.lib.common:hibernate相关的jar包,依赖com.abc.def.lib.common 和 com.abc.def.properties两个bundle,MANIFEST.MF中增加: Eclipse-BuddyPolicy: dependent

4

4、bundle>=com.abc.def.lib.spring:Spring相关的jar包和相关的类,依赖com.abc.def.lib.common、 com.abc.def.properties、com.abc.def.lib.hibernate三个bundle,MANIFEST.MF中增加: Eclipse-BuddyPolicy: dependent

5

5、bundle>=com.abc.def.main:基础架构的入口bundle,这里进行RCP的初始化管理,依赖以上所有bundle。

6

6、RCP和SH的集成,很关键的一点就是上面红色字体标注部分,通过Eclipse的buddy机制实现bundle之间的双向访问。e5a48de588b6e799bee5baa6338

7

7、bundle>=com.abc.def.lib.spring中,修改覆盖了jar包中的org.springframework.util.ResourceUtils,避免Spring访问Annotation标注的Service类时找不到类的问题。

8

8、Spring和Hibernate实现了真正的零配置,所有Entity和Service都通过Spring和Hibernate的Annotation直接定义。目前在bundle>=com.abc.def.lib.spring中,在DataSourceConfig类中,用Annotation定义了dataSource、sessionFactory、txManager等bean,其中,sessionFactory中,通过PackagesToScan设置了model的统一根节点:com.abc.def,只要在这个package(包括子包)下的Annotation定义的model,都将自动纳入到SH的管理。

9

9、bundle>=com.abc.def.main中,通过ApplicationContext appContext = new AnnotationConfigApplicationContext('com.abc.def'),得到ApplicationContext。

注意事项

SessionFactory的创建交由IOC容器来管理。

推荐信息