Inject the Sessionfactory class with spring annotations

Source: Internet
Author: User
Brief Introduction

Currently, spring hibernate is used as the framework for the project, and object loading is done using annotations. It is often necessary to inject a Sessionfactory object when loading a DAO object, usually with DAO inheriting to hibernatedaosuppor,t and then adding Setsupersessionfactory method to the DAO to inject, There's a better way to go online these days, so let's take a look at both of these methods. method One (inherit Hibernatedaosupport) This is a more common method, see many articles used in this way. Preconditions: Sessionfactory has been configured in the spring configuration file in steps: 1. Inherit the Hibernatedaosupport Class 2. Add the Setsupersessionfactory method for injection.

@Repository public
class Aviatordao extends Hibernatedaosupport {
	
	//cannot be used directly setsessionfactory is because the hibernatedaosupport is defined as the final 
	@Resource (name = "Sessionfactory") public 
	void Setsupersessionfactory ( Sessionfactory sessionfactory) {
		super.setsessionfactory (sessionfactory);
	}

}

method Two (Inherit Basedao tool base class)
Preconditions: Sessionfactory The steps are configured in the Spring configuration file:
1. Write the Basedao tool base class and define the Sessionfactory property 2. Annotate this sessionfactory (configure @resource tag) 3.Dao inherit Basedao tool base class

Reference code: Principle Analysis In fact, the principle of injection is relatively simple, sessionfactory itself in the spring environment has already, so in fact, in DAO itself can also annotate a sessionfactory, But the advantages of these two approaches lie in the fact that they inject sessionfactory and inherit some common methods.

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.