Spring Rest CXF [bean 错误] Tomcat

发布于 2024-10-31 17:13:19 字数 1872 浏览 11 评论 0原文

我正在rest、spring、cxf和tomcat中做一个网络服务。

链接完整项目: http://www55.zippyshare.com/v/99585767/file.html

我在 bean 上遇到了这个错误。 无法弄清楚为什么会发生这种情况?

严重:上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名称为“restContainer”的bean时出错:使用键[0]设置bean属性“serviceBeans”时无法解析对bean“timeService”的引用;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“timeService”的bean
在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)

timeService.java

@Service("timeService")  
@Path("/time")  
public class TimeService {

    @GET
    @Produces("text/plain")
    public String getDateTime()
    {
        DateFormatter formatter = new DateFormatter("dd/MM/yyyy hh:mm:ss");
        return formatter.print(Calendar.getInstance().getTime(), Locale.getDefault());
    }
}

beans.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">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxrs:server id="restContainer" address="/">
        <jaxrs:serviceBeans>
            <ref bean="timeService"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>

</beans>

这些是我的文件,我无法找出问题所在。这让我抓狂!

I´m doing a webservice in rest, spring, cxf and tomcat.

Link full project: http://www55.zippyshare.com/v/99585767/file.html

I´ve got this error on bean.
Can´t figure out why is this happening?

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restContainer': Cannot resolve reference to bean 'timeService' while setting bean property 'serviceBeans' with key [0]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'timeService' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)

timeService.java

@Service("timeService")  
@Path("/time")  
public class TimeService {

    @GET
    @Produces("text/plain")
    public String getDateTime()
    {
        DateFormatter formatter = new DateFormatter("dd/MM/yyyy hh:mm:ss");
        return formatter.print(Calendar.getInstance().getTime(), Locale.getDefault());
    }
}

beans.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">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxrs:server id="restContainer" address="/">
        <jaxrs:serviceBeans>
            <ref bean="timeService"/>
        </jaxrs:serviceBeans>
    </jaxrs:server>

</beans>

These are my files and i can´t find out what is wrong. This is driving me nuts!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情魔剑神 2024-11-07 17:13:19

Spring 文档 说您需要添加一个元素来指导查找带有 @Service 注释的 bean。例如,如果您的 bean 位于包 org.example 或其子包之一中,则您可以在 beans.xml 中使用组件扫描器配置,如下所示:(

<context:component-scan base-package="org.example"/>

只要它位于 元素,无论它位于 元素之上还是之下都可以。)

The Spring documentation says that you need to add an element to direct the finding of your @Service-annotated beans. For example, if your beans were in the package org.example or one of its sub-packages, you'd use a component scanner configuration in your beans.xml like this:

<context:component-scan base-package="org.example"/>

(As long as it's inside the <beans> element, it's fine whether it goes above or below the <jaxrs:server> element.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文