basic spring 3 hibernate 4 mvc web application with many exceptions -
status: solved
. nevermind. found problem. entitymanager should not have been autowired in dao. effort.
i have simple newly created web project spring 3.2.3 , hibernate 4.x.x. maven in netbeans 7.3. getting following exception stated @ end following code.
does 1 had similar problem or has idea why happening.
controller
/* * change template, choose tools | templates * , open template in editor. */ package web.controller; import org.springframework.stereotype.controller; import org.springframework.ui.modelmap; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import web.entity.users; import web.service.userservice; /** * * @author syncsys */ @controller public class usercontroller { @autowired private userservice userservice; // @requestmapping(value = "/create", method = requestmethod.get ) @requestmapping(value = "/create" ) public string creatuser(modelmap model){ users user = new users(); user.setemail("myemail@mydomain.com"); user.setname("myname"); userservice.saveorupdate(user); //////// line 29 system.out.println("created--------------------"); return "create"; }
dao
package web.dao.impl.jpa; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import org.hibernate.session; import org.springframework.stereotype.repository; import web.dao.userdao; import web.entity.users; /** * * @version $revision$ * @since 1.0 */ @repository public class userdaoimpl implements userdao { /** * jpa entity manager */ @autowired private entitymanager entitymanager; /** * set entity manager * * @param entitymanager */ @persistencecontext public void setentitymanager(entitymanager entitymanager) { this.entitymanager = entitymanager; } /** * helper method return hibernate session jpa * entity manager implementation. * * @return hibernate {#link session} */ protected session gethibernatesession() { return entitymanager.unwrap(session.class); } /** * saves or updates existing user entity instance. * * @param user user entity * @return managed user entity instance */ public void saveorupdate(users user) { if(user.getid() == 0) { entitymanager.persist(user); } else entitymanager.merge(user); }
service
package web.service; /** * * @author syncsys */ import web.dao.userdao; import web.entity.users; @service @transactional(readonly = true) public class userserviceimpl implements userservice{ @autowired private userdao userdao; @transactional(readonly = false) public void saveorupdate(users user){ userdao.saveorupdate(user); } }
dispacther-servlet.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" > <context:component-scan base-package="web" > <context:include-filter type="annotation" expression="org.springframework.stereotype.service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.repository" /> </context:component-scan> <mvc:annotation-driven /> <!-- <context:annotation-config />--> <bean class="org.springframework.web.servlet.mvc.support.controllerclassnamehandlermapping"/> <!-- controllers use controllerclassnamehandlermapping above, index controller using parameterizableviewcontroller, must define explicit mapping it. --> <bean id="urlmapping" class="org.springframework.web.servlet.handler.simpleurlhandlermapping"> <property name="mappings"> <props> <prop key="index.htm">indexcontroller</prop> </props> </property> </bean> <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix" value="/web-inf/view/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- index controller. --> <bean name="indexcontroller" class="org.springframework.web.servlet.mvc.parameterizableviewcontroller"> <property name="viewname" value="index" /> </bean> <!--- ########################################################################## hibernate --> <!-- <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="org.postgresql.driver" /> <property name="url" value="${jdbc.url}jdbc:postgresql://localhost:5432/postgres" /> <property name="username" value="postgres" /> <property name="password" value="abc" /> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource"/> <property name="packagestoscan" value="web.entity" /> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.postgresqldialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="cache.provider_class">org.hibernate.cache.internal.nocacheprovider</prop> </props> </property> </bean> <tx:annotation-driven transaction-manager="transactionmanager"/> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory"/> </bean> ########################################################### --> <!-- jpa based instead of hibernate --> <bean class="org.springframework.orm.jpa.support.persistenceannotationbeanpostprocessor"/> <!-- configures entitymanagerfactory object used jpa/spring managed persistent objects. --> <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"> <property name="persistencexmllocation" value="classpath*:meta-inf/persistence.xml" /> <property name="persistenceunitname" value="persistence-unit-demo" /> <property name="datasource" ref="datasource" /> <property name="packagestoscan" value="web.entity" /> <property name="jpavendoradapter"> <bean class="org.springframework.orm.jpa.vendor.hibernatejpavendoradapter"> <property name="database" value="postgresql" /> <!-- giving errors <property name="databaseplatorm" value="org.hibernate.dialect.postgresqldialect"/>--> <!-- <property name="database" value="hsql" />--> <property name="showsql" value="true" /> <property name="generateddl" value="true" /> </bean> </property> <property name="jpaproperties"> <props> <prop key="hibernate.hbm2ddl.auto">create-drop</prop> </props> </property> </bean> <bean id="jpadialect" class="org.springframework.orm.jpa.vendor.hibernatejpadialect" /> <!-- pulls database connection tomcat container's context database pool via jndi --> <!-- <jee:jndi-lookup id="datasource" jndi-name="jdbc/mssqlserver" resource-ref="true"/>--> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource" > <property name="driverclassname" value="org.postgresql.driver" /> <property name="url" value="jdbc:postgresql://localhost:5432/postgres" /> <property name="username" value="postgres" /> <property name="password" value="abc" /> </bean> <!-- sets our transaction manager. --> <bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager"> <property name="entitymanagerfactory" ref="entitymanagerfactory" /> <property name="jpadialect" ref="jpadialect" /> <property name="datasource" ref="datasource" /> <!-- giving errors <property name="loadtimeweaver"> <bean class="org.springframework.instrument.classloading.instrumentationloadtimeweaver" /> </property>--> </bean> <!-- defines our transaction manager transactional annotations. --> <tx:annotation-driven transaction-manager="transactionmanager" /> <bean id="sessionfactory" factory-bean="entitymanagerfactory" factory-method="getsessionfactory" /> </beans>
maven pom.xml
<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/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.tm.spring</groupid> <artifactid>ttmaven</artifactid> <version>1.0-snapshot</version> <packaging>war</packaging> <name>ttmaven</name> <url>http://maven.apache.org</url> <properties> <spring.version>3.2.3.release</spring.version> <junit.version>4.11</junit.version> <jdk.version>1.6</jdk.version> </properties> <dependencies> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>4.2.3.final</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-entitymanager</artifactid> <version>4.2.3.final</version> </dependency> <dependency> <groupid>org.jumpmind.symmetric.jdbc</groupid> <artifactid>postgresql</artifactid> <version>9.2-1002-jdbc4</version> </dependency> <!-- java ee --> <dependency> <groupid>javax.servlet</groupid> <artifactid>jstl</artifactid> <version>1.1.1</version> <scope>runtime</scope> </dependency> <dependency> <groupid>taglibs</groupid> <artifactid>standard</artifactid> <scope>runtime</scope> <version>1.1.1</version> </dependency> <!-- spring 3 dependencies --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-web</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-orm</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalname>counterwebapp</finalname> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version>3.0</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project>
exceptions
description server encountered internal error prevented fulfilling request.
exception
http://pastebin.com/kzzvh5c9 exceptions long , stackoverflow character limit exceeded.
some main ones are
org.springframework.beans.factory.beancreationexception: error creating bean name 'usercontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private web.service.userservice web.controller.usercontroller.userservice; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'userserviceimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private web.dao.userdao web.service.userserviceimpl.userdao; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'userdaoimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private javax.persistence.entitymanager web.dao.impl.jpa.userdaoimpl.entitymanager; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanager] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} org.springframework.beans.factory.beancreationexception: not autowire field: private web.service.userservice web.controller.usercontroller.userservice; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'userserviceimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private web.dao.userdao web.service.userserviceimpl.userdao; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'userdaoimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private javax.persistence.entitymanager web.dao.impl.jpa.userdaoimpl.entitymanager; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanager] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} org.springframework.beans.factory.beancreationexception: error creating bean name 'userserviceimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private web.dao.userdao web.service.userserviceimpl.userdao; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'userdaoimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private javax.persistence.entitymanager web.dao.impl.jpa.userdaoimpl.entitymanager; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanager] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} org.springframework.beans.factory.beancreationexception: not autowire field: private web.dao.userdao web.service.userserviceimpl.userdao; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'userdaoimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private javax.persistence.entitymanager web.dao.impl.jpa.userdaoimpl.entitymanager; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanager] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} org.springframework.beans.factory.beancreationexception: error creating bean name 'userdaoimpl': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: private javax.persistence.entitymanager web.dao.impl.jpa.userdaoimpl.entitymanager; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanager] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} org.springframework.beans.factory.beancreationexception: not autowire field: private javax.persistence.entitymanager web.dao.impl.jpa.userdaoimpl.entitymanager; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanager] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [javax.persistence.entitymanager] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
seems didn't inject userservice controller.
@autowired private userservice userservice;
Comments
Post a Comment