Announcement Announcement Module
Collapse
No announcement yet.
Autowire doesn't work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowire doesn't work

    Hi All

    Can someone please tell me what am I doing wrong here, some how Autowire doesn't work in any of the class and it's been two long days.

    Error:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed;
    nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
    private com.logicalideas.service.ContactService com.logicalideas.controller.ContactController.conService; 
    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
    Could not autowire field: public com.logicalideas.dao.ContactsDAO com.logicalideas.service.ContactServiceImp.contactInterface; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactsImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.logicalideas.dao.ContactsImp.sessionFactory; nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;
    servlet-context.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    	http://www.springframework.org/schema/mvc 
    	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    	http://www.springframework.org/schema/context 
    	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- DispatcherServlet Context: defines this servlet's request-processing 
    		infrastructure -->
    
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<annotation-driven />
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    		up static resources in the ${webappRoot}/resources directory -->
    	<resources mapping="/resources/**" location="/resources/" />
    
    	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    		in the /WEB-INF/views directory -->
    	<beans:bean
    		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans:property name="prefix" value="/WEB-INF/views/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>
    	<context:component-scan base-package="com.logicalideas.controller" />
    	<context:component-scan base-package="com.logicalideas.dao" />
    	<context:component-scan base-package="com.logicalideas.persistence" />
    	<context:component-scan base-package="com.logicalideas.service" />
    
    	<beans:bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<beans:property name="dataSource" ref="dataSource" />		
    		<beans:property name="hibernateProperties">
    			<beans:props>
    				<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
    			</beans:props>
    		</beans:property>
    		<beans:property name="annotatedClasses">
    			<beans:list>
    				<beans:value> com.logicalideas.persistence.Contacts </beans:value>
    			</beans:list>
    		</beans:property>
    	</beans:bean>
    
    	<beans:bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<beans:property name="sessionFactory" ref="sessionFactory" />
    	</beans:bean>
    </beans:beans>
    Thanks

  • #2
    Hello

    Could not autowire field:
    private org.hibernate.SessionFactory com.logicalideas.dao.ContactsImp.sessionFactory;
    nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;
    Check that you have required dependencies, seems you have a problem of version too.

    Comment


    • #3
      Thanks for your reply dr_pompeii. Any ideas which dependencies are missing. I made the project from SpringSouce Template Projects I'm slightly lost to which further dependencies to bring in.

      my pom.xml
      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      	<modelVersion>4.0.0</modelVersion>
      	<groupId>com.logicalideas</groupId>
      	<artifactId>controller</artifactId>
      	<name>logicalideas</name>
      	<packaging>war</packaging>
      	<version>1.0.0-BUILD-SNAPSHOT</version>
      	<properties>
      		<java-version>1.6</java-version>
      		<org.springframework-version>3.1.0.RELEASE</org.springframework-version>
      		<org.aspectj-version>1.6.9</org.aspectj-version>
      		<org.slf4j-version>1.5.10</org.slf4j-version>
      	</properties>
      	<dependencies>
      		<!-- Spring -->
      		<dependency>
      			<groupId>org.springframework</groupId>
      			<artifactId>spring-context</artifactId>
      			<version>${org.springframework-version}</version>
      			<exclusions>
      				<!-- Exclude Commons Logging in favor of SLF4j -->
      				<exclusion>
      					<groupId>commons-logging</groupId>
      					<artifactId>commons-logging</artifactId>
      				</exclusion>
      			</exclusions>
      		</dependency>
      		<dependency>
      			<groupId>org.springframework</groupId>
      			<artifactId>spring-webmvc</artifactId>
      			<version>${org.springframework-version}</version>
      		</dependency>
      		<dependency>
      			<groupId>org.springframework</groupId>
      			<artifactId>spring-orm</artifactId>
      			<version>3.1.1.RELEASE</version>
      		</dependency>
      		<dependency>
      			<groupId>org.hibernate</groupId>
      			<artifactId>hibernate-entitymanager</artifactId>
      			<version>4.1.1.Final</version>
      		</dependency>
      		<dependency>
      			<groupId>org.hibernate.common</groupId>
      			<artifactId>hibernate-commons-annotations</artifactId>
      			<version>4.0.1.Final</version>
      		</dependency>
      		<dependency>
      			<groupId>cglib</groupId>
      			<artifactId>cglib</artifactId>
      			<version>2.2.2</version>
      			<scope>runtime</scope>
      		</dependency>
      
      		<!-- AspectJ -->
      
      		<dependency>
      			<groupId>org.aspectj</groupId>
      			<artifactId>aspectjrt</artifactId>
      			<version>${org.aspectj-version}</version>
      		</dependency>
      
      		<!-- Logging -->
      		<dependency>
      			<groupId>org.slf4j</groupId>
      			<artifactId>slf4j-api</artifactId>
      			<version>${org.slf4j-version}</version>
      		</dependency>
      		<dependency>
      			<groupId>org.slf4j</groupId>
      			<artifactId>jcl-over-slf4j</artifactId>
      			<version>${org.slf4j-version}</version>
      			<scope>runtime</scope>
      		</dependency>
      		<dependency>
      			<groupId>org.slf4j</groupId>
      			<artifactId>slf4j-log4j12</artifactId>
      			<version>${org.slf4j-version}</version>
      			<scope>runtime</scope>
      		</dependency>
      		<dependency>
      			<groupId>log4j</groupId>
      			<artifactId>log4j</artifactId>
      			<version>1.2.15</version>
      			<exclusions>
      				<exclusion>
      					<groupId>javax.mail</groupId>
      					<artifactId>mail</artifactId>
      				</exclusion>
      				<exclusion>
      					<groupId>javax.jms</groupId>
      					<artifactId>jms</artifactId>
      				</exclusion>
      				<exclusion>
      					<groupId>com.sun.jdmk</groupId>
      					<artifactId>jmxtools</artifactId>
      				</exclusion>
      				<exclusion>
      					<groupId>com.sun.jmx</groupId>
      					<artifactId>jmxri</artifactId>
      				</exclusion>
      			</exclusions>
      			<scope>runtime</scope>
      		</dependency>
      
      		<!-- @Inject -->
      		<dependency>
      			<groupId>javax.inject</groupId>
      			<artifactId>javax.inject</artifactId>
      			<version>1</version>
      		</dependency>
      
      		<dependency>
      			<groupId>commons-pool</groupId>
      			<artifactId>commons-pool</artifactId>
      			<version>20030825.183949</version>
      		</dependency>
      
      		<!-- Servlet -->
      		<dependency>
      			<groupId>javax.servlet</groupId>
      			<artifactId>servlet-api</artifactId>
      			<version>2.5</version>
      			<scope>provided</scope>
      		</dependency>
      		<dependency>
      			<groupId>javax.servlet.jsp</groupId>
      			<artifactId>jsp-api</artifactId>
      			<version>2.1</version>
      			<scope>provided</scope>
      		</dependency>
      		<dependency>
      			<groupId>javax.servlet</groupId>
      			<artifactId>jstl</artifactId>
      			<version>1.2</version>
      		</dependency>
      
      		<!-- Test -->
      		<dependency>
      			<groupId>junit</groupId>
      			<artifactId>junit</artifactId>
      			<version>4.7</version>
      			<scope>test</scope>
      		</dependency>
      	</dependencies>
      	<build>
      		<plugins>
      			<plugin>
      				<artifactId>maven-eclipse-plugin</artifactId>
      				<version>2.9</version>
      				<configuration>
      					<additionalProjectnatures>
      						<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
      					</additionalProjectnatures>
      					<additionalBuildcommands>
      						<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
      					</additionalBuildcommands>
      					<downloadSources>true</downloadSources>
      					<downloadJavadocs>true</downloadJavadocs>
      				</configuration>
      			</plugin>
      			<plugin>
      				<groupId>org.apache.maven.plugins</groupId>
      				<artifactId>maven-compiler-plugin</artifactId>
      				<version>2.3.2</version>
      				<configuration>
      					<source>1.6</source>
      					<target>1.6</target>
      					<compilerArgument>-Xlint:all</compilerArgument>
      					<showWarnings>true</showWarnings>
      					<showDeprecation>true</showDeprecation>
      				</configuration>
      			</plugin>
      			<plugin>
      				<groupId>org.codehaus.mojo</groupId>
      				<artifactId>exec-maven-plugin</artifactId>
      				<version>1.2.1</version>
      				<configuration>
      					<mainClass>org.test.int1.Main</mainClass>
      				</configuration>
      			</plugin>
      		</plugins>
      		<pluginManagement>
      			<plugins>
      				<!--This plugin's configuration is used to store Eclipse m2e settings 
      					only. It has no influence on the Maven build itself. -->
      				<plugin>
      					<groupId>org.eclipse.m2e</groupId>
      					<artifactId>lifecycle-mapping</artifactId>
      					<version>1.0.0</version>
      					<configuration>
      						<lifecycleMappingMetadata>
      							<pluginExecutions>
      								<pluginExecution>
      									<pluginExecutionFilter>
      										<groupId>
      											org.codehaus.mojo
      										</groupId>
      										<artifactId>
      											exec-maven-plugin
      										</artifactId>
      										<versionRange>
      											[1.2,)
      										</versionRange>
      										<goals>
      											<goal>java</goal>
      										</goals>
      									</pluginExecutionFilter>
      									<action>
      										<ignore></ignore>
      									</action>
      								</pluginExecution>
      							</pluginExecutions>
      						</lifecycleMappingMetadata>
      					</configuration>
      				</plugin>
      			</plugins>
      		</pluginManagement>
      	</build>
      </project>
      Thanks
      Last edited by zed420; Jul 10th, 2012, 02:17 PM.

      Comment


      • #4
        For starters fix your hibernate setup... You are using hibernate3 in your configuration but are including hibernate4 in your project...

        Comment


        • #5
          Thanks for pointing it out Marten this is what I have now.

          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans:beans xmlns="http://www.springframework.org/schema/mvc"
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
          	xmlns:context="http://www.springframework.org/schema/context"
          	xsi:schemaLocation="http://www.springframework.org/schema/beans 
          	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
          	http://www.springframework.org/schema/mvc 
          	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          	http://www.springframework.org/schema/context 
          	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
          
          	<!-- DispatcherServlet Context: defines this servlet's request-processing 
          		infrastructure -->
          
          	<!-- Enables the Spring MVC @Controller programming model -->
          	<annotation-driven />
          
          	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
          		up static resources in the ${webappRoot}/resources directory -->
          	<resources mapping="/resources/**" location="/resources/" />
          
          	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
          		in the /WEB-INF/views directory -->
          	<beans:bean
          		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          		<beans:property name="prefix" value="/WEB-INF/views/" />
          		<beans:property name="suffix" value=".jsp" />
          	</beans:bean>
          	<context:component-scan base-package="com.logicalideas.controller" />
          	<context:component-scan base-package="com.logicalideas.dao" />
          	<context:component-scan base-package="com.logicalideas.persistence" />
          	<context:component-scan base-package="com.logicalideas.service" />
          
          	<beans:bean id="sessionFactory"
          		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
          		<beans:property name="dataSource" ref="dataSource" />
          		<beans:property name="hibernateProperties">
          			<beans:props>
          				<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
          				</beans:prop>
          			</beans:props>
          		</beans:property>
          		<beans:property name="annotatedClasses">
          			<beans:list>
          				<beans:value> com.logicalideas.persistence.Contacts </beans:value>
          			</beans:list>
          		</beans:property>
          	</beans:bean>
          	<beans:bean id="transactionManager"
          		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
          		<beans:property name="sessionFactory" ref="sessionFactory" />
          	</beans:bean>
          </beans:beans>
          Just fined out the problems I have deleted the @Atuowired annotation from all three classes and this is the error I'm getting now.
          Code:
          Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
          Any suggestions ???

          Comment


          • #6
            I suggest you start to read your stacktraces...

            Code:
            Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'
            There is no bean named dataSource...

            Also removing @Autowired isn't going to solve your problem as you now basically get no injection and your SessionFactory is null, which in the end will lead to NullPointerExceptions.

            Comment


            • #7
              After replacing all the @Autowired annotation and creating a dataSource class this is what I get;
              Error:
              Code:
              org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.logicalideas.service.ContactService com.logicalideas.controller.ContactController.conService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.logicalideas.dao.ContactsDAO com.logicalideas.service.ContactServiceImp.contactInterface; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactsImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.logicalideas.dao.ContactsImp.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
              servlet-context.xml
              Code:
              <?xml version="1.0" encoding="UTF-8"?>
              <beans:beans xmlns="http://www.springframework.org/schema/mvc"
              	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
              	xmlns:context="http://www.springframework.org/schema/context"
              	xsi:schemaLocation="http://www.springframework.org/schema/beans 
              	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
              	http://www.springframework.org/schema/mvc 
              	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
              	http://www.springframework.org/schema/context 
              	http://www.springframework.org/schema/context/spring-context-3.0.xsd">
              
              	<!-- DispatcherServlet Context: defines this servlet's request-processing 
              		infrastructure -->
              
              	<!-- Enables the Spring MVC @Controller programming model -->
              	<annotation-driven />
              
              	<!-- Handles HTTP GET requests for /resources/** by efficiently serving 
              		up static resources in the ${webappRoot}/resources directory -->
              	<resources mapping="/resources/**" location="/resources/" />
              
              	<!-- Resolves views selected for rendering by @Controllers to .jsp resources 
              		in the /WEB-INF/views directory -->
              	<beans:bean
              		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              		<beans:property name="prefix" value="/WEB-INF/views/" />
              		<beans:property name="suffix" value=".jsp" />
              	</beans:bean>
              	<context:component-scan base-package="com.logicalideas.controller" />
              	<context:component-scan base-package="com.logicalideas.dao" />
              	<context:component-scan base-package="com.logicalideas.persistence" />
              	<context:component-scan base-package="com.logicalideas.service" />
              
              	<beans:bean id="sessionFactory"
              		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              		<beans:property name="dataSource" ref="dataSource" />
              		<beans:property name="hibernateProperties">
              			<beans:props>
              				<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
              			</beans:props>
              		</beans:property>
              		<beans:property name="annotatedClasses">
              			<beans:list>
              				<beans:value> com.logicalideas.persistence.Contacts </beans:value>
              			</beans:list>
              		</beans:property>
              	</beans:bean>
              	
              	<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              		<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
              		<beans:property name="url" value="jdbc:mysql://localhost:3306/test" />
              		<beans:property name="username" value="root" />
              		<beans:property name="password" value="password" />
              	</beans:bean>
              	
              	<beans:bean id="transactionManager"
              		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
              		<beans:property name="sessionFactory" ref="sessionFactory" />
              	</beans:bean>
              </beans:beans>
              Any thoughts

              Comment


              • #8
                I'm feeling I'm repeating myself over-and-over... Read the stack trace!!!!

                There is an error in your datasource configuration and the stack trace will tell you what is wrong with it (you only posted a partial stack trace so the actual part pointing to the solution isn't there). However my guess is the mysql jdbc driver isn't in your classpath...

                Comment


                • #9
                  Thank you Marten for being patient I've managed to get rid of most the errors apart from this one. I've been gooling it for a while it seems to be well known. I have tried most of the suggestions but as yet no joy. I've tried using Hibernate-core 3.6.10.Final that doesn't work either.

                  Code:
                  root cause
                  
                  org.hibernate.HibernateException: No Session found for current thread
                  	org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
                  	org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1039)
                  	com.logicalideas.dao.ContactsImp.currentSession(ContactsImp.java:23)
                  	com.logicalideas.dao.ContactsImp.addNewContacts(ContactsImp.java:34)
                  	com.logicalideas.service.ContactServiceImp.addNewContacts(ContactServiceImp.java:18)
                  	com.logicalideas.controller.ContactController.proccssForm(ContactController.java:31)
                  	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                  	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                  	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                  	java.lang.reflect.Method.invoke(Method.java:597)
                  	org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
                  	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
                  	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
                  	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
                  	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
                  	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
                  	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
                  	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
                  	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
                  	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
                  	javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
                  	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
                  Any input ???

                  Many Thanks

                  Comment


                  • #10
                    You have nothing that drives the transactions. You have the infrastructure ready (the transaction manager) but haven't specified where/when to start/stop a transaction. I suggest a read of the reference guide on how to do declarative transaction management.

                    Comment


                    • #11
                      Did you mean this line,
                      Code:
                      <tx:annotation-driven transaction-manager="transactionManager"/>
                      Much appreciated Marten for all your help. I'm cooking on Gas now lol.
                      It's working

                      Thank you

                      Comment

                      Working...
                      X