Spring uses annotations to inject hibernatetemplate sessionfactory exceptions

Source: Internet
Author: User
@Component public class Hibernatetemplate {private static final threadlocal<session> ThreadLocal = new Threadlo        
    Cal<session> ();	
	
    private static Org.hibernate.SessionFactory sessionfactory; @Resource (name= "sessionfactory")//Here the static method using annotations will be unusually public static void Setsessionfactory (Org.hibernate.SessionFactory
	sessionfactory) {hibernatetemplate.sessionfactory = sessionfactory;  }/** * Returns the ThreadLocal Session instance.
     Lazy Initialize * The <code>SessionFactory</code> if needed. * * @return Session * @throws Hibernateexception */public static session getsession () throws Hiberna

		Teexception {Session session = (session) Threadlocal.get (); 
					if (session = = NULL | |!session.isopen ()) {session = (sessionfactory! = null)? Sessionfactory.getcurrentsession ()
			: null;
		Threadlocal.set (session);
    } return session; }/** * Close the single hibernate session inStance. * * @throws hibernateexception */public static void CloseSession () throws Hibernateexception {Ses
        Sion Session = (session) Threadlocal.get ();

        Threadlocal.set (NULL);
        if (session! = NULL) {session.close (); }}/** * return session Factory * */public static org.hibernate.SessionFactory Getsessionfactory (
	) {return sessionfactory; }

	
}


Using the annotation method to inject sessionfactory an exception occurs:

caused by:org.springframework.beans.factory.BeanCreationException:Error creating bean with Name ' hibernatetemplate ' Defined in class path resource [Applicationcontext.xml]: post-processing failed of bean type [class Com.util.HibernateTemp Late] failed; Nested exception is java.lang.IllegalStateException:

@Resource annotation is isn't supported on static methods


Workaround:

Remove the annotations and replace them with hibernatetemplate in the Applicationcontext.xml.

<bean id= "Hibernatetemplate" class= "Com.util.HibernateTemplate" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>


or change the Sessessionfactory method to non-static


You can then inject hibernatetemplate from Basedao using annotation methods

Private hibernatetemplate template;

Public hibernatetemplate gettemplate () {
return template;
}

@Resource (name= "hibernatetemplate")
public void SetTemplate (hibernatetemplate template) {
This.template = template;
}

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.