Use spring to configure declarative things for Hibernate

Source: Internet
Author: User


From an AOP point of view, things are like logs, things that are not related to business logic, and these two things are very appropriate to put into the system through a cut-in approach. Here, the things in Hibernate are configured in spring, so that we do not have to consider the existence of things when programming, and focus on the logical implementation of the system.


Add the following configuration to the spring configuration file:


<!--configuration sessionfactory--><bean id= "sessionfactory" class= " Org.springframework.orm.hibernate3.LocalSessionFactoryBean "><!--Configure hibernate configuration file--><property name=" Configlocation "><value>classpath:hibernate.cfg.xml</value></property></bean><!-- Configure transaction manager--><bean id= "TransactionManager" class= " Org.springframework.orm.hibernate3.HibernateTransactionManager "><property name=" Sessionfactory ">< Ref bean= "Sessionfactory"/></property></bean><!--Configure which classes of which methods use transactions--><aop:config>< Aop:pointcut id= "Allmanagermethod" expression= "Execution (* com.lhc.usermanage.*.* (..))" /><aop:advisor pointcut-ref= "Allmanagermethod" advice-ref= "Txadvice"/></aop:config><!-- Configure the propagation characteristics of a transaction--><tx:advice id= "Txadvice" transaction-manager= "TransactionManager" ><tx:attributes>< Tx:method name= "add*" propagation= "REQUIRED"/><tx:method name= "del*" propagation= "REQUIRED"/&GT;&LT;TX: Method Name= "modify*"propagation= "REQUIRED"/><tx:method name= "*" propagation= "REQUIRED" read-only= "true"/></tx:attributes ></tx:advice>


here, we let the DAO layer class inherit Hibernatedaosupport:





This simplifies crud operations.


However, you should also configure the Sessionfactory property for Usermanagerimpl:



In addition to the configuration of the things manager, the rest of the configuration is similar to the configuration we need when we use the dynamic agent for AOP, mainly defining where to cut, as for the content after the cut, of course, to join our things.


Brain hole big Open, feel this time cut should be before and after content.


In addition, there is a matter of how to cut into the problem, here is related to the spread of things, for example, I have a thing before, I will operate directly with this thing I still open another, and so on ...


Add a sentence, this time take everyone to write Crud method, it is necessary to define the naming rules, in order to match the advice inside the method name, to add things!!! Finally realized that the Convention is greater than the configuration. (Do not know the accuracy of understanding?) )


And less write a lot of code .... So lazy.






Use spring to configure declarative things for Hibernate

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.