Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Autowiring problem

  1. #11
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    No you have to add it to EACH applicationcontext loaded.

    In general you have a DispatcherServlet which should only contain the web stuff (controllers, viewresolvers etc) and a ContextLoaderListener which loads the general stuff. However the DispatcherServlet has an ApplicationContext AND the ContextLoaderListener loads an ApplicationContext.

    The DispatcherServlet has full access to the ApplicationContext loaded by the ContextLoaderListener but its configuration doesn't influence it nor does it the other way around.

    So adding context:annotation-config to the dispatcherservlet configuration doesn't automatically mean that your xml files (the applicationcontext) loaded by the ContextLoaderListener are also going to use/activate annotation based configuration.
    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

  2. #12
    Join Date
    Jun 2009
    Posts
    106

    Default contextload listener location

    Okay this is getting kinda confusing....

    I have always had my contextLoadListener in my web.xml as so:

    Code:
        <listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value> 
    					 classpath:data-access-context.xml
    		</param-value>
    	</context-param>
    is this the wrong location for this? NOTE: that I am defining my data-access-context location here.
    I have also added to my data-access-context.xml:
    Code:
    <context:component-scan base-package="com.logixplayer.pf" />
    	<context:annotation-config />
    but now I am getting errors at startup:

    Code:
    Jan 1, 2010 3:14:01 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring root WebApplicationContext
    Jan 1, 2010 3:14:05 PM org.apache.catalina.core.StandardContext listenerStart
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homePageController': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.logixplayer.pf.service.IProfileService com.logixplayer.pf.homepage.controller.HomePageController.profileService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IProfileService' defined in class path resource [application-context-iProfileService.xml]: Cannot resolve reference to bean 'ProfileDAO' while setting bean property 'IProfileDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ProfileDAO' defined in class path resource [data-access-context.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [data-access-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: dataAccessPU] Unable to build EntityManagerFactory
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.logixplayer.pf.service.IProfileService com.logixplayer.pf.homepage.controller.HomePageController.profileService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IProfileService' defined in class path resource [application-context-iProfileService.xml]: Cannot resolve reference to bean 'ProfileDAO' while setting bean property 'IProfileDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ProfileDAO' defined in class path resource [data-access-context.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [data-access-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: dataAccessPU] Unable to build EntityManagerFactory
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
    	... 40 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IProfileService' defined in class path resource [application-context-iProfileService.xml]: Cannot resolve reference to bean 'ProfileDAO' while setting bean property 'IProfileDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ProfileDAO' defined in class path resource [data-access-context.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [data-access-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: dataAccessPU] Unable to build EntityManagerFactory
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
    	... 42 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ProfileDAO' defined in class path resource [data-access-context.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [data-access-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: dataAccessPU] Unable to build EntityManagerFactory
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
    	... 57 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [data-access-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: dataAccessPU] Unable to build EntityManagerFactory
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
    	... 70 more
    Caused by: javax.persistence.PersistenceException: [PersistenceUnit: dataAccessPU] Unable to build EntityManagerFactory
    	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
    	at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
    	... 80 more
    Caused by: org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
    	at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:57)
    	at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:39)
    	at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:426)
    	at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
    	... 86 more
    Jan 1, 2010 3:14:05 PM org.apache.catalina.core.ApplicationContext log
    INFO: Set web app root system property: 'pf' = [C:\apache-tomcat-6.0.20\webapps\PublicFountainWAR-0.0.1-SNAPSHOT\]
    Jan 1, 2010 3:14:05 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing log4j from [C:\apache-tomcat-6.0.20\webapps\PublicFountainWAR-0.0.1-SNAPSHOT\WEB-INF\conf\log4j.xml]
    Jan 1, 2010 3:14:05 PM org.apache.catalina.core.ApplicationContext log
    INFO: Shutting down log4j
    Jan 1, 2010 3:14:05 PM org.apache.catalina.core.ApplicationContext log
    INFO: Closing Spring root WebApplicationContext
    Jan 1, 2010 3:14:07 PM org.apache.catalina.core.ApplicationContext log
    INFO: ContextListener: contextInitialized()
    Jan 1, 2010 3:14:07 PM org.apache.catalina.core.ApplicationContext log
    INFO: SessionListener: contextInitialized()
    its talking about how the entitymanager cannot be instantiated because of Hibernate Dialect must be explicitly set and other errors..

    I think the way I have organized all of this might not be correct. i.e. the location of the org.springframework.web.context.ContextLoaderListe ner or the way I am setting up the component scan? In the meantime I can try to resolve the hibernate dialect issue but I am not sure if this is really the crux of the problem...

  3. #13
    Join Date
    Jun 2009
    Posts
    106

    Smile looks like its solved for now....

    hey Martin and Spring Users...

    thank you Marten for your help, much appreciated...
    after I made the changes from my last post, I was getting an error with the hibernate.dialect so I added this to my persistence.xml:

    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

    and now I am not getting null values for my entity manager and internal autowired properties. That's amazing!
    I just hope this was the right way to go and I will be monitoring this fix to test it out thoroughly.

    Thank you for all your support!!

  4. #14
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Well there is one issue right now, which might not be apparent or quite clear.

    You have 2 component-scan elements now which means your controllers are instantiated twice (they are detected twice) so you might want to either remove the component-scan from the root (ContextLoaderListener) application context because you already specified all the beans (leave the context:annotation-config in place!). Or do some more configuration on the component-scan element. Check the reference guide for options.
    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

Tags for this Thread

Posting Permissions

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