Injecting sessionfactory into DAO by annotation

Source: Internet
Author: User

Most of the time we are using XML configuration to inject sessionfactory into the DAO to instantiate the bean, sometimes the DAO instantiation is implemented by annotations.

The biggest obstacle to DAO annotations is how to inject sessionfactory with annotations at the same time.

DAO inherits from Hibernatedaosupport, Sessionfactory from Hibernatedaosupport, We cannot modify the Sessionfactory attribute in Hibernatedaosupprot to annotate, nor can we override the Setsessionfactory method of the final type. So if you want to inject the sessionfatory, you have to find another way.

Either way, it is essentially assigning sessionfactory to the sessionfactory in Hibernatedaosupprot, so that we can arbitrarily use any of the methods in Hibernatedaosupport.

The first method: inject by name by @resouce--(@Autowired-injected by type) and execute the method body content [Java] view plain copy print? The method type in Hibernatedaosupprot is final

  Java code   public final void setsessionfactory (sessionfactory sessionfactory)  {           if  (this.hibernatetemplate ==  null | |  sessionfactory != this.hibernatetemplate.getsessionfactory ())  {                this.hibernateTemplate =  Createhibernatetemplate (sessionfactory);           }        }      Public final void sethibernatetemplate ( hibernatetemplate hibernatetemplate)  {            this.hibernatetemplate = hibernatetemplate;       }  



Workaround: Java Code @Transactional @Repository ("Baselocationdao") public class Baselocationdaoimpl extends Hibernatedaosupport im              Plements locationdao{private Logger Logger = Loggerfactory.getlogger (Baselocationdaoimpl.class); @Resource public void Setmysessionfactory (Sessionfactory sessionfactory) {super.setsessionfactory (sessionf       Actory); }   }

The second method: the @resource comment above actually has two effects, one is that the method needs to be executed when the DAO is instantiated, and the other effect is to inject a sessionfactory to the method. The combination of the two eventually assigns the injected sessionfactory to the Sesseionfactory attribute in the superclass, resulting in similar injections, which can be used directly in the superclass. Then this second method is the first method of two effects separated.

@Repository

public class Testdaoimpl extends Hibernatedaosupport implements
Itestdao {

/**
* Inject Mysessionfactory
*/
@Autowired @qualifier ("mysessionfactory")/***** injection *****/
protected Sessionfactory mysessionfactory;
This method is executed when the/*****bean is instantiated @PostConstruct *******/
protected void Injectsessionfactory () {
Super.setsessionfactory (mysessionfactory);
}

}

The method is divided into two steps, one step injection and one step assignment.

The third approach: inheriting the Hibernatedaosupport class, mostly to use the various methods in its Hibernatetemplate class, can be achieved by combining rather than inheriting.

public class Testdaoimp implements itestdao{

Hibernatetemplate hibernatetemplate;

@Resource

public void Setmysessionfactory (Sessionfactory sessionfactory) {

This.hibernatetemplate = new Hibernatetemplate (mysessionfactory);

}

Public Hibernatetemplate gethibernatetemplate () {
return hibernatetemplate;

}
}

This method can be used to refer to the Gethibernatetemplate () method.

The same can be changed by the second method:

@Autowired @qualifier ("mysessionfactory")/***** injection *****/
protected Sessionfactory mysessionfactory;
This method is executed when the/*****bean is instantiated @PostConstruct *******/
protected void Injecthibernatetemplate () {
This.hibernatetemplate = new Hibernatetemplate (mysessionfactory);
}

The fourth method, as in the above, is a combination, but injects a subclass of Hibernatedaosupport (Hibernatedaosupport can be configured directly in XML as a bean I don't know), and the subclass is first configured as a bean in XML.

You can directly use its methods by first configuring the Bean in XML and then injecting the bean by combining the reference 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.