Spring @Autowired Not Working Error Creating Bean Injection Of Autowire dependencies failed -
i use spring , have lot of trouble using @autowired
annotation. upon compiling throws these errors edit using regular java classes write classes. not causing problem, it?
aug 12, 2013 2:40:32 pm org.apache.catalina.core.applicationcontext log info: no spring webapplicationinitializer types detected on classpath aug 12, 2013 2:40:32 pm org.apache.catalina.core.applicationcontext log info: initializing spring root webapplicationcontext aug 12, 2013 2:40:33 pm org.apache.catalina.core.standardcontext listenerstart severe: exception sending context initialized event listener instance of class org.springframework.web.context.contextloaderlistener org.springframework.beans.factory.beancreationexception: error creating bean name 'homecontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: com.mycompany.smsdatabase.service.personimport com.mycompany.smsdatabase.controller.homecontroller.personimport; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [com.mycompany.smsdatabase.service.personimport] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:287) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1106) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:517) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:456) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:294) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:225) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:291) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:193) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:585) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:913) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:464) @ org.springframework.web.context.contextloader.configureandrefreshwebapplicationcontext(contextloader.java:384) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:283) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:111) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4887) @ org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5381) @ org.apache.catalina.util.lifecyclebase.start(lifecyclebase.java:150) @ org.apache.catalina.core.containerbase.addchildinternal(containerbase.java:901) @ org.apache.catalina.core.containerbase.addchild(containerbase.java:877) @ org.apache.catalina.core.standardhost.addchild(standardhost.java:633) @ org.apache.catalina.startup.hostconfig.deploydescriptor(hostconfig.java:657) @ org.apache.catalina.startup.hostconfig.deployapps(hostconfig.java:536) @ org.apache.catalina.startup.hostconfig.check(hostconfig.java:1462) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:601) @ org.apache.tomcat.util.modeler.basemodelmbean.invoke(basemodelmbean.java:301) @ com.sun.jmx.interceptor.defaultmbeanserverinterceptor.invoke(defaultmbeanserverinterceptor.java:819) @ com.sun.jmx.mbeanserver.jmxmbeanserver.invoke(jmxmbeanserver.java:791) @ org.apache.catalina.manager.managerservlet.check(managerservlet.java:1445) @ org.apache.catalina.manager.managerservlet.deploy(managerservlet.java:860) @ org.apache.catalina.manager.managerservlet.doget(managerservlet.java:357) @ javax.servlet.http.httpservlet.service(httpservlet.java:621) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.filters.setcharacterencodingfilter.dofilter(setcharacterencodingfilter.java:108) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:581) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:99) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:953) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1008) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:589) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:312) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1110) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:603) @ java.lang.thread.run(thread.java:722) caused by: org.springframework.beans.factory.beancreationexception: not autowire field: com.mycompany.smsdatabase.service.personimport com.mycompany.smsdatabase.controller.homecontroller.personimport; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [com.mycompany.smsdatabase.service.personimport] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:506) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:284) ... 53 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no matching bean of type [com.mycompany.smsdatabase.service.personimport] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:924) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:793) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:707) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:478) ... 55 more aug 12, 2013 2:40:33 pm org.apache.catalina.core.applicationcontext log info: closing spring root webapplicationcontext
homecontroller.java
package com.mycompany.smsdatabase.controller; import com.mycompany.smsdatabase.domain.person; import com.mycompany.smsdatabase.repositories.personrepository; import com.mycompany.smsdatabase.service.personimport; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.support.abstractapplicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; @controller public class homecontroller { @autowired personimport personimport; @requestmapping(value = "/", method = requestmethod.get) public string home() { return "index"; } }
personimport.java
package com.mycompany.smsdatabase.service; import com.mycompany.smsdatabase.domain.person; import com.mycompany.smsdatabase.repositories.personrepository; import javax.transaction.transaction; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; import org.springframework.web.bind.annotation.pathvariable; @service public class personimport { @autowired private personrepository personrepository; public person doimportperson( string id) { person person = personrepository.findbyid(id); if (person == null) { person = new person(id,"anthony"); } personrepository.save(person); system.out.println("+++++++++++++++++++++++++++++++++++++hello+++++++++++++"); return person; } }
personrepository.java
import com.mycompany.smsdatabase.domain.person; import org.springframework.data.neo4j.core.graphdatabase; import org.springframework.data.neo4j.repository.graphrepository; import org.springframework.data.neo4j.repository.namedindexrepository; public interface personrepository extends graphrepository<person>, namedindexrepository<person>{ person findbyid(string id); }
person.java
package com.mycompany.smsdatabase.domain; import java.util.date; import org.springframework.data.neo4j.annotation.graphid; import org.springframework.data.neo4j.annotation.indexed; import org.springframework.data.neo4j.annotation.nodeentity; import org.springframework.data.neo4j.support.index.indextype; @nodeentity public class person { @graphid long nodeid; @indexed(unique=true) string id; @indexed(indextype=indextype.fulltext, indexname = "people") string name; private date birthday; public person(string id, string name) { this.id = id; this.name = name; } public string getid() { return id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public void setbirthday(date birthday) { this.birthday = birthday; } }
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>contextclass</param-name> <param-value>org.springframework.web.context.support.annotationconfigwebapplicationcontext</param-value> </context-param> <!-- location of java @configuration classes configure components makeup application --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>com.mycompany.smsdatabase.controller /web-inf/applicationcontext.xml </param-value> </context-param> <!-- creates spring container shared servlets , filters --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- ensure utf-8 character encoding used --> <filter> <filter-name>encodingfilter</filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceencoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- enables support delete , put request methods web browser clients --> <filter> <filter-name>hiddenhttpmethodfilter</filter-name> <filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</filter-class> </filter> <filter-mapping> <filter-name>hiddenhttpmethodfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>appservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/dispatcherservlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet> <servlet-name>h2console</servlet-name> <servlet-class>org.h2.server.web.webservlet</servlet-class> <init-param> <param-name>-weballowothers</param-name> <param-value>true</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>h2console</servlet-name> <url-pattern>/admin/h2/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app>
dispatchservlet.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.mycompany.smsdatabase" /> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix"> <value>/web-inf/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
application context.xml
<?xml version="1.0" encoding="utf-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:neo4j="http://www.springframework.org/schema/data/neo4j" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:annotation-config/> <context:component-scan base-package="com.mycompany.smsdatabase"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller"/> </context:component-scan> <context:spring-configured/> <bean class="org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor"/> <neo4j:config storedirectory="target/data/graph.db"/> <neo4j:repositories base-package="com.mycompany.smsdatabase.repositories"/> <bean id="graphdatabaseservice" class="org.neo4j.test.impermanentgraphdatabase" destroy-method="shutdown"/> <tx:annotation-driven mode="proxy"/> </beans>
i had same problem , found solution.
the problem class proxied (either @transactional or similar). spring cannot tell proxy instance of class, proxied interface.
i've found 2 solutions:
- create
interface personimport
,@autowire
instead of class. - use:
proxy-target-class="true"
in<tx:annotation-driven>
Comments
Post a Comment