Results 1 to 3 of 3

Thread: Autowired objects null?

  1. #1
    Join Date
    Jun 2010
    Location
    Reston, VA
    Posts
    2

    Default Autowired objects null?

    I have a class similar to this (extraneous code snipped):
    Code:
    package com.mycompany.myproj;
    
    import java.util.List;
    import com.mycompany.persistence.dao.myStuffDao;
    import com.mycompany.persistence.model.myStuff;
    import org.springframework.beans.factory.annotation.Autowired;
    
    public class MyThread implements Runnable {
        @Autowired
        private MyStuffDao myStuffDao;
    
        public void run() {
            try
            {
                List<myStuff> stuffList = 
                    (List<myStuff>) myStuffDao.findAll();
                // I would do more stuff here, but myStuffDao is null.
            } catch (Exception e) {
                log.error("Oops: " + e.getMessage());
            }
        }    
    }
    I have a spring XML file that includes the following in the beans block:
    Code:
      <context:component-scan base-package="com.mycompany.myproj"/>
      <context:annotation-config />
      <bean id="app" class="com.mycompany.myproj.App"></bean>
    App gets kicked off by Tomcat, which then runs MyThread.

    Pretty simple, but at runtime, myStuffDao is null. I've used these persistence classes in a RESTful webapp, and they work fine. This is also in a webapp, but for some reason, Autowiring doesn't occur.

    Any tips on where to begin troubleshooting this would be great. I've been trying to get this to work for over a week now. I'm still a Spring amateur, so I'm hoping it's something obvious that I'm missing.
    Last edited by bm3719; Jan 18th, 2011 at 01:34 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Use the instance from the container, don't create a new instance yourself.

    That is probably what is going on.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2010
    Posts
    2

    Default @Service annotation maybe missing

    Please check inf @Service annotation or @component annotation is missing at the top of your class before putting it in the scan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •