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.