Announcement Announcement Module
Collapse
No announcement yet.
Autowiring problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring problem

    I have a dao object that I am trying to autowire with a sessionFactory Object that is defined in my appContext. I can autowire simple objects just fine if I annotate them with @Component. But I have my Dao Object Annotated with @Repository("tstDao") and I have it listed in my appContext with

    <bean id="tstDao"
    class="mypackage.dao.TstDao">
    <property name="sessionFactory" ref="mytestSessionFactory" />
    </bean>

    I also have my appContext set up to scan the classpath for annotated object with

    <context:annotation-config/>
    <context:component-scan base-package="mypackage"/>

    However when I add the Autowiring config to my controller, it throws the following Exception
    java.lang.NoClassDefFoundError: org/springframework/orm/ObjectRetrievalFailureException
    java.lang.ClassLoader.defineClass1(Native Method)
    java.lang.ClassLoader.defineClass(ClassLoader.java :620)


    Here is my Controller class:
    package mypackage.controller;


    @Controller
    public class PriorityController
    {
    /* The autowiring for the TestBean works fine
    I have the TestBean class annotated as @Component("testBean")
    If I comment out all the dao related stuff, the view will render but my
    list will be empty. It seems that there is a problem with autowiring only
    related to the dao object.
    */
    @Autowired
    private TestBean test;
    @Autowired
    @Qualifier("tstDao")
    private TstDao tstDao;


    @RequestMapping("/createTstList.htm")
    public String renderForm(Model model )
    {
    model.addAttribute(test);
    model.addAttribute( "tstList", tstDao.find( "from Tst " ) );
    return "listTst";
    }

    }

  • #2
    Originally posted by solid View Post
    ...

    However when I add the Autowiring config to my controller, it throws the following Exception
    java.lang.NoClassDefFoundError: org/springframework/orm/ObjectRetrievalFailureException
    java.lang.ClassLoader.defineClass1(Native Method)
    java.lang.ClassLoader.defineClass(ClassLoader.java :620)
    I'm afraid it's not possible to give an exact answer for the described symptoms. Build a simple test-case that is possible to post here, I'll take a look.

    However, java.lang.NoClassDefFoundError: org/springframework/orm/ObjectRetrievalFailureException looks very suspicious.

    Comment


    • #3
      tried everything

      OK now I think I have tried everything.

      I have a controller class that has a method. in that method I instantiate a PriorityDao Object via
      Code:
      PriorityDao dao = new PriorityDao();
      here is my PriorityDao Class
      Code:
      @Component("priorityDao")
      //@Repository("priorityDao")
      //@Transactional
      public class PriorityDao extends GenericHibernateDao<Priority, Long>
      {
          @Autowired
          @Qualifier("mySessionFactory") 
          private SessionFactory sessionFactory;
      
          //    
          
          public PriorityDao( )
          {
              super( Priority.class ); // Priority object is my custom entity class
          }
          
      }
      Here is the GenericHibernateDao object
      Code:
      public class GenericHibernateDao<T extends GenericEntity, PK extends Serializable> 
      {
      
          private Class<T> type;
         
          private SessionFactory sessionFactory;
          private HibernateTemplate hibernateTemplate; 
      
          
          public GenericHibernateDao(Class<T> type)
          {
              this.type = type;
              System.out.println("GenericHibernateDao instantiated.");
              
          }
          
          @SuppressWarnings("unchecked")
          public List find(final String s )
          {
             
              return getHibernateTemplate().find( s );
          }
      
          public SessionFactory getSessionFactory()
          {
              return sessionFactory;
          }
          public void setSessionFactory(SessionFactory sessionFactory )
          {
              System.out.println("session Factory created.");
              System.out.println(sessionFactory.getAllClassMetadata());
              this.sessionFactory = sessionFactory;
              this.hibernateTemplate= new HibernateTemplate(this.sessionFactory);
          }
          public HibernateTemplate getHibernateTemplate()
          {
              return this.hibernateTemplate;
          }
          public void setHibernateTemplate( HibernateTemplate hibernateTemplate )
          {
              this.hibernateTemplate = hibernateTemplate;
          }
      }
      how come the simple instantiation of the PriorityDao object does not trigger the setSessionFactory() method of the parent GenericHibernateDao class? What annotations do I need to add to force the autowiring and calling of the setSessionFactory method?

      Comment


      • #4
        Spring container is not involved in new object creation, hence, it is not able to resolve the dependencies. You should consider to retrieve necessary object from the container or use AspectJ support and @Configurable annotation - 6.8.1. Using AspectJ to dependency inject domain objects with Spring

        Comment


        • #5
          Originally posted by denis.zhdanov View Post
          I'm afraid it's not possible to give an exact answer for the described symptoms. Build a simple test-case that is possible to post here, I'll take a look.

          However, java.lang.NoClassDefFoundError: org/springframework/orm/ObjectRetrievalFailureException looks very suspicious.
          Yes, the Spring ORM jar is missing. See

          http://www.springsource.com/reposito...ilureException

          Comment


          • #6
            Now I am confused

            Ok what then is the right way to instantiate my do object while forcing the sessionFactory setter method to be called?

            Comment


            • #7
              can anyone answer this?

              I need to be able to auto wire my dao. Does anyone know how to do this?

              Comment


              • #8
                nevermind

                I added spring orm as a dependency of this project and suddenly adding Autowired to the controller allowed it to work. Now who should I talk to about better exceptions???

                Comment

                Working...
                X