Inject sessionfactory with annotations for Hibernatedaosupport

Source: Internet
Author: User

Original address

http://blog.csdn.net/tsingheng/article/details/8847047

All know spring provides 0 configuration features, and see someone else's project using Spring+mybatis, only defined in the Applicationcontext.xml sqlsessionfactory, and then nothing is configured, Basedao inherits from Sqlsessiondaosupport, just add @Repository annotations on each DAO, and spring automatically injects Sqlsessionfactory objects into the object.

I use hibernate imitate the other people's example, I also only in Applicationcontext.xml definition Sessionfactory,basedao inherit from Hibernatedaosupport, the result how to get all error,' Sessionfactory ' or ' hibernatetemplate ' is required

After tracing for a long, found that Sqlsessiondaosupport and hibernatedaosupport are not the same, the former has such a method

[Java]View PlainCopy
    1. @Autowired (required=false)
    2. Private void Setsqlsessionfacoty (Sqlsessionfacoty sqlsessionfacoty) {
    3. this.sqlsessionfacoty = Sqlsessionfacoty;
    4. }


And the latter method is

[Java]View PlainCopy
    1. Public void Setsessionfacoty (Sessionfacoty sessionfactory) {
    2. this.sessionfactory = Sessionfacoty;
    3. }


The latter is not @autowired, so spring does not automatically assemble sessionfacotry for objects that inherit hibernatedaosupport.

So how to solve this problem there are many ways, such as your own Hibernatedaosupport code to add an automatic assembly of annotations, as their own implementation.

The simplest way to add a method to the Basedao

[Java]View PlainCopy
    1. @Autowired
    2. Public void Setmysessionfactory (Sessionfactory sessionfactory) {
    3. super.setsessionfactory (sessionfactory);
    4. }

Spring is automatically assembled by type, so the method name can be written casually so that the object of the subclass can be automatically injected with sessionfactory.

In order not to give the students who have not used the 0 configuration time-consuming, the important configuration to paste out:


The Applicationcontext.xml is configured as follows

[HTML]View PlainCopy
    1. <!--auto-scan configuration-
    2. <context:component-scan base-package="Com.dao"/>
    3. <context:component-scan base-package="Com.service"/>

If you use annotations to configure things, add

[HTML]View PlainCopy
      1. <tx:annotation-driven transaction-manager="TransactionManager"/>

Use annotations to inject sessionfactory (go) for Hibernatedaosupport

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.