Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7433251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:35:09+00:00 2026-05-29T09:35:09+00:00

from the app-context.xml: <bean id=userDao class=com.vaannila.dao.UserDAOImpl> <property name=sessionFactory ref=mySessionFactory/> </bean> <bean name=MyServiceT class=com.s.server.ServiceT> <property

  • 0

from the app-context.xml:

 <bean id="userDao" class="com.vaannila.dao.UserDAOImpl">
    <property name="sessionFactory" ref="mySessionFactory"/>
</bean>  

<bean name="MyServiceT" class="com.s.server.ServiceT">
    <property name="userDao" ref="userDao"/>
</bean> 

and inside ServiceT.java:

private UserDAO userDao;

public void setUserDao(UserDAO userDao){
    this.userDao = userDao;
}

the issue is: the setUserDao is called when the server goes on but when I call my doGet method:

    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    PrintWriter writer = response.getWriter();

    writer.write("hello");
    }

the userDao is null.
I put a breakpoint inside the setUserDao method and than another one inside the doGet method and saw that it is not the same insatnce… what is the reason? how can I fix it?
thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:35:10+00:00Added an answer on May 29, 2026 at 9:35 am

    Spring is atowiring your bean correctly, the problem is that servlet container instantiates your servlet independently of spring. So you basically have two different instances – one created by spring and another created by container.

    One workaround is to use ServletContextAttributeExporter, by putting the following in your app-context.xml:

    <bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
     <property name="attributes">
         <map>
             <entry key="userDao">
                 <ref bean="userDao"/>
             </entry>
          </map>
    </property>
    

    and then, in your servlet:

    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    
        UserDao userDao = (UserDao)getServletContext().getAttribute("userDao");
    
        // do something with userDao
    
        PrintWriter writer = response.getWriter();
    
        writer.write("hello");
    }
    

    another is to access the WebApplicationContext directly:

    protected void doGet(HttpServletRequest reqest, HttpServletResponse response)
                                         throws ServletException, IOException {
    
        WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserDao userDao =(UserDao)springContext.getBean("userDao");
    
     }
    

    … or simply use Spring MVC and let it autowire everything like it should.

    Also see this blog post. It might be easier to convert your servlet to HttpRequestHandler and let it be served by HttpRequestHandlerServlet, both provided by spring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app-config.xml has a definition for my UserDao bean: <bean id=userDao class=com.blah.core.db.hibernate.UserDaoImpl> <property name=sessionFactory
Tried to configure Spring for tests with hibernate and transactions. Getting bean from app
Running my my Test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {classpath:eq-mo-dcc-context-block.xml}) public class SpringTest { @Autowired
My app downloads content from a server and recently I've noticed the GUI has
I'm working on a phonegap iPhone app which downloads content from the network in
We have the rails app, content served from the database. Now we need to
Here's the line from App.Config: <add key=CheckFileFormatString value=P{0}\t&quot;{1}, {2}&quot;\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}/> Here's the code that puts
I am testing whether option from app.config listed below Is applied to all transactions
Note: I am cross-posting this from App Engine group because I got no answers
I know how to open an URL from app in Safari using [[UIApplication sharedApplication]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.