在Spring中使用@Resource和@Service注释时出错

时间:2011-05-16 04:30:48

标签: java spring

我一直试图在Spring中使用@Resource和@Service注释,如下所示。

这是我的服务类

@Service("personService")
public class PersonService {
    protected static Logger logger = Logger.getLogger("PersonService");
    // service methods here
}

我的控制器课程在这里

@Controller
public class PersonController {

    protected static Logger logger = Logger.getLogger("controller");

    @Resource(name="personService")
    private PersonService personService;

    // controller methods
}

当我在jboss服务器上部署此应用程序时,我收到以下错误。

javax.ejb.EJBException: java.lang.ClassNotFoundException: com.demoApp.service.PersonService from BaseClassLoader@1612c8f{VFSClassLoaderPolicy@b49448{name=vfsfile:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/ domain=ClassLoaderDomain@d5f9b9{name=DefaultDomain parentPolicy=BEFORE parent=org.jboss.system.NoAnnotationURLClassLoader@11a9310} roots=[MemoryContextHandler@24324950[path= context=vfsmemory://5c4o066-473awx-gnqvtutg-1-gnqvu8rq-x real=vfsmemory://5c4o066-473awx-gnqvtutg-1-gnqvu8rq-x], FileHandler@2241655[path=jbossweb.sar context=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/ real=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/], DelegatingHandler@27422898[path=jbossweb.sar/jasper-jdt.jar context=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/ real=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jasper-jdt.jar], DelegatingHandler@26532309[path=jbossweb.sar/jboss-web-service.jar context=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/ real=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jboss-web-service.jar], DelegatingHandler@23676237[path=jbossweb.sar/jbossweb.jar context=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/ real=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jbossweb.jar], DelegatingHandler@4513709[path=jbossweb.sar/jstl.jar context=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/ real=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jstl.jar], DelegatingHandler@28896918[path=jbossweb.sar/jsf-libs/jboss-faces.jar context=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/ real=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jsf-libs/jboss-faces.jar], DelegatingHandler@3657798[path=jbossweb.sar/jsf-libs/jsf-api.jar context=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/ real=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jsf-libs/jsf-api.jar], DelegatingHandler@4907609[path=jbossweb.sar/jsf-libs/jsf-impl.jar context=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/ real=file:/E:/servers/demoAppserver/jboss-5.0.0.GA/server/default/deploy/jbossweb.sar/jsf-libs/jsf-impl.jar]]  delegates=null exported=[, org.apache.tomcat.util.http.fileupload, com.sun.faces.mgbean, org.apache.taglibs.standard, org.apache.tomcat.util.net, org.apache.catalina.core, org.jboss.web.tomcat.security, org.apache.jasper.resources, org.jboss.web.tomcat.security.jaspi.modules, org.apache.tomcat.util.http, com.sun.faces, javax.faces.validator, org.apache.jasper.xmlparser, org.apache.catalina.util, org.apache.tomcat.util.log, org.eclipse.jdt.internal.compiler.classfmt, org.apache.catalina.ant.jmx, org.eclipse.jdt.internal.compiler.codegen, javax.faces.application, org.apache.catalina.startup, com.sun.faces.el, org.jboss.web.tomcat.service.jasper, org.apache.tomcat.util.modeler.modules, org.apache.taglibs.standard.tag.rt.core, org.apache.taglibs.standard.tag.common.core, org.apache.catalina.manager.host, org.jboss.web.jsf.integration.config, org.apache.taglibs.standard.tag.common.sql, com.sun.faces.lifecycle, org.apache.jasper.el, org.apache.naming.java, javax.servlet.jsp.jstl.core, com.sun.faces.config, org.jboss.web.tomcat.service.jca, org.jboss.web.cluster.advertise, org.apache.tomcat.util.net.jsse, org.apache.taglibs.standard.tag.common.fmt, org.apache.coyote.memory, org.apache.jasper.util, org.apache.taglibs.standard.lang.support, com.sun.faces.taglib.jsf_core, org.apache.taglibs.standard.tag.rt.sql, org.apache.catalina, com.sun.faces.ext.validator, org.jboss.web.jsf.integration.serialization, org.apache.tomcat.util.json, org.eclipse.jdt.core.compiler, org.apache.catalina.ssi, org.apache.jasper.servlet, javax.servlet.jsp.jstl.sql, javax.faces.lifecycle, org.apache.taglibs.standard.tag.el.core, org.apache.taglibs.standard.lang.jstl.test, org.jboss.net.protocol.jndi, org.apache.taglibs.standard.lang.jstl.test.beans, org.eclipse.jdt.internal.compiler.flow, org.apache.naming.resources, com.sun.faces.vendor, com.sun.faces.taglib.html_basic, javax.faces.webapp, org.apache.coyote.http11.filters, javax.faces.convert, org.apache.tomcat.bayeux, org.apache.el.parser, com.sun.faces.spi, com.sun.faces.ext.component, org.apache.taglibs.standard.lang.jstl.parser, org.apache.catalina.mbeans, org.apache.catalina.security, javax.faces.render, com.sun.faces.config.configprovider, org.apache.taglibs.standard.tei, org.apache.catalina.servlets, org.apache.taglibs.standard.resources, org.apache.taglibs.standard.tag.el.sql, org.jboss.web.tomcat.statistics, org.apache.naming.factory.webservices, javax.faces.event, org.eclipse.jdt.internal.core.util, org.eclipse.jdt.internal.compiler.parser.diagnose, org.jboss.web.cluster, com.sun.faces.ext.render, META-INF.services, org.apache.jasper.security, com.sun.faces.renderkit.html_basic, org.jboss.web.jsf.integration.injection, com.sun.faces.resources, org.eclipse.jdt.internal.compiler.ast, org.apache.taglibs.standard.extra.spath, org.apache.naming, org.apache.tomcat.util.http.mapper, org.jboss.web.tomcat.service.sso, org.apache.taglibs.standard.tlv, org.apache.catalina.manager, org.jboss.web.rewrite, org.apache.tomcat.util.http.res, com.sun.faces.io, org.apache.taglibs.standard.functions, javax.servlet.jsp.jstl.fmt, org.jboss.web.tomcat.service.deployers, org.apache.jasper.tagplugins.jstl, org.apache.taglibs.standard.tag.rt.fmt, org.apache.el.lang, org.apache.tomcat.util.res, javax.faces, org.apache.tomcat.util.buf.res, org.apache, javax.faces.component, org.apache.tomcat.util.digester, javax.faces.el, org.apache.taglibs.standard.lang.jstl, org.apache.catalina.manager.util, org.apache.tomcat.util, org.apache.naming.resources.jndi, org.jboss.web.tomcat.service, org.jboss.web.tomcat.filters, org.jboss.web.tomcat.security.jaspi, org.apache.catalina.loader, org.jboss.servlet.http, org.eclipse.jdt.internal.compiler.env, org.apache.jasper.compiler, org.eclipse.jdt.internal.compiler.lookup, org.eclipse.jdt.internal.compiler.util, com.sun.faces.application, org.apache.taglibs.standard.tag.rt.xml, org.jboss.web.tomcat.service.session.notification, org.jboss.web.tomcat.security.authenticators, org.apache.coyote, org.apache.catalina.authenticator, org.eclipse.jdt.internal.compiler.problem, javax.faces.component.html, org.apache.el.util, com.sun.faces.config.processor, com.sun.faces.taglib, com.sun.faces.ext.taglib, org.apache.catalina.connector, org.apache.catalina.realm, org.apache.jasper.runtime, com.sun.faces.context, org.apache.cometd.bayeux, org.apache.jasper.tagplugins.jstl.core, javax.faces.context, org.apache.taglibs.standard.tag.el.fmt, org.jboss.web.tomcat.security.login, org.jboss.net.ssl, org.apache.jasper, org.apache.coyote.http11, org.jboss.web.php, org.apache.catalina.ant, org.eclipse.jdt.internal.compiler, org.apache.tomcat.bayeux.request, com.sun.faces.scripting, org.apache.tomcat.util.buf, org.eclipse.jdt.internal.compiler.batch, org.eclipse.jdt.core.compiler.batch, org.apache.tomcat.util.modeler, org.jboss.web.tomcat.servlets, org.apache.jasper.compiler.tagplugin, org.apache.catalina.session, org.jboss.web.tomcat.service.injection, com.sun.faces.renderkit, org.apache.taglibs.standard.tag.common.xml, javax.servlet.jsp.jstl.tlv, org.apache.catalina.deploy, org.apache.catalina.valves, org.apache.tomcat.util.net.jsse.res, javax.faces.model, com.sun.faces.util, org.apache.taglibs.standard.tag.el.xml, META-INF, org.apache.tomcat.jni, org.apache.coyote.ajp, org.jboss.web.tomcat.service.session, org.apache.tomcat.util.net.res, org.apache.naming.factory, org.jboss.web.tomcat.metadata, org.eclipse.jdt.internal.compiler.impl, org.eclipse.jdt.internal.compiler.parser, org.apache.el] <IMPORT-ALL>NON_EMPTY}}
at org.jboss.web.tomcat.service.injection.WebResourceHandler.loadXmlResourceEnvRefs(WebResourceHandler.java:267)
at org.jboss.web.tomcat.service.injection.WebResourceHandler.loadXml(WebResourceHandler.java:307)
at org.jboss.web.tomcat.service.TomcatInjectionContainer.processMetadata(TomcatInjectionContainer.java:544)

当我不使用这些注释时,应用程序可以正常运行。

2 个答案:

答案 0 :(得分:1)

是否与此有关 应用程序上下文中的<context:annotation-config/><context:component-scan> xml?

答案 1 :(得分:1)

异常是java.lang.ClassNotFoundException: com.demoApp.service.PersonService这意味着问题不在于注入不起作用,而是因为找不到PersonService类。

您应该检查它是否正确部署(类和注释)。并且您使用了正确的注释(例如,导入了正确的Service类,而不是像org.my.strange.project.Service那样)