---
title: "JSF1.2 +Spring2.0 +Hibernate3.2  "
author: "iamlibo"
date: "2007-05-24T15:46:00Z"
tags: []
slug: "jsf1-2-spring2-0-hibernate3-2"
draft: false
lastmod: "2007-05-24T15:46:00Z"
id: 274414
url: "https://iamlibo.iteye.com/blog/274414"
---

最近在准备一个项目，经过比较最终选JSF1.2 +Spring2.0 +Hibernate3.2的框架，至于为什么我就不说了，每个都有自己的看法。下面进入正题。

    开发工具是MyEclipse5.5 支持JEE5的开发，其实用到JEE5的功能可能很少或者没有，但即然有还是选择先进的技术吧。JSF1.2暂时选择SUN的实现，以后要可以随时换成MyFaces的（现在MyFaces是1.1.5吧？），Spring是2.0.5版。使用Maven2来管理Jar包和生命周期，用着也是很方便的，比ANT要少写不少配置的东西。而且有专门的E clipse下的插件。

    开始操作吧。

1. # 新建工程

在MyEclipse下新建一个Web工程。注意J2EE服务器选择J2EE5.0 ,然后点完成。以前的MyEclise新建Web工程很慢，有时会死机，现在好多了。已经好长时间没用MyEclipse了。

1. # 加入JSF

在工程目录上点右键，在菜单中选择MyEclipse-&gt;Add JSF Capabilities…会出现一个窗口，如图

Faces-config.xml 文件放到/WEB-INF/目录下，Faces Servlet name 保持不变是 Faces Servlet即可，我将URL pattern 改成了\*.jsf ，不改也可以只是要注意后面涉及到访问JSF的都要用\*.faces。

现在JSF就配置好了，主要是web.xml中的一个Servlet和faces-config.xml。

1. # 加入Spring

用MyEclipse也能够象加入JSF一样加入Spring 的支持，但是我们要用MAVEN2来管理JAR包，所以就不用这种方式加入。在工程点上右键，在弹出的菜单中选择Maven2然后选择endable。（要求你得先正确安装[Maven2](http://maven.apache.org/)和[Eclipse的插件](http://m2eclipse.codehaus.org/)， 插件的更新地址是：[http://m2eclipse.codehaus.org/update/](http://m2eclipse.codehaus.org/update/)，多数是不成功，得找一个正确的Eclipse版本，我也是换了好多个版本）。

在弹出的窗口中输入要求的信息，主要是packaging 选择war。

完成后工程根目录就会新建一个pom.xml 文件。在文件上右键，maven2-&gt;add dependency 来加入需要的jar包。看下面完整的[pom.xml](#_%E5%AE%8C%E6%95%B4pom.xml)(注意：编辑完pom.xml之后编译工程就要联网下载jar包了。)

 

在WEB-INF/目录下一个名为applicationContext.xml的文件，因为了Spring启动时会自己在这个目录下查找这个文件。内容如下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns=[](http://www.springframework.org/schema/beans)[http://www.springframework.org/schema/beans xmlns:xsi=](http://www.springframework.org/schema/beans)[](http://www.w3.org/2001/XMLSchema-instance)[http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;](http://www.w3.org/2001/XMLSchema-instance)

[](http://www.w3.org/2001/XMLSchema-instance)            

[&lt;/beans&gt;](http://www.w3.org/2001/XMLSchema-instance)

[只是一个没有任何配置的文件。](http://www.w3.org/2001/XMLSchema-instance)

1. # [JSF结合Spring](http://www.w3.org/2001/XMLSchema-instance)
   
   [Spring 加入后，得让它能够和JSF结合起来工作，也就是在JSF中能够访问Spring中的bean，只需要在web.xml文件中加入下面一段：](http://www.w3.org/2001/XMLSchema-instance)

    [&lt;listener&gt;](http://www.w3.org/2001/XMLSchema-instance)

        [&lt;listener-class&gt;](http://www.w3.org/2001/XMLSchema-instance)

            [org.springframework.web.context.ContextLoaderListener](http://www.w3.org/2001/XMLSchema-instance)

        [&lt;/listener-class&gt;](http://www.w3.org/2001/XMLSchema-instance)

    [&lt;/listener&gt;](http://www.w3.org/2001/XMLSchema-instance)

[这样就可以访问Spring 中的bean 了。](http://www.w3.org/2001/XMLSchema-instance)

    [例如：](http://www.w3.org/2001/XMLSchema-instance)

        [Faces-confg.xml中的一段代码](http://www.w3.org/2001/XMLSchema-instance)

    [&lt;managed-bean&gt;](http://www.w3.org/2001/XMLSchema-instance)

        [&lt;managed-bean-name&gt;UserBean&lt;/managed-bean-name&gt;](http://www.w3.org/2001/XMLSchema-instance)

        [&lt;managed-bean-class&gt;](http://www.w3.org/2001/XMLSchema-instance)

            [cn.xiangyunsoft.web.jsf.beans.UserBean](http://www.w3.org/2001/XMLSchema-instance)

        [&lt;/managed-bean-class&gt;](http://www.w3.org/2001/XMLSchema-instance)

        [&lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;](http://www.w3.org/2001/XMLSchema-instance)

        [&lt;managed-property&gt;](http://www.w3.org/2001/XMLSchema-instance)

            [&lt;property-name&gt;userDao&lt;/property-name&gt;](http://www.w3.org/2001/XMLSchema-instance)

            [&lt;value&gt;#{userDao}&lt;/value&gt;](http://www.w3.org/2001/XMLSchema-instance)

        [&lt;/managed-property&gt;](http://www.w3.org/2001/XMLSchema-instance)

    [&lt;/managed-bean&gt;](http://www.w3.org/2001/XMLSchema-instance)

    [其中#{userDao} 这个就是访问Spring中的bean .](http://www.w3.org/2001/XMLSchema-instance)

    [applicationContext.xml](http://www.w3.org/2001/XMLSchema-instance)

    [&lt;bean id="userDao"](http://www.w3.org/2001/XMLSchema-instance)

        [class="cn.xiangyunsoft.busniess.dao.impl.UserDaoImpl"&gt;](http://www.w3.org/2001/XMLSchema-instance)

        [……](http://www.w3.org/2001/XMLSchema-instance)

    [&lt;/bean&gt;](http://www.w3.org/2001/XMLSchema-instance)

[](http://www.w3.org/2001/XMLSchema-instance)    

1. # [加入Hibernate3](http://www.w3.org/2001/XMLSchema-instance)
   
   [在pom.xml文件中加入依赖包。看后面完整的](http://www.w3.org/2001/XMLSchema-instance)[pom.xml](#_%E5%AE%8C%E6%95%B4pom.xml)

在applicationContext.xml文件加入下面代码

    &lt;bean id="XYSDataSource"

        class="org.apache.commons.dbcp.BasicDataSource"&gt;

        &lt;property name="driverClassName"

            value="net.sourceforge.jtds.jdbc.Driver"&gt;

        &lt;/property&gt;

        &lt;property name="url"

            value="jdbc:jtds:sqlserver://localhost:1433/erpdata"&gt;

        &lt;/property&gt;

        &lt;property name="username" value="sa"&gt;&lt;/property&gt;

        &lt;property name="password" value=""&gt;&lt;/property&gt;

    &lt;/bean&gt;

    &lt;bean id="HibernateSessionFactory"

        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;

        &lt;property name="dataSource"&gt;

            &lt;ref bean="XYSDataSource" /&gt;

        &lt;/property&gt;

        &lt;property name="hibernateProperties"&gt;

            &lt;props&gt;

                &lt;prop key="hibernate.dialect"&gt;

                    org.hibernate.dialect.SQLServerDialect

                &lt;/prop&gt;

            &lt;/props&gt;

        &lt;/property&gt;

    &lt;/bean&gt;

第一个bean XYSDataSource是设置数据源，也可以把它换成JNDI方式由服务器管理的数据源。

第二个bean HibernateSessionFactory是设置HibernateSession工厂，可以通过Spring bean 来访问Hibernate session.因为使用annotations方式配置hibernate，所以连hibernate.cfg.xml 都不用了！

1. # 完整pom.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;project&gt;

&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

&lt;groupId&gt;cn.xiangyunsoft&lt;/groupId&gt;

&lt;artifactId&gt;XI&lt;/artifactId&gt;

&lt;packaging&gt;war&lt;/packaging&gt;

&lt;version&gt;0.0.1&lt;/version&gt;

&lt;description&gt;&lt;/description&gt;

&lt;dependencies&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring-core&lt;/artifactId&gt;

&lt;version&gt;2.0.5&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring-support&lt;/artifactId&gt;

&lt;version&gt;2.0.5&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring-hibernate3&lt;/artifactId&gt;

&lt;version&gt;2.0.5&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring-aop&lt;/artifactId&gt;

&lt;version&gt;2.0.5&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring-jpa&lt;/artifactId&gt;

&lt;version&gt;2.0.5&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring-jms&lt;/artifactId&gt;

&lt;version&gt;2.0.5&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring-jca&lt;/artifactId&gt;

&lt;version&gt;2.0.5&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring&lt;/artifactId&gt;

&lt;version&gt;2.0.5&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.hibernate&lt;/groupId&gt;

&lt;artifactId&gt;hibernate-annotations&lt;/artifactId&gt;

&lt;version&gt;3.2.1.ga&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;commons-dbcp&lt;/groupId&gt;

&lt;artifactId&gt;commons-dbcp&lt;/artifactId&gt;

&lt;version&gt;1.2.2&lt;/version&gt;

&lt;/dependency&gt;

&lt;/dependencies&gt;

&lt;/project&gt;

好了。所有的配置都完成了。现在他们可以好好的相处了。就差加点代码让它们工作了。过两天会把完整的代码发上来。

还有，JSF+Spring+hibernate最少需要那些Spring的包还没试。谁知道可以知会一下。现在差不多是都加上了。