【Spring】Spring Framework Reference Documentation中文版34

Part VIII. Appendices

附录

 

37. Migrating to Spring Framework 4.x

集成spring框架4.x

 

Migration guides for upgrading from previous releases of the Spring Framework are now provided as a Wiki page.

集成指南已经被更新并且提供在Wiki页面上。

 

38. Spring Annotation Programming Model

spirng的注解编程模型

 

Springs annotation programming model is documented in the Spring Framework Wiki.

spring的注解编程模型已经文档话在spring框架的WiKi上。

 

39. Classic Spring Usage

经典的spring使用方法

 

This appendix discusses some classic Spring usage patterns as a reference for developers maintaining legacy Spring applications. These usage patterns no longer reflect the recommended way of using these features, and the current recommended usage is covered in the respective sections of the reference manual.

附加的讨论一些经典的spring使用模式作为一个开发者的参考对于spring的应用。这些使用方式不会影响使用这个特性的推荐方式并且当前的推荐使用可能在相关的引用手册中被覆盖。

 

39.1 Classic ORM usage

经典的ORM使用方式

 

This section documents the classic usage patterns that you might encounter in a legacy Spring application. For the currently recommended usage patterns, please refer to the ORM chapter.

这个章节讲述了经典的使用方式对于你可以使用spring的应用。对于当前建议的使用方式,建议参考OMR的章节。

 

39.1.1 Hibernate

 

For the currently recommended usage patterns for Hibernate see Section 20.3, Hibernate.

对于当前有关Hibernate的使用方式建议参考章节20.3Hibernate

 

The HibernateTemplate

 

The basic programming model for templating looks as follows, for methods that can be part of any custom data access object or business service. There are no restrictions on the implementation of the surrounding object at all, it just needs to provide a Hibernate SessionFactory. It can get the latter from anywhere, but preferably as bean reference from a Spring IoC container - via a simple setSessionFactory(..) bean property setter. The following snippets show a DAO definition in a Spring container, referencing the above defined SessionFactory, and an example for a DAO method implementation.

基本的编程模式对于模板话可以找到如下,对于方法可以是自定义的一部分对于任何自定义的访问object和业务服务。没有限制对于围绕object的实现,可以需要提供一个HibernateSessionFactory。他可以被获取并且最好作为一个bean的引用来自springIOC容器————通过一个简单的setSessionFactorybean属性设置。下面的片段展示了一个DAO的定义在spring的容器中,参考上面的定义的SessionFactory和一个DAO方法实现的例子。

 

 

    

        

    

 

 

public class ProductDaoImpl implements ProductDao {

 

    private HibernateTemplate hibernateTemplate;

 

    public void setSessionFactory(SessionFactory sessionFactory) {

        this.hibernateTemplate = new HibernateTemplate(sessionFactory);

    }

 

    public Collection loadProductsByCategory(String category) throws DataAccessException {

        return this.hibernateTemplate.find("from test.Product product where product.category=?", category);

    }

}

 

The HibernateTemplate class provides many methods that mirror the methods exposed on the Hibernate Session interface, in addition to a number of convenience methods such as the one shown above. If you need access to the Session to invoke methods that are not exposed on the HibernateTemplate, you can always drop down to a callback-based approach like so.

HibernateTemplate类提供了许多方法镜像暴露在HibernateSession接口上,此外一些方便的方法例如上面展示的。如果你需要访问Session来调用方法没有暴露在HibernateTemplate上,你可以使用这样的方式实现回调。

 

public class ProductDaoImpl implements ProductDao {

 

    private HibernateTemplate hibernateTemplate;

 

    public void setSessionFactory(SessionFactory sessionFactory) {

        this.hibernateTemplate = new HibernateTemplate(sessionFactory);

    }

 

    public Collection loadProductsByCategory(final String category) throws DataAccessException {

        return this.hibernateTemplate.execute(new HibernateCallback() {

            public Object doInHibernate(Session session) {

                Criteria criteria = session.createCriteria(Product.class);

                criteria.add(Expression.eq("category", category));

                criteria.setMaxResults(6);

                return criteria.list();

            }

        };

    }

 

}

 

A callback implementation effectively can be used for any Hibernate data access. HibernateTemplate will ensure that Session instances are properly opened and closed, and automatically participate in transactions. The template instances are thread-safe and reusable, they can thus be kept as instance variables of the surrounding class. For simple single step actions like a single find, load, saveOrUpdate, or delete call, HibernateTemplate offers alternative convenience methods that can replace such one line callback implementations. Furthermore, Spring provides a convenient HibernateDaoSupport base class that provides a setSessionFactory(..) method for receiving a SessionFactory, and getSessionFactory() and getHibernateTemplate() for use by subclasses. In combination, this allows for very simple DAO implementations for typical requirements:

一个回调实现可以使用对于任何Hibernate数据访问。HibernateTemplate将保证Session实例的打开和关闭并且自动的参与事务。模板实例是线程安全的并且是可以重用的,可以作为一个实例变量对于给定的类。对于一个简单的步骤类似于简单的findloadsaveOrUpdate或删除调用,HibernateTemplate提供了方便的方法替代可以替换这样一行回调实现。此外,spring提供了方便的HibernateDaoSupportbean类提供了一个setSessionFactory方法用于接收一个SessionFactorygetSessionFactorygetHibernateTemplate方法用于子类的使用。在组合中,他们允许简单的DAO实现对于典型的要求。

 

public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {

 

    public Collection loadProductsByCategory(String category) throws DataAccessException {

        return this.getHibernateTemplate().find(

            "from test.Product product where product.category=?", category);

    }

 

}

 

Implementing Spring-based DAOs without callbacks

实现基于springDAO而不需要回调

 

As alternative to using Springs HibernateTemplate to implement DAOs, data access code can also be written in a more traditional fashion, without wrapping the Hibernate access code in a callback, while still respecting and participating in Springs generic DataAccessException hierarchy. The HibernateDaoSupport base class offers methods to access the current transactional Session and to convert exceptions in such a scenario; similar methods are also available as static helpers on the SessionFactoryUtils class. Note that such code will usually pass false as the value of the getSession(..) methods allowCreate argument, to enforce running within a transaction (which avoids the need to close the returned Session, as its lifecycle is managed by the transaction).

可以使用springHibernateTemplate作为代替实现的DAO,数据访问代码可以被写入以一种传统的方式,不需回调Hibernate的访问代码,依然代表和参与spring的通用DataAccessException结构。HibernateDaoSupport基类提供了方法来访问当前的事务会话和转换异常在一些场景中;类似的方法也可以以作为静态的帮助类在SessionFactoryUtils类中。注意这样的代码通常传递false作为getSession方法的allowCreate参数,为了强制在事务中执行(避免需要关闭返回的session作为他的生命周期通过事务)。

 

public class HibernateProductDao extends HibernateDaoSupport implements ProductDao {

 

    public Collection loadProductsByCategory(String category) throws DataAccessException, MyException {

        Session session = getSession(false);

        try {

            Query query = session.createQuery("from test.Product product where product.category=?");

            query.setString(0, category);

            List result = query.list();

            if (result == null) {

                throw new MyException("No search results.");

            }

            return result;

        }

        catch (HibernateException ex) {

            throw convertHibernateAccessException(ex);

        }

    }

}

 

The advantage of such direct Hibernate access code is that it allows any checked application exception to be thrown within the data access code; contrast this to the HibernateTemplate class which is restricted to throwing only unchecked exceptions within the callback. Note that you can often defer the corresponding checks and the throwing of application exceptions to after the callback, which still allows working with HibernateTemplate. In general, the HibernateTemplate class' convenience methods are simpler and more convenient for many scenarios.

这样直接的Hibernate访问代码允许任何的检查应用异常被抛出在数据访问代码中,相对于HibernateTemplate类仅限于抛出非检查异常在回调代码中。注意你可以决定相应的检查和抛出应用异常在回调之后,依然允许使用HibernateTemplate。在通常情况下,HibernateTemplate类的方法方法是简单的并且适用于很多的场景的。

 

39.2 JMS Usage

JMS使用

 

One of the benefits of Springs JMS support is to shield the user from differences between the JMS 1.0.2 and 1.1 APIs. (For a description of the differences between the two APIs see sidebar on Domain Unification). Since it is now common to encounter only the JMS 1.1 API the use of classes that are based on the JMS 1.0.2 API has been deprecated in Spring 3.0. This section describes Spring JMS support for the JMS 1.0.2 deprecated classes.

springJMS之后ic的一个好处就是屏蔽了用于对于JMS1.0.21.1API的不同。(对于这两种API的不同参考他们的网站)。因此一旦遇到了JMS1.1API使用基于JMS1.0.2API并且这个API已经在spring3.0中被废弃了。这个章节描述了springJMS支持对于JMS1.0.2的废弃的类。

 

Domain Unification

 

There are two major releases of the JMS specification, 1.0.2 and 1.1.

这是JMS规范1.0.21.1主要的区别

 

JMS 1.0.2 defined two types of messaging domains, point-to-point (Queues) and publish/subscribe (Topics). The 1.0.2 API reflected these two messaging domains by providing a parallel class hierarchy for each domain. As a result, a client application became domain specific in its use of the JMS API. JMS 1.1 introduced the concept of domain unification that minimized both the functional differences and client API differences between the two domains. As an example of a functional difference that was removed, if you use a JMS 1.1 provider you can transactionally consume a message from one domain and produce a message on the other using the same Session.

JMS1.0.2定义了两种类型的方法domains,点对点(队列)和发布/订阅(主题)。1.0.2API引用这两种domains通过提供平行的类结构相对于每一个domain。因此,一个客户端应用成为domain通过使用指定的JMSAPIJMS1.1引入了domain unification的概念来最小化功能的不同对于客户端API在两个domain之间。由于功能的不同被移除,如果你使用一个JMS1.1的提供者你可以消费消息来自一个domain和生产一个消息对于其他使用相同的会话。

 

[Note]

注意

 

The JMS 1.1 specification was released in April 2002 and incorporated as part of J2EE 1.4 in November 2003. As a result, common J2EE 1.3 application servers which are still in widespread use (such as BEA WebLogic 8.1 and IBM WebSphere 5.1) are based on JMS 1.0.2.

JMS1.1规范依赖于2002年四月并且并且合并到J2EE1.4中在2003年的11月份,通常的J2EE1.3的应用服务器依然被广泛使用(例如BEAWebLogic8.1IBMWebSphere5.1)是基于JMS1.0.2的。

 

39.2.1 JmsTemplate

 

Located in the package org.springframework.jms.core the class JmsTemplate102 provides all of the features of the JmsTemplate described the JMS chapter, but is based on the JMS 1.0.2 API instead of the JMS 1.1 API. As a consequence, if you are using JmsTemplate102 you need to set the boolean property pubSubDomain to configure the JmsTemplate with knowledge of what JMS domain is being used. By default the value of this property is false, indicating that the point-to-point domain, Queues, will be used.

位于org.springframework.jms.core包中的类JmsTemplate102提供了所有的JmsTemplate特性描述在JMS章节中,但是基于JMS1.0.2API代替JMS1.1API。因此,如果你使用JmsTemplate102你需要设置一个布尔属性pubSubDomain来配置JmsTemplate使用JMSdomain。默认的这个属性的值是false,指定点对点的domain队列将被使用。

 

39.2.2 Asynchronous Message Reception

异步的消息重复

 

MessageListenerAdapters are used in conjunction with Springs message listener containers to support asynchronous message reception by exposing almost any class as a Message-driven POJO. If you are using the JMS 1.0.2 API, you will want to use the 1.0.2 specific classes such as MessageListenerAdapter102, SimpleMessageListenerContainer102, and DefaultMessageListenerContainer102. These classes provide the same functionality as the JMS 1.1 based counterparts but rely only on the JMS 1.0.2 API.

MessageListenerAdapter被使用配合spring的消息监听器来支持异步的消息复制通过暴露基于所有的类作为消息驱动的POJO。如果你使用JMS1.0.2API,你将需要使用1.0.2的指定类例如MessageListenerAdapter102SimpleMessageListenerContainer102DefaultMessageListenerContainer102。这些类提供了相同的功能作为基于JMS的基本部分只是依赖于JMS1.0.2API

 

39.2.3 Connections

连接

 

The ConnectionFactory interface is part of the JMS specification and serves as the entry point for working with JMS. Spring provides an implementation of the ConnectionFactory interface, SingleConnectionFactory102, based on the JMS 1.0.2 API that will return the same Connection on all createConnection() calls and ignore calls to close(). You will need to set the boolean property pubSubDomain to indicate which messaging domain is used as SingleConnectionFactory102 will always explicitly differentiate between a javax.jms.QueueConnection and a javax.jmsTopicConnection.

ConnectionFactory接口是JMS规范的一部分并且服务于JMS的接入点。spring提供了ConnectionFactory接口的实现,SingleConnectionFactory102基于JMS1.0.2API将返回相同的连接对于所有的createConnection调用和忽略调用close。你需要设置布尔属性pubSubDomain来指定消息domain来使用SingleConnectionFactory102来明确javax.jms.QueueConnectionjavax.jmsTopicConnection的不同。

 

39.2.4 Transaction Management

事务管理

 

In a JMS 1.0.2 environment the class JmsTransactionManager102 provides support for managing JMS transactions for a single Connection Factory. Please refer to the reference documentation on JMS Transaction Management for more information on this functionality.

JMS1.0.2环境的类JmsTransactionManager102提供了支持用于管理JMS的事务对于单一的连接工厂。请参考文档对于JMS事务管理来了解更多功能的信息。

 

 

你可能感兴趣的:(Spring框架参考文档翻译,V4.3.4.RELEASE)