GeeCON Prague 2014*
The moose likes Other Application Frameworks and the fly likes how to inject DAO with Spring??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "how to inject DAO with Spring???" Watch "how to inject DAO with Spring???" New topic
Author

how to inject DAO with Spring???

gopal kishan
Ranch Hand

Joined: Feb 23, 2005
Posts: 99
Hi All,

I am currently working of JSF with Hibernate. And i want to use Spring IOC for my DAO, so that i can avoid the multiple instances of DAO,

actually my framework is

JSF-serviceLayer -DAO(hibernate) - DB
i dont know how to configure those DAO with Spring.

please provide some information.

I want to inject those DAO.
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Originally posted by gopal kishan:
Hi All,

I am currently working of JSF with Hibernate. And i want to use Spring IOC for my DAO, so that i can avoid the multiple instances of DAO,

actually my framework is

JSF-serviceLayer -DAO(hibernate) - DB
i dont know how to configure those DAO with Spring.

please provide some information.

I want to inject those DAO.



Same as injecting any Java object into another:

(1) Create the DAO interface and an implementation using the technology of your choice:



(2) Create a service interface of some kind that will use the DAO:



(3) Create a service implementation that will use the DAO:



(4) Wire it into your Spring context for the service:



What's the problem? Any Spring book will show you how to do the wiring.


%
Michael Duffy
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Sorry, I forgot to add the setter for the DAO on the service implementation AND I didn't have it extend the interface as needed:



My mistake. Sorry.
gopal kishan
Ranch Hand

Joined: Feb 23, 2005
Posts: 99
hi

how to create spring context file.

it is like applicationcontext.xml???


I want to use spring only to inject DAO , not for other, i am directly calling hibernate session in my DAO.i am not using HibernateTemplate.

I need to use only for injection of my dao...
gopal kishan
Ranch Hand

Joined: Feb 23, 2005
Posts: 99
HI I did the same thing as u mentioned.

i created a applicationContext.xml and i placed my mappings. and i defined in serviceImpl.

when i run it shows NullPointerException?
Sahil Saxena
Greenhorn

Joined: May 26, 2005
Posts: 22
Hi,
I have also the same problem.
Today only I tried using JSF/Sprng/Hibernate but when i use Dao isntance from the service implementation class i also get the Null Pointer Exception.
Below is the method from the LoginServiceImpl class:

private LoginDao loginDao;

public void setLoginDao(LoginDao loginDao)
{
this.loginDao = loginDao;
}

public User login(String username, String password) throws Exception
{
User user = null;
try
{
user = this.loginDao.getUser(username);
...................

The configuration made in the applicationContext.xml is as follows

<!-- Login DAO object: Hibernate implementation -->
<bean id="loginDao" class="com.model.dao.hibernateimpl.LoginDaoHibernateImpl">

<property name="hibernateTemplate"><ref bean="hibernateTemplate"/></property>

</bean>

<!-- User Service Defintion -->
<bean id="loginService" class="com.model.service.impl.LoginServiceImpl">
<property name="loginDao"><ref local="loginDao"/></property>
</bean>

I will be also grateful if anyone figure out where am going wrong.
[ February 02, 2006: Message edited by: Sahil Saxena ]

SCJP 1.4 & SCWCD
sushma sree
Greenhorn

Joined: Mar 27, 2006
Posts: 12
Gopal,

if you look at your applicationContext.xml, you have mentioned the class attribute="LoginDaoHibernateImpl" whereas in your LoginServiceImpl class you have a setter for LoginDao object. So, you have to make sure that the setter in your class matches the class you specified in class attribute. Hope this is helpful. Let me know if i am wrong
Udaya Raj
Greenhorn

Joined: Nov 01, 2002
Posts: 26
Hi Gopal,

Did you find a solution? I am also facing the same issue?

Regards

Uday


IBM Certified WebSphere 4.0 Specialist<br />IBM Certified in OOAD and UML
 
GeeCON Prague 2014
 
subject: how to inject DAO with Spring???