Applicationcontext. XML in spring + hibernate

Source: Internet
Author: User

 

We take the order in the project as an example to briefly describe the integration of spring and hibernate. We have introduced how to use hibernate to map database tables. Here we are concerned about how to configure spring so that it can manage hibernate. In fact, as long as you configure a bean named sessionfactory in the spring configuration file (applicationcontext. xml here), spring can be connected with hibernate.
<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate. Local sessionfactorybean">
<Property name = "configlocation">
<Value> COM/ascent/bean/hibernate. cfg. xml </value>
</Property>
</Bean>
In this way, the first step of integration between spring and Hibernate is completed, and now the key point is: how can we combine spring and Hibernate to implement business logic?
Configure applicationcontext. xml first.
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate. hibernatetransactionmanager">
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean>
In the above, you may feel the benefits spring has brought to us. Spring's IOC mode allows you to manage all layers in a unified manner, while making them loosely coupled, this maximizes decoupling between layers, which has always been the pursuit of Web architecture.
However, spring brings more benefits than this. In addition to IOC and AOP, spring can use AOP to implement many functions. The most common one is transaction processing. Here we use the business service layer and Data Access Object layer. At the business service layer, we add transaction processing and the Data Access Object layer is responsible for data reading and writing.
First, assemble and configure service beans.
<Bean id = "orderservice" class = "org. springframework. transaction. Interceptor. transactionproxyfactorybean">
<Property name = "transactionmanager">
<Ref local = "transactionmanager"/>
</Property>
<Property name = "target">
<Ref local = "ordertarget"/>
</Property>
<Property name = "transactionattributes">
<Props>
<Prop key = "find *"> propagation_required, readonly,-orderexception </prop>
<Prop key = "Save *"> propagation_required,-orderexception,-orderminimum amountexception </prop>
</Props>
</Property>
</Bean>
Then, you need to assemble the business service objects and data access objects, and assign these objects to a transaction manager.
Configuration Information in spring.
<Bean id = "ordertarget" class = "com. ascent. Business. Service. orderserviceimpl">
<Property name = "orderdao">
<Ref local = "orderdao"/>
</Property>
</Bean>
 
<Bean id = "orderdao" class = "com. ascent. Dao. hibernate. orderhibernatedao">
<Property name = "sessionfactory">
<Ref local = "sessionfactory"/>
</Property>
</Bean>
Figure 1 outlines the construction of a project object. It can be seen that every object is associated with spring and can be injected to other objects through spring. Compare it with the spring configuration file and observe the relationship between them.

 

Spring builds various beans based on the configuration file.
Here we use a transactionproxyfactorybean, which defines a settransactionmanager (). This object is very useful and can easily process the items in the service object you declare, you can use the transbutes attribute of transaction to define how to handle it.
Transactionproxyfactorybean also has a setter, which will be referenced by the business service object (ordertarget). ordertarget defines the business service layer and has an attribute that is referenced by setorderdao.
Note that bean can be created in two ways, which are defined in sington and propotype. The default method is Singleton, which means that shared instances will be bound, and the prototype mode allows new instances when bean is used by spring. When each user needs to get the copy of their own bean, you should only use the prototype mode. In this way, spring and hibernate have completed the management of business objects.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lwj3025/archive/2007/11/15/1887434.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.