Spring+hibernate in Applicationcontext.xml detailed

Source: Internet
Author: User
Tags aop

It's called << Project. Practical Solution: Java application development based on Struts-spring-hibernate >>

Some of the concepts are not familiar with, a word "halo" ah, search on the Internet data, explain applicationcontext.xml we take the order of the project as an example to briefly explain the integration of Spring and Hibernate. about how to use Hibernate to map database tables, as we've already done before, we're concerned about how to configure Spring so that it can manage Hibernate. In fact, as long as you configure a bean called Sessionfactory in the spring's configuration file (we are here applicationcontext.xml), spring can be associated 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> So, the first integration of Spring and Hibernate is done, and now it's the key--how to get Spri Ng and Hibernate double swords to achieve business logic. Or make a configuration first in the Applicationcontext.xml file. <bean id= "TransactionManager" class= "Org.springframework.orm.hibernate". Hibernatetransactionmanager ">     <property name=" Sessionfactory ">        <ref local= "sessionfactory"/>     </property>   </bean> on top you probablyWe can feel the benefits spring brings us, the spring IoC model can manage each layer uniformly, and make each layer loosely coupled, so that the maximum decoupling between the layers, this is the WEB architecture has always been the pursuit. But the benefits of spring are more than that, and with the exception of the IoC and AOP, spring can use AOP to implement many functions, the most common of which is transaction processing. Here we use the Business Services (business service) layer and the data Access object layer, in the business services layer we increase transaction processing, data Access object layer is responsible for data read and write. First of all, 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>         &LT;PROp key= "save*" >propagation_required,-orderexception,-orderminimum amountexception</prop>    After     </props>     </property>   </bean>, we need to assemble business service objects and data Access objects. and match these objects to a transaction manager (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 6-10 is an outline of the project object. As you can see, each object is associated with spring and can be injected into other objects through spring. Compare it to the Spring configuration file to see how they relate to each other. This is how Spring is built with each Bean based on the configuration file. Here we use a transactionproxyfactorybean, which defines a SettransactionmanaGER (), this object is very useful, it can easily handle the things in your stated Service Object, you can define how to handle it by transaction the Attributes attribute. Transactionproxyfactorybean also has a setter, which is referenced by the Business service object (ordertarget), Ordertarget defines the business service layer, and it has an attribute, by Setorderdao () Reference to this property. It is also important to note that the bean can be created in two ways, which are defined in the single case pattern (Sington) and the prototype pattern (Propotype). The default approach is singleton, which means that the shared instance will be bound, while the prototype pattern allows the new instance to be created when Spring uses the bean. When each user needs to get Copy of their own Bean, you should only use the prototype mode.   In this way, Spring and Hibernate complete the management of business objects.

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.