`
抛出异常的爱
  • 浏览: 621629 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring、hibernate、tapestry整合篇配置过程

阅读更多

spring、hibernate、tapestry整合篇
www.aspsky.net2005-9-5动网先锋


目前java开源框架真可以说是琳琅满目,最近一个多星期一直在接触spring、hibernate、tapestry。将最近一个多星期以来的学习汇总一下,以便日后查阅,也方便大家学习。

简单的介绍到处都是,对于spring、hibernate、tapestry是干什么用的,我就不多介绍了。大致能够知道spring的IOC/DI概念(AOP方面我也理解不好,暂时)、hibernate的基本概念,如对象持久,ORM,POJO这些概念,tapestry重在组件。我想懂这些并且做个简单小例子,理解本文应该就没什么大问题了。

我的开发环境是eclipse3.1+tomcat5.0.18+jdk1.4.2/jdk1.5 +mysql+这些相关jar包

注:spring1.2+hibernate2.1+tapestry3.0.3

对于eclipse的操作这里不做详细介绍,下面会给出整合代码。

建立项目如together,引入需要的包,方便起见就把spring.jar引入,因为它比较全,它没有包含mock(主要是测试用的)。总之是把这些都导进到你的project里来。别import时候找不到就可以了。

Spring和hibernate结合部分:

数据库准备工作:如mysql,建库为learn,建表为customer,包含字段(aid,username,password)分别为int,varchar型。

相关代码(代码存放位置):

spring-hibernate.xml(与src保持同级)

test spring and hibernate

class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

com.mysql.jdbc.Driver

jdbc:mysql://localhost/learn

root

class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

yunguang/learn/springandhibernate/Customer.hbm.xml

net.sf.hibernate.dialect.MySQLDialect

true

customer.hbm.xml(yunguang.learn.springandhibernate包下)

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">


/*

* Created on 2005-7-6

*

* TODO To change the template for this generated file go to

* Window - Preferences - Java - Code Style - Code Templates

*/

package yunguang.learn.springandhibernate;

 

/**

* @author Administrator

*

* TODO To change the template for this generated type comment go to Window -

* Preferences - Java - Code Style - Code Templates

*/

public class Customer {

private int id;


private String username;


private String password;

public int getId() {

return id;

}

public String getPassword() {

return password;

}

public String getUsername() {

return username;

}

public void setId(int id) {

this.id = id;

}

public void setPassword(String password) {

this.password = password;

}

public void setUsername(String username) {

this.username = username;

}

}


单元测试类

package test.junit;

import junit.framework.TestCase;

import junit.textui.TestRunner;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import yunguang.learn.springandhibernate.Customer;

import yunguang.spring.dao.ITestDao;

public class TestDaoTest extends TestCase {

public void testInsertTest(){

ApplicationContext beans = new FileSystemXmlApplicationContext("spring-hibernate.xml");

// XmlBeanFactory beans = new XmlBeanFactory(new FileInputStream(

// "spring-hibernate.xml"));

ITestDao tests = (ITestDao) beans.getBean("hibernatedao");

Customer customer = new Customer();

customer.setId(55);

customer.setUsername("yunguangtest==================");

customer.setPassword("passwordsdlkjfklsd");

tests.insertTest(customer);

}

public static void main(String[] args) {

TestRunner.run(TestDaoTest.class);

}

}

以eclipse中的junite运行这一单元测试。即可一路跑绿,查看数据库完成插入操作。

小结:

核心为spring-hibernate.xml,利用spring的依赖注入的特性。不用单独配置hibernate的配置文件(hibernate.cfg.xml和hibernate.properties)。其他部分见代码吧!如果单独都能各自都能理解,则看上面代码不会有什么太大障碍。

spring和tapestry结合部分:

相关代码(代码存放位置):

前提当然是建立web project,才能使tapestry发挥其作用,才能完整此例子的练习。这里我只是在tomcat下建立deploy并且简单测试了一下:

Web.xml:(这个文件我想大家都知道放在哪里吧。呵呵)

PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">


together


contextConfigLocation

/WEB-INF/springandtapestry.xml

context

org.springframework.web.context.ContextLoaderServlet

together


org.apache.tapestry.ApplicationServlet


together

/app


Home.html

Springandtapestry.xml(这个和web.xml 放在同个目录下,即web-inf目录下)


"http://www.springframework.org/dtd/spring-beans.dtd">


Together. application(与project同名的xml文件。这是tapestry的要求)


"-//Apache Software Foundation//Tapestry Specification 3.0//EN"

"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">


add a description

Home.html(在你的tapestry能够访问到的默认目录)


Home.page(与home.html同级)

"-//Apache Software Foundation//Tapestry Specification 3.0//EN"

"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">

global.appContext.getBean("aBean")


相关类文件:

package yunguang.learn.springandtapestry;


public interface IBean {

public String getAmethod();

}


package yunguang.learn.springandtapestry;

public class Bean implements IBean {

public String getAmethod() {

// do something;

System.out

.println("================================================test a amethod=============================================");

return "===================test===========================";

}

}

package yunguang.learn.springandtapestry;

public class Global {

}


package yunguang.learn.springandtapestry;

import java.util.Map;

import org.apache.tapestry.engine.BaseEngine;

import org.apache.tapestry.request.RequestContext;

import org.springframework.context.ApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

public class MyEngine extends BaseEngine {

public static final String APPLICATION_CONTEXT_KEY = "appContext";

protected void setupForRequest(RequestContext context) {

super.setupForRequest(context);

Map global = (Map) getGlobal();

ApplicationContext ac = (ApplicationContext) global

.get(APPLICATION_CONTEXT_KEY);

if (ac == null) {

ac = WebApplicationContextUtils.getWebApplicationContext(context

.getServlet().getServletContext());

System.out.println("测试" + ac); global.put(APPLICATION_CONTEXT_KEY, ac);


}

}

}


package yunguang.learn.springandtapestry;

import org.apache.tapestry.event.PageEvent;

import org.apache.tapestry.event.PageRenderListener;

import org.apache.tapestry.html.BasePage;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class Home extends BasePage implements PageRenderListener {

public abstract IBean getABean();


/** 当页面表现之前,首先运行这个方法 */

public void pageBeginRender(PageEvent event) {

WebApplicationContext appContext = WebApplicationContextUtils

.getWebApplicationContext(getRequestCycle().getRequestContext()

.getServlet().getServletContext());

IBean bean = (IBean) appContext.getBean("aBean");

//bean.getAmethod();

}

}

 

小结:

核心在于利用engine部分,通过定义map的global。然后在home.page中可以进行通过global.appContext.getBean("aBean")对home.java中abstract属性进行赋值。

另外:


contextConfigLocation

/WEB-INF/springandtapestry.xml


这部分是个小小重点!如果把此xml文件改成spring默认的xml文件,则不需要此配置参数过程。

一个小疑惑是:在home.html中"ognl:aBean.Amethod"中的amethod中的a大小写都可以。我觉得应该小写a是正确的,但是错误的写成大写A了居然也可以正常显示。还是看看tapestry源代码吧。以后再写了!


希望本文对你有所帮助。至于其中原理,我想可以通过breakpoint方式一步一步跟下去就明白了。

当然完成本文例子需要有基本的spring、hibernate、tapestry知识。而高手们就不要见笑了。就当看看笑话吧!

其实距离真正结合还差一步,因为并没有通过tapestry作为view,而spring作为主体框架,实现hibernate的对象持久。逻辑清了,下面也就不难了。GOOD LUCK!

分享到:
评论

相关推荐

    tapestry5.3.3+spring+hibernate整合源码

    tapestry5.3.3+spring+hibernate整合源码,也可以到官方网站下载:http://flywind.org/newtechnologydetail/135

    tapestry4+spring+hibernate整合实例

    一个简单的T4SH整合实例,实现用户数据的增删改查询

    基于Tapestry+Spring+Hibernate框架的Web应用

    在维普上下载下来的。介绍3个框架的整合!!!!!!

    Struts2+Hibernate+Spring框架电子商城

    本系统采用的关键技术是Struts2+Hibernate+Spring整合和AJAX。之所以采用SSH整合是因为在软件工程领域,为了降低模块耦合度,提高模块的可重用性,分层一直是广为采纳的一个方法。其实分层还可以使开发人员专注于某...

    spring in action英文版

     4.4 用Spring整合Hibernate  4.4.1 Hibernate概览  4.4.2 管理Hibernate资源  4.4.3 用HibernateTemplate访问Hibernate  4.4.4 HibernateDaoSupport的子类  4.5 Spring和JDO  4.5.1 配置JDO ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 ...

    Spring 2.0 开发参考手册

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    Spring中文帮助文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    Spring API

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...

    spring chm文档

    6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点...

    SSH相关整合的总要资料

    SSH是指Struts,Spring,Hibernate的简称。  Struts 是一个为开发基于模型(Model)-视图(View)-控制器(Controller)(MVC)模式的应用架构的开源框架,是利用Java Servlet和JSP构建Web应用的一项非常有用的技术。其...

    java编程.数据库.j2ee架构与模式等电子书整合(2)

    16.Tapestry 17.Junit 18.Ejb 19.Spring 20.Unix 21.Vss 22.Jbuilder 23.ireport + jasperreport 24.网络编程 25.Rational Rose 26.applet 和 java 安全 27.javascript代码 28.字符编码及java中文字符 29.java声音...

    java编程.数据库.j2ee架构与模式等电子书整合(1)

    16.Tapestry 17.Junit 18.Ejb 19.Spring 20.Unix 21.Vss 22.Jbuilder 23.ireport + jasperreport 24.网络编程 25.Rational Rose 26.applet 和 java 安全 27.javascript代码 28.字符编码及java中文字符 29.java声音...

Global site tag (gtag.js) - Google Analytics