如何在Seam中使用Spring Managed Hibernate Session?

时间:2013-01-13 13:28:27

标签: spring hibernate jpa-2.0 seam2

我最近的项目是在我尝试使用Spring(3.1.1.RELEASE)管理Hibernate会话到Seam(2.3.0.Final)的地方。

在JBoss Seam文档中,他们解释How to use Seam Managed Hibernate Session in Spring。但我们的要求与文件相反。

我得到了另一个解决方案Using Spring PlatformTransactionManagement,但我的要求是在Seam中使用Spring Managed Hibernate Session。我不想使用Seam管理Hibernate Session / Hibernate Entity Manager / Transaction。只想使用Seam管理依赖注入来通过seam @In而不是Spring @Autowire将Spring bean注入到Seam管理bean中。

如果有人遇到此类挑战,请帮助配置Seam components.xml&春天applicationContext.xml或为我提供指导方针来克服这个问题。

1 个答案:

答案 0 :(得分:0)

首先按照您配置的那样配置applicationContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <!-- hibernate Properties Here -->                     
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven/>

然后按以下方式配置components.xml

<core:init debug="true" transaction-management-enabled="true" />
<spring:context-loader config-locations="classpath*:/META-INF/spring/applicationContext.xml"/>
<core:manager concurrent-request-timeout="500" conversation-timeout="120000" conversation-id-parameter="cid" parent-conversation-id-parameter="pid" />
<persistence:managed-hibernate-session name="hibernateSession" auto-create="true" session-factory="#{sessionFactory}"/>
<spring:spring-transaction platform-transaction-manager="#{transactionManager}"/>

很容易将Spring(3.1.1.RELEASE)与Seam(2.3.0.Final)进行交互,但永远不要忘记为seam配置web.xml

<listener>
    <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
</listener>

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<servlet>
    <servlet-name>Seam Resource Servlet</servlet-name>
    <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Seam Resource Servlet</servlet-name>
    <url-pattern>/resource/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>Seam Filter</filter-name>
    <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Seam Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<login-config>
    <auth-method>BASIC</auth-method>
</login-config>

<session-config>
    <session-timeout>10</session-timeout>
</session-config>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

对于facelets,您应该在faces-config.xml

中添加变量解析器
<application>
    <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application>

最好下载Seam-2.3.0.Final。将其解压缩,在examples\spring\文件夹中,您将获得所需的配置