Results 1 to 6 of 6

Thread: Problem injecting sessionFactory by name

  1. #1
    Join Date
    May 2007
    Posts
    6

    Default Problem injecting sessionFactory by name

    I'm exploring ways to use springframework annotations on our existing application. It works well in the current state (spring 2.0.3 no annotations etc)

    I got the DI working at the service layer. But it is failing in the DAO layer.
    We have 3 session factory definitions and I'm trying to inject one of them in the DAO. When i try to use @Autowired it fails ( due to 3 bean definitions for the type). When I try to use @Resource with name="sessionFactory" (one of the bean names configured in our spring-beans.xml) , i get an error that says illegal argument exception

    nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
    Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
    at org.springframework.orm.hibernate3.support.Hiberna teDaoSupport.checkDaoConfig(HibernateDaoSupport.ja va:115)
    at org.springframework.dao.support.DaoSupport.afterPr opertiesSet(DaoSupport.java:44)
    I'm trying with the following code, but the compiler wont let me put the @Resource on the constructor and setSessionFactory method is final in HibernateDaoSupport.

    Code:
    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;
    // public void setSessionFactory(SessionFactory sessionFactory) {
    // this.sessionFactory = sessionFactory;
    // }
    public TPHibernateDAO(SessionFactory sessionFactory){
    setSessionFactory(sessionFactory);
    }
    Can someone help me how to inject the sessionFactory by name using annotations?
    I'm trying to do this with the new spring-framework-2.1-m1
    Last edited by dasikalyan; May 21st, 2007 at 01:02 PM.

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    Field injection for the sessionFactory is not an option since the base class does not actually have a 'sessionFactory' field. The setSessionFactory(..) method actually just instantiates and sets the 'hibernateTemplate' field. Therefore, one option would be to provide a bean definition for the hibernateTemplate (with a single property: 'sessionFactory') and then use field injection for that. Another option would be to provide your own method with a different name (due to the 'final' superclass version), and then call super.setSessionFactory(..) from your method.

  3. #3
    Join Date
    May 2007
    Posts
    6

    Default

    Thanks for your reply.
    sounds like a good idea to me. I'll give it a try and will let you know.

  4. #4
    Join Date
    May 2007
    Posts
    6

    Default

    Mark,
    I did the following.

    In spring-beans.xml, I defined a new bean definition.
    Code:
    <bean id="hibernateTemplate"
    	class="org.springframework.orm.hibernate3.HibernateTemplate">
    	<property name="sessionFactory">
    		<ref local="sessionFactory" />
    	</property>
    </bean>
    In the DAO
    Code:
    	@Autowired
     	private HibernateTemplate hibernateTemplate;
    I get the following error
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'treatmentPatternDAOTarget': Injection of resources failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private org.springframework.orm.hibernate3.HibernateTempla te com.gs.core.services.dao.hibernate.TreatmentPatter nHibernateDAO.hibernateTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [org.springframework.orm.hibernate3.HibernateTempla te] is defined: expected single bean but found 0
    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private org.springframework.orm.hibernate3.HibernateTempla te com.gs.core.services.dao.hibernate.TreatmentPatter nHibernateDAO.hibernateTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [org.springframework.orm.hibernate3.HibernateTempla te] is defined: expected single bean but found 0
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No unique bean of type [org.springframework.orm.hibernate3.HibernateTempla te] is defined: expected single bean but found 0
    at org.springframework.beans.factory.BeanFactoryUtils .beanOfTypeIncludingAncestors(BeanFactoryUtils.jav a:303)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.getBeanOfType(Auto wiredAnnotationBeanPostProcessor.java:178)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredMember.au towireMember(AutowiredAnnotationBeanPostProcessor. java:222)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiringMetadata .autowireMembers(AutowiredAnnotationBeanPostProces sor.java:196)
    at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessAfterIn stantiation(AutowiredAnnotationBeanPostProcessor.j ava:129)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:412)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 51)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:156)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:160)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:287)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:352)
    at org.springframework.test.AbstractSingleSpringConte xtTests.createApplicationContext(AbstractSingleSpr ingContextTests.java:199)
    at org.springframework.test.AbstractSingleSpringConte xtTests.loadContextLocations(AbstractSingleSpringC ontextTests.java:179)
    at org.springframework.test.AbstractSingleSpringConte xtTests.loadContext(AbstractSingleSpringContextTes ts.java:158)
    at org.springframework.test.AbstractSpringContextTest s.getContext(AbstractSpringContextTests.java:105)
    at org.springframework.test.AbstractSingleSpringConte xtTests.setUp(AbstractSingleSpringContextTests.jav a:87)
    at junit.framework.TestCase.runBare(TestCase.java:125 )
    at org.springframework.test.ConditionalTestCase.runBa re(ConditionalTestCase.java:69)
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:2 08)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:478)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:344)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)

  5. #5
    Join Date
    May 2007
    Posts
    6

    Default

    Solved it by defining a pojo with a single property reference to sessionFactory. Defined a method injection and called super.setSessionFactory(...

    thanks Mark.

  6. #6

    Default

    Quote Originally Posted by dasikalyan View Post
    Solved it by defining a pojo with a single property reference to sessionFactory. Defined a method injection and called super.setSessionFactory(...

    thanks Mark.
    Hello, little old thread but I'll give a chance...

    dasikalyan so you created a POJO with the single attribute "sessionFactory", I assume that this POJO was "HibernateTemplate", where did you located it on you classpath? I mean, in which package?

    I guess the bean definition (spring-beans.xml) changed to point your POJO, right?

    Code:
    <bean id="hibernateTemplate"
    	class="org.springframework.orm.hibernate3.HibernateTemplate">
    	<property name="sessionFactory">
    		<ref local="sessionFactory" />
    	</property>
    </bean>
    Thx in advance for any help, tip or hint.

Posting Permissions

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