【问题标题】:NoSuchBeanDefinitionException with spring and gwt (requestFactory)NoSuchBeanDefinitionException 与 spring 和 gwt (requestFactory)
【发布时间】:2012-08-15 18:10:29
【问题描述】:

我用 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"/>

我得到了同样的结果

有什么想法吗?

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

【问题讨论】:

  • 您是否在 web.xml 中声明了 RequestFactory servlet?
  • 我有
    gwtRequestcom.google.web.bindery.requestfactory.server.RequestFactoryServlet-class>
  • 我不确定,但我认为您缺少 servlet 映射。请看这里:crazygui.wordpress.com/2011/09/23/…了解更多详情
  • 不好说,链接信息不多,web.xml也不可用
  • 您的 web.xml 文件似乎正确。抱歉,我无法确定确切的问题。当我有 noSuchBeanException 时,通常是因为 Spring 在应用程序的上下文中找不到他试图注入的 bean。您确定 bean 定义在您的应用程序上下文中吗?

标签: spring gwt requestfactory


【解决方案1】:

我认为仅使用 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>

【讨论】:

  • 不,我没有 DispatcherServlet,所以我的 dispatcher-servlet 肯定没有加载。我需要将此文件的内容移动到 applicationContext.xml 并查看是否可行...之后我会尝试您的解决方案
  • 我尝试将配置注释放在 applicationContext.xml 中但没有成功。我也尝试了您的解决方案,但没有成功。
  • 你能把你的web.xml、applicationContext.xml和dispatcherServlet.xml贴出来
  • 查看您的配置,似乎没有处理 dispatcher-servlet.xml,因为在您的 web.xml 中,参数 contextConfigLocation 仅指 applicationContext.xml。也许您可以将两个文件合并为一个以确保弹簧配置正确?首先,请确保您的 Spring 设置正常工作(将记录器级别设置为 DEBUG 会有所帮助)。你也可以在你的服务类中设置一个断点来查看它是否被加载了。
  • 我的 dispatcherServlet.xml 中只有:。所以我把这个命令放到我的应用程序上下文中并得到了相同的结果。
【解决方案2】:

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

希望对你有帮助

【讨论】:

  • 我认为服务扩展了组件......只是更专业
  • 你说得对,我也记得...我认为问题在于用户没有将组件扫描放在他们的应用程序上下文 xml 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
相关资源
最近更新 更多