Spring和gwt(requestFactory)的NoSuchBeanDefinitionException

时间:2012-08-15 18:10:30

标签: spring gwt requestfactory

我使用gwt(使用requestfactory)和spring

得到此错误
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.calibra.server.service.AccountService] is defined: expected single bean but found 0: 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)
at org.calibra.server.SpringServiceLocator.getInstance(SpringServiceLocator.java:24)
at com.google.web.bindery.requestfactory.server.LocatorServiceLayer.createServiceInstance(LocatorServiceLayer.java:56)

我的服务定位器

public class SpringServiceLocator implements ServiceLocator {
    @Override
    public Object getInstance(Class<?> clazz) {
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(                
            RequestFactoryServlet.getThreadLocalServletContext());
        return context.getBean(clazz);
    }
}

我的春季服务

@Service
public class AccountServiceImpl implements AccountService{
     @Override
     public void addNewAccount(Account account) {
       ...
     }

     @Override
     public List<Account> loadAllAccounts() {
        ...
     }

}

Gwt requestContext,引用我的spring服务

@Service(value=AccountService.class, locator=SpringServiceLocator.class)
public interface AccountRequest extends RequestContext {

    Request<Void> addNewAccount(AccountProxy account);

    Request<List<AccountProxy>> loadAllAccounts();

}

我的web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>gwtRequest</servlet-name>
    <servlet-class>com.google.web.bindery.requestfactory.server.RequestFactoryServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>gwtRequest</servlet-name>
    <url-pattern>/gwtRequest</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>welcomeGWT.html</welcome-file>
</welcome-file-list>

我不明白我如何拥有0个AccountService bean?

我试图添加dispatcher-servlet

<bean id="accountService" class="org.calibra.server.service.AccountServiceImpl"/>

我得到了相同的结果

有什么想法吗?

编辑:如果有人有完整的完整示例,那可能会有用。

2 个答案:

答案 0 :(得分:0)

我认为单独使用ContextLoaderListener是不够的,因为您似乎没有使用DispatcherServlet(对吗?)。 以下几行对我有用:

<filter>
    <filter-name>springRequestContextFilter</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>springRequestContextFilter</filter-name>
    <url-pattern>/gwtRequest</url-pattern>
</filter-mapping>

答案 1 :(得分:0)

我在其他几个地方看过这个问题。你应该首先尝试明确地将AccountServiceImpl定义为applicationContext.xml中的bean(而不是dispatch-servlet.xml),看看你是否仍然得到错误,如果你没有,那么你就知道你错过了组件-scan在您的应用程序上下文xml中,我认为是这种情况。

希望这会有所帮助