开发者

spring web MVC and hibernate setup - how to get a hold of your hibernate bean?

开发者 https://www.devze.com 2023-01-21 08:26 出处:网络
I am new to Spring and the bean concept, so sorry if asking the obviously. I have set up a Java project and used the Hibernate framework to make my connections to the DB (using hibernate tool on eclip

I am new to Spring and the bean concept, so sorry if asking the obviously. I have set up a Java project and used the Hibernate framework to make my connections to the DB (using hibernate tool on eclipse, really recommended btw). My basic setup was as follows:

  1. some hibernate pojos (generated by Hibernate tools),
  2. hibernate mapping files (hbm.xml files).
  3. The configuration was set in hibernate.cfg.xml file placed at my root of the packages.
  4. I initially setup a HibernateUtils class that gets a session from my sessionFactory.
  5. some classes that act as Dao - creates the queries using the pojos and the hibernateUtils to get the session.

This was working fine with hibernate's powerful framework. Then I migrated to project to Spring MVC. From the various tutorials I read I understood that some changes need to be done in order to make things play nicely again. The m开发者_如何学Goain change is that the Spring FW now loads the beans by itself, therefore the following:

  1. the pojos did not change, nor did the hbm.xml files
  2. The configuration is no longer set at hibernate.cfg.xml - the Spring MVC FW loads it's own LocalSessionFactoryBean once I declared it in the XML files (Spring search for xmls as part of the initialization process). So I created a separate xml file called spring-hibernate.xml, that has the DB definition, the session factory bean and a hibernate template. To complete it, the xml file also declare the location of the Dao and the hbm files.
  3. with hibernate template configured in the xml, there is a bean injection, therefore I changed the Dao files to add getter/setter to a hibernate template property. Then using this - the Dao class may create the db query.

All of this configuration works fine and I can get the Spring FW initialize the Dao classes (I put a sysout just to confirm that the Dao classes are injected with the hibernate template during MVC startup). Now my problem is - how to get access to the Dao bean instance created?

In my logic part, I get a hold of the bean as follows:

ClassPathXmlApplicationContext appContex = new ClassPathXmlApplicationContext(new String[] {"spring-hibernate.xml"});

UserUIDDao userUIDDao= (UserUIDDao)appContext.getBean("UserUIDDao");

Problem is - this is a NEW reference to a newly created bean and not the same one created during the spring MVC startup.

So my questions are: how to get a hold of the original bean created by the Spring init process? and - is my setup correct?


the pojos did not change, nor did the hbm.xml files

That's correct.

The configuration is no longer set at hibernate.cfg.xml - the Spring MVC FW loads it's own LocalSessionFactoryBean once I declared it in the XML files (Spring search for xmls as part of the initialization process).

You can choose to use Spring to configure Hibernate (but you could keep your hibernate.cfg.xml).

So I created a separate xml file called spring-hibernate.xml, that has the DB definition, the session factory bean and a hibernate template. To complete it, the xml file also declare the location of the Dao and the hbm files. with hibernate template configured in the xml, there is a bean injection, therefore I changed the Dao files to add getter/setter to a hibernate template property. Then using this - the Dao class may create the db query.

Using Spring's HibernateTemplate is one option, but you could also go template-less and just inject Hibernate's SessionFactory and use sessionFactory.getCurrentSession().

This is actually the officially recommended approach for new projects, check out So should you still use Spring's HibernateTemplate and/or JpaTemplate?? and the javadoc of HibernateTemplate.

Now my problem is - how to get access to the Dao bean instance created?

You inject it where needed (typically, in your services).

In my logic part, I get a hold of the bean as follows (...)

No, don't do this. This "service locator approach" actually defeats the point of a DI container like Spring. Configure Spring to inject your DAOs where needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消