java - How to import springConfig.xml into web xml -
i create spring mvc eclipse project direct spring mvc template in eclipse. use mybatis api dao crud. when test crud mybatis crud using junit there not error. when try use real controller there error. think maybe because don't link springconfig.xml , web.xml load configuration. can show me how.?
below content of file : springconfig.xml located in javaresource folder
error: org.springframework.web.servlet.dispatcherservlet - context initialization failed org.springframework.beans.factory.beancreationexception: error creating bean name 'homecontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire method: public void kh.com.gfam.homecontroller.setdao(kh.com.gfam.rsos.dataaccess.dao.hotelenvironmentdao); nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [kh.com.gfam.rsos.dataaccess.dao.hotelenvironmentdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:292) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1185) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:537) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:475) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:304) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:228) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:300) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:195) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:700) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:760) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:482) @ org.springframework.web.servlet.frameworkservlet.configureandrefreshwebapplicationcontext(frameworkservlet.java:658) @ org.springframework.web.servlet.frameworkservlet.createwebapplicationcontext(frameworkservlet.java:624) @ org.springframework.web.servlet.frameworkservlet.createwebapplicationcontext(frameworkservlet.java:672) @ org.springframework.web.servlet.frameworkservlet.initwebapplicationcontext(frameworkservlet.java:543) @ org.springframework.web.servlet.frameworkservlet.initservletbean(frameworkservlet.java:484) @ org.springframework.web.servlet.httpservletbean.init(httpservletbean.java:136) @ javax.servlet.genericservlet.init(genericservlet.java:158) @ org.apache.catalina.core.standardwrapper.initservlet(standardwrapper.java:1241) @ org.apache.catalina.core.standardwrapper.loadservlet(standardwrapper.java:1154) @ org.apache.catalina.core.standardwrapper.allocate(standardwrapper.java:827) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:135) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:505) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:610) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:534) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1081) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:658) @ org.apache.coyote.http11.http11nioprotocol$http11connectionhandler.process(http11nioprotocol.java:222) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1566) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1523) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745) caused by: org.springframework.beans.factory.beancreationexception: not autowire method: public void kh.com.gfam.homecontroller.setdao(kh.com.gfam.rsos.dataaccess.dao.hotelenvironmentdao); nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [kh.com.gfam.rsos.dataaccess.dao.hotelenvironmentdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {} @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredmethodelement.inject(autowiredannotationbeanpostprocessor.java:596) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:87) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:289) ... 37 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [kh.com.gfam.rsos.dataaccess.dao.hotelenvironmentdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {} @ org.springframework.beans.factory.support.defaultlistablebeanfactory.raisenosuchbeandefinitionexception(defaultlistablebeanfactory.java:1100) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:960) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:855) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredmethodelement.inject(autowiredannotationbeanpostprocessor.java:553) ... 39 more
web.xml location int webint directory
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- definition of root spring container shared servlets , filters --> <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring/root-context.xml</param-value> </context-param> <!-- creates spring container shared servlets , filters --> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- processes application requests --> <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/spring/appservlet/servlet-context.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> <!-- style --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <!-- script --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <!-- image --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> </web-app>
my directory structure
here homecontroller.java try use
package kh.com.gfam; import java.text.dateformat; import java.util.date; import java.util.locale; import kh.com.gfam.rsos.dataaccess.dao.hotelenvironmentdao; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; /** * handles requests application home page. */ @controller public class homecontroller { private static final logger logger = loggerfactory.getlogger(homecontroller.class); private hotelenvironmentdao h = null ; @autowired public void setdao(hotelenvironmentdao h){ this.h = h; } /** * selects home view render returning name. */ @requestmapping(value = "/", method = requestmethod.get) public string home(locale locale, model model) { logger.info("welcome home! client locale {}.", locale); date date = new date(); dateformat dateformat = dateformat.getdatetimeinstance(dateformat.long, dateformat.long, locale); string formatteddate = dateformat.format(date); h.getcount(); model.addattribute("servertime", formatteddate ); return "home"; } }
there no problem in configuration springconfig.xml
under web.xml
. there problem in code auto wiring in class homecontroller
, , hence error:
org.springframework.beans.factory.beancreationexception: error creating bean name 'homecontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire method: public void kh.com.gfam.homecontroller.setdao(kh.com.gfam.rsos.dataaccess.dao.hotelenvironmentdao); nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [kh.com.gfam.rsos.dataaccess.dao.hotelenvironmentdao] found dependency: expected @ least 1 bean qualifies autowire candidate dependency. dependency annotations: {}
edit: seems related name of wired bean:
private hotelenvironmentdao h = null ;
and setter method:
@autowired public void setdao(hotelenvironmentdao h){ this.h = h; }
setter name should either seth.
Comments
Post a Comment