`
winyee
  • 浏览: 54053 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Configuring a JBoss + Spring + JPA (Hibernate) + JTA web application

阅读更多
Configuring a JBoss + Spring + JPA (Hibernate) + JTA web application


http://www.swview.org/node/214

Here's how one might go about deploying a Spring application in JBoss (4.something) that uses JPA with Hibernate as the provider for persistence and JTA for transaction demarcation.

1. Define the Spring configuration file in the web.xml file
 
<context-param>
        <description>Spring configuration file</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

2. Define the Spring loader in the web.xml file
<listener>
        <description>Spring Loader</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3. Define the persistence unit reference in the web.xml file (which in fact has no effect until the Servlet container supports Servlet spec 2.5):
<persistence-unit-ref>
        <description>
            Persistence unit for the bank application.
        </description>
        <persistence-unit-ref-name>persistence/BankAppPU</persistence-unit-ref-name>
        <persistence-unit-name>BankAppPU</persistence-unit-name>        
</persistence-unit-ref>

* Note that this is what enables "<jee:jndi-lookup>" which has been commented out in the below given Spring configuration file.

* For the above to work well, your web.xml should start like this (note the version 2.5):
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

4. Here's the persistence.xml file. Make the changes to the <jta-data-source> as you have defined in your system (for example in a file like JBOSS_HOME/server/default/deploy/bank-ds.xml - See JBOSS_HOME/docs/examples/jca/ for templates).
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="BankAppPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:BankAppDS</jta-data-source>
    <properties>
      <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
      <property name="jboss.entity.manager.factory.jndi.name" value="java:/BankAppPU"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

5. Here's a sample Spring configuration file (applicationContext.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

    <!-- In a fully J5EE compatible environment, the following xml tag should work in accessing the EMF -->           
<!--
    <jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/BankAppPU"/>
-->
   
    <!-- Hack for JBoss 4.something until full compliance is reached -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
      <property name="persistenceUnitName" value="BankAppPU"/>
    </bean>

    <!-- Let's access the JTA transaction manager of the application server -->
    <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName" value="java:/TransactionManager"/>
        <property name="userTransactionName" value="UserTransaction"/>
    </bean>
    
    <!-- Let's define a DAO that uses the EMF -->
    <bean id="accountHolderDAO" class="bankapp.dao.AccountHolderDAO">
        <property name="emf" ref="entityManagerFactory"/>
    </bean>
    
    <!-- This is a service object that we want to make transactional.
         You will have an interface implemented (AccountManager) in the class.
    -->
    <bean id="accountManager" class="bankapp.AccountManagerImpl">
        <property name="accountHolderDAO" ref="accountHolderDAO"/>
    </bean>
    
    
    <!-- The transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
            <!-- all methods starting with 'get' are read-only transactions -->
            <tx:method name="get*" read-only="true"/>
            <!-- other methods use the default transaction settings (see below) -->
            <tx:method name="*" read-only="false" />
        </tx:attributes>
    </tx:advice>
    
    
    <!-- ensure that the above transactional advice runs for execution
      of any operation defined by the AccountManager interface -->
    <aop:config>
        <aop:pointcut id="accountManagerOperation",
           expression="execution(* bankapp.AccountManager.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="accountManagerOperation"/>
    </aop:config>

</beans>

6. Here's the sample AccountManagerImpl:
public class AccountManagerImpl implements AccountManager {
    
    /** Creates a new instance of AccountManagerImpl */
    public AccountManagerImpl() {
    }

    private AccountHolderDAO accountHolderDAO;
    
    public AccountHolder createAccountHolder(AccountHolder accountHolder) throws BankException {
        return accountHolderDAO.create(accountHolder);
    }

    public AccountHolderDAO getAccountHolderDAO() {
        return accountHolderDAO;
    }

    public void setAccountHolderDAO(AccountHolderDAO accountHolderDAO) {
        this.accountHolderDAO = accountHolderDAO;
    }  
}

7. Here's the sample AccountHolderDAO:
public class AccountHolderDAO {
    
    /** Creates a new instance of AccountHolderDAO */
    public AccountHolderDAO() {
    }
    
    private EntityManagerFactory emf;

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }
    
    public AccountHolder create(AccountHolder newAccountHolder) throws BankException {
        try {
            
            // JTA Transaction assumed to have been started by AccountManager (Spring tx advice)
            EntityManager em = emf.createEntityManager();
            //em.getTransaction().begin(); - Not required
            em.persist(newAccountHolder);
            //em.getTransaction().commit(); - Not required
            return newAccountHolder;
            // JTA Transaction will be completed by Spring tx advice
            
        } catch (Exception e) {
            throw new BankException("Account creation failed" + e.getMessage(), e);
        }
    }  
}

You will have some other code accessing the Spring bean "accountManager" and invoke the createAccountHolder() with the required parameters. Things should work well.

JTA: http://www.javaworld.com/javaworld/jw-04-2007/jw-04-xa.html
分享到:
评论

相关推荐

    Manning JBoss in Action: Configuring the JBoss Application Server

    He is a certified JBoss instructor and teaches courses in Hibernate and the JBoss Application Server. Javid is also an Agile evangelist and spends a large portion of his time transforming, coaching, ...

    JBoss in Action: Configuring the JBoss Application Server

    ### JBoss in Action: Configuring the JBoss Application Server #### 关键知识点概览 - **JBoss Application Server**: 一种开源的应用服务器,适用于Java应用程序的部署与管理。 - **JBoss in Action**: 一本...

    Pro+Spring+Security

    根据提供的文件信息,可以推断这是一本关于Spring Security的专业书籍,书名为《Pro+Spring+Security》,主要面向Java开发者。该书深入探讨了Spring Security框架,涵盖了从基础的安全概念到高级的安全策略实施。接...

    Configuring+SAP+ERP+Financials++-+Jones%2C+Peter

    Configuring SAP ERP Financials - Jones, Peter 这本书主要讲述了 SAP ERP 财务模块的配置和实现,书中涵盖了财务会计企业结构、总账、应付账款、应收账款、财务供应链管理、控制企业结构、成本元素会计、成本中心...

    Pro Spring MVC With Web Flow

    Table of Contents Configuring a Spring Development Environment Spring Framework Fundamentals Web Application Architecture Spring MVC Architecture Implementing Controllers Implementing Controllers ...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    8.1. Configuring Web Flow in Spring 8.1.1. Wiring a flow executor 8.1.2. Configuring a flow registry 8.1.3. Handling flow requests 8.2. The components of a flow 8.2.1. States 8.2.2. Transitions 8.2.3....

    Getting.started.with.Spring.Framework.2nd.Edition1491011912.epub

    Getting started with Spring Framework is a hands-on guide to begin developing applications using Spring Framework. This book is meant for Java developers with little or no knowledge of Spring ...

    spring-boot-reference.pdf

    11. Developing Your First Spring Boot Application 11.1. Creating the POM 11.2. Adding Classpath Dependencies 11.3. Writing the Code 11.3.1. The @RestController and @RequestMapping Annotations 11.3.2. ...

    JBoss in Action.pdf

    JBoss Application Server是一款开源的Java应用服务器,它支持多种企业级功能,如EJB(Enterprise JavaBeans)、JMS(Java Message Service)、JPA(Java Persistence API)以及Web服务等。本书第一章“VoteforJBoss...

    Spring Boot in Action(Manning,2015)

    Spring Boot radically streamlines spinning up a Spring application. You get automatic configuration and a model with established conventions for build-time and runtime dependencies. You also get a ...

    Hibernate_3.6.6_CHM 文档

    Hibernate JavaDoc (3.6.6.Final) Core API org.hibernate This package defines the...org.hibernate.stat This package exposes statistics about a running Hibernate instance to the application. 等等......

    西门子_Technical Instructions for Configuring a TCP Connection.pdf

    总结起来,西门子技术指导文件《Configuring a TCP Connection for S7-300/S7-400 Industrial Ethernet CPs》为自动化领域工程师和技术人员提供了一套详尽的TCP连接配置指导,不仅包括了基本的网络和通讯处理器设置...

    PHP 6 MySQL CakePHP Web Application Development

    Chapter 1: Configuring Your Installation ..........................................................3 Chapter 2: Creating PHP Pages Using PHP6 ..................................................19 ...

    IIs6整合jboss4.2 配置开发包

    这通常是为了利用IIS的Web服务器功能,同时利用JBOSS作为应用程序服务器的特性。 【描述】:“验证可用”意味着这个配置包经过测试,可以成功实现IIS6与JBOSS 4.2的整合,其中包括关键组件isapi_redirect-1.2.26....

    ArcGIS Server 9.2帮助文档翻译(2)Configuring Web Controls

    Web ADF(ArcGIS Web Application Developer Framework)提供了一种框架,用于开发与ArcGIS Server紧密集成的Web应用程序。在本段落中,我们将深入探讨如何配置Web Controls,以及相关的XML配置文件。 首先,我们...

    hibernate3.6API

    Core API org.hibernate This package defines the central Hibernate APIs....org.hibernate.stat This package exposes statistics about a running Hibernate instance to the application. ..........

Global site tag (gtag.js) - Google Analytics