我在rest,spring,cxf和tomcat中做了一个web服务。
链接完整项目:http://www55.zippyshare.com/v/99585767/file.html
我在bean上遇到了这个错误。 无法弄清楚为什么会这样?
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>
这些是我的文件,我无法找出问题所在。这让我疯了!
答案 0 :(得分:0)
Spring documentation表示您需要添加一个元素来指导查找@Service
- 带注释的bean。例如,如果您的bean位于包org.example
或其中一个子包中,那么您将在beans.xml中使用组件扫描程序配置,如下所示:
<context:component-scan base-package="org.example"/>
(只要它位于<beans>
元素内,无论它是高于还是低于<jaxrs:server>
元素,都可以。)